Re: 'NonlinearFit` confusion

*To*: mathgroup at smc.vnet.net*Subject*: [mg49878] Re: 'NonlinearFit` confusion*From*: Paul Abbott <paul at physics.uwa.edu.au>*Date*: Thu, 5 Aug 2004 09:20:41 -0400 (EDT)*Organization*: The University of Western Australia*References*: <ceqt91$jr7$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

In article <ceqt91$jr7$1 at smc.vnet.net>, klingot at yahoo.com (Klingot) wrote: > I'm trying to fit a sinusoidal function to my data using > 'NonlinearFit' but it's exhibiting rather odd behaviour. Please see my > examples below: > > EXAMPLE (1). > > **** As a test, I created a list of data from a function of the form y > = 6 + 2Sin[3 + x] with: > > datay = Table [6 + 2Sin[3 + x], {x, -10Pi, 10Pi, 0.05}]; > datax = Table [x, {x, -10Pi, 10Pi, 0.05}]; > data = Table[{datax[[i]], datay[[i]]}, {i, 1, Length[datay]}]; You can generate this data directly using data = Table[{x,6 + 2Sin[3 + x]}, {x, -10Pi, 10Pi, 0.05}] > **** Then tested to see whether NonlinearFit would correctly deduce > the equations parameters with: > > NonlinearFit[data, c + a Sin[d + e x], x, {a, c, d,e}] > > **** As expected, it gave me '6.+ 2. Sin[3. + 1. x]' ... exactly as > one would expect :) > > > EXAMPLE (2). > > **** Second test, I modified the equation by multiplying x by 5, ie. > y = 6 + 2Sin[3 + 5x]: > > datay = Table [6 + 2Sin[3 + 5 x], {x, -10Pi, 10Pi, 0.05}]; > datax = Table [x, {x, -10Pi, 10Pi, 0.05}]; > data = Table[{datax[[i]], datay[[i]]}, {i, 1, Length[datay]}]; After re-generating your data, data = Table[{x,6 + 2Sin[3 + 5 x]}, {x, -10Pi, 10Pi, 0.05}] have a look at it: ListPlot[data] I think you will see what the problem is. > ***** and applied the NonlinearFit as before: > > NonlinearFit[data, c + a Sin[d + e x], x, {a, c, d,e}] > > ***** but this time I get a wildly innacurate result: 6.000165 + > 0.025086 Sin[0.0080308 - 0.247967 x] > > Specifically, the parameters 'a', 'd' and 'e' are all completely in > error by orders of magnitude. > > I tried extending the range of the data to include more cycles of the > sinusoid, thereby making it more continuous/monotonic but that made no > difference. In 5.0 you can use FindFit instead of NonlinearFit. Note that you may still have to provide a reasonable initial guess for the parameters to obtain convergence. Cheers, Paul -- Paul Abbott Phone: +61 8 9380 2734 School of Physics, M013 Fax: +61 8 9380 1014 The University of Western Australia (CRICOS Provider No 00126G) 35 Stirling Highway Crawley WA 6009 mailto:paul at physics.uwa.edu.au AUSTRALIA http://physics.uwa.edu.au/~paul