Re: Can I use NonlinearModelFit to model some data with a

*To*: mathgroup at smc.vnet.net*Subject*: [mg120006] Re: Can I use NonlinearModelFit to model some data with a*From*: Morris Pearl <morris.pearl at gmail.com>*Date*: Tue, 5 Jul 2011 05:10:17 -0400 (EDT)*References*: <201107041044.GAA02488@smc.vnet.net>

Thanks to both responders for the suggestion. That does indeed work much better. Is there some place in the documentation with information about that option, and what it does? On Mon, Jul 4, 2011 at 11:00 AM, Daniel Lichtblau <danl at wolfram.com> wrote: > > ----- Original Message ----- > > From: "morris" <morris.pearl at gmail.com> > > To: mathgroup at smc.vnet.net > > Sent: Monday, July 4, 2011 5:44:35 AM > > Subject: Can I use NonlinearModelFit to model some data with a > piece wise linear function?? > > 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). > > > > Thank you for any thoughts. > > > > Happy Independence Day to Americans. > > > > > --------------------------------------------------------------------------- > > > > > > In[49]:= > > > > 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}] > > > > Out[49]= {{-13.782,28.218},{-12.8514,26.1486},{-11.9445,24.0555}, > > {-10.9566,22.0434},{-9.85094,20.1491},{-8.75561,18.2444}, > > {-7.92158,16.0784},{-6.99195,14.008},{-5.79096,12.209}, > > {-4.95951,10.0405},{-3.96988,8.03012},{-2.92945,6.07055}, > > {-1.98475,4.01525},{-0.877226,2.12277},{0.157389,0.157389}, > > {1.10679,-1.89321},{2.17641,-3.82359},{3.22352,-5.77648}, > > {4.04408,-7.95592},{5.17738,-9.82262},{6.16403,-11.836}, > > {7.23777,5.23777},{8.03634,5.03634},{9.01375,5.01375},{10.196,5.19598}, > > {11.0892,33.0892},{12.2308,36.2308},{13.2204,39.2204}, > > {14.0469,42.0469},{15.0948,45.0948},{16.1524,48.1524}, > > {17.2123,51.2123},{18.0879,54.0879},{19.0543,57.0543}, > > {20.1223,60.1223}} > > > > In[50]:= f[x_] := Piecewise[{ {a1 + x b1, x < c1}, {a2 + x b2, c1 > > <= x <= c2}, {a3 + x b3, x > c2}}] > > > > > > Out[50]= \[Piecewise] a1+b1 x x<c1 > > a2+b2 x c1<=x<=c2 > > a3+b3 x x>c2 > > 0 True > > > > > > In[51]:= m = NonlinearModelFit[curve, f[x], {c1,c2,a1,b1,a2,b2,a3,b3}, > > {x}] > > > > > > ] > > In[52]:= Normal[m] > > > > Out[52]= \[Piecewise] 0.227214 -2.0132 x x<1. > > 1. +1. x 1.<=x<=1. > > -22.4279+4.20451 x x>1. > > 0 True > > Probably gets stuck in a not-so-good local minimum. Could try instead: > > m = NonlinearModelFit[curve, > f[x], {c1, c2, a1, b1, a2, b2, a3, b3}, {x}, Method -> NMinimize] > > This gives a much better result. See: > > Plot[Normal[m], {x, -15, 22}] > > Daniel Lichtblau > Wolfram Research > > >

**References**:**Can I use NonlinearModelFit to model some data with a piece wise linear function??***From:*morris <morris.pearl@gmail.com>