Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #3467
| From | morris <morris.pearl@gmail.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Can I use NonlinearModelFit to model some data with a piece wise linear function?? |
| Date | 2011-07-04 10:46 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <ius5lt$2ft$1@smc.vnet.net> (permalink) |
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
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Can I use NonlinearModelFit to model some data with a piece wise linear function?? morris <morris.pearl@gmail.com> - 2011-07-04 10:46 +0000
csiph-web