Re: NonlinearFit works not so good
- To: mathgroup at smc.vnet.net
- Subject: [mg49587] Re: NonlinearFit works not so good
- From: "Kevin J. McCann" <kmccann at umbc.edu>
- Date: Sat, 24 Jul 2004 03:47:17 -0400 (EDT)
- Organization: University of Maryland, Baltimore County
- References: <cdqqa4$kkd$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
I am a simple kinda guy; so, I tend to use a "roll-your-own" LSQ whenever
possible. I do this especially when I have a multivariate fit. The real
problem is that it is that there is no single answer due to the periodicity
of the sine. This makes a numerical search complicated. Anyway, here is my
version.
\!\(\*
RowBox[{\(p1 = Plot[100\ Sin[3\ x + 1] + 2, {x, 0, 5}];\),
"\n", \(data = p1[\([\)\(1, 1, 1, 1\)\(]\)];\),
"\n", \(p2 = ListPlot[data];\), "\n",
RowBox[{\(f[a_, b_, c_, d_, x_]\), ":=",
StyleBox[\(a\ Sin[b\ \ x + c] + d\),
FormatType->StandardForm]}],
"\n", \(lsfit[a_, b_, c_, d_]\ := \
Plus\ @@ Apply[\ \((f[a, b, c, d, #1] - #2)\)\^2 &, data, {1}]\),
"\n", \(lsfit[100, 3, 0, 2]\),
"\n", \(sol =
FindMinimum[
lsfit[a, b, c, d], {a, 90, 91}, {b, 2.5, 2.6}, {c, 0, 1}, {d, 0, 1},
MaxIterations \[Rule] 1000]\),
"\n", \(Plot[f[a, b, c, d, x] /. sol[\([\)\(2\)\(]\)], {x, 0, 5},
Epilog \[Rule] {AbsolutePointSize[4], p2[\([\)\(1\)\(]\)]}];\)}]\)
Cheers,
Kevin
--
Kevin J. McCann
Joint Center for Earth Systems Technology (JCET)
Department of Physics
UMBC
Baltimore MD 21250
"Daohua Song" <ds2081 at columbia.edu> wrote in message
news:cdqqa4$kkd$1 at smc.vnet.net...
> Dear group,
> I find the NonlinearFit give an answer far away from the truth; Here
> is an example:
> Plot[100 Sin[3 x+1]+2,{x,0,5}];
> data=Cases[Graphics[%],Line[x___]?x,Infinity];
> <<Statistics`NonlinearFit`
> NonlinearFit[data//First,A Sin[B x+C]+D,{x},{A,B,C,D}];
> Plot[%,{x,0,5}]
> ListPlot[data//First]
>
> If i chop the +2 from above, it works fine. but The whole thing gives
> bad fit again if you change {x,0,5}->{x,0,10}
> So NonlinearFit also is not stable!
> I hope it should be improved
> Daohua
>
- Follow-Ups:
- Re: Re: NonlinearFit works not so good
- From: stephen layland <layland@wolfram.com>
- Re: Re: NonlinearFit works not so good
- From: DrBob <drbob@bigfoot.com>
- Re: Re: NonlinearFit works not so good