MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Can I use NonlinearModelFit to model some data with a piece wise linear function??

  • To: mathgroup at smc.vnet.net
  • Subject: [mg120005] Re: Can I use NonlinearModelFit to model some data with a piece wise linear function??
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Tue, 5 Jul 2011 05:10:06 -0400 (EDT)

On 7/4/11 at 6:44 AM, morris.pearl at gmail.com (morris) wrote:

>I attempt to do this, but it does not seem to work very well.  See
>example session below.  I first create some data out of three linear
>functions, plus a little bit of random perturbation.  Then I have a
>function f[x] which is a piecewise linear function, with three
>pieces.

>NonlinearModelFit seems however to respond with only two pieces, by
>giving the same value to the two parameters which represent the cut
>points (c1 and c2 in the below example).

>curve = Join[Table[{i, -2i},{i,-14,6}], Table[{i,5},{i,7,10}],
>Table[{i, 3 i}, {i, 11, 20}]] + Table[Random[Real,0.25],{35}]

>In[50]:=   f[x_] := Piecewise[{  {a1 + x b1, x < c1}, {a2 + x b2, c1
><= x <= c2}, {a3 + x b3, x  > c2}}]

You are attempting to fit a curve with discontinuities to your
data. This will definitely not work well with the default
arguments to NonlinearModelFit since the gradient isn't
continuous. There are a couple of things that can be done to
improve the fitting,

First write the function to be fitted in terms of the
Mathematica function HeavisideTheta rather than using Piecewise.
Mathematica knows how to compute the derivative of HeavisideTheta.

Second, specify the method to use as NMinimize. Note, you will
also likely want to specify the method for NMiminize to be
DifferentialEvolution or SimulatedAnnealing.

Take a look at

tutorial/ConstrainedOptimizationOverview
tutorial/UnconstrainedOptimizationOverview

for more information



  • Prev by Date: Re: Numerical accuracy/precision - this is a bug or a feature?
  • Next by Date: Re: Can I use NonlinearModelFit to model some data with a piece wise linear function??
  • Previous by thread: Re: Can I use NonlinearModelFit to model some data with a
  • Next by thread: Re: Can I use NonlinearModelFit to model some data with a piece wise linear function??