       Re: 'NonlinearFit` confusion

• To: mathgroup at smc.vnet.net
• Subject: [mg49895] Re: [mg49844] 'NonlinearFit` confusion
• From: Yasvir Tesiram <yat at omrf.ouhsc.edu>
• Date: Thu, 5 Aug 2004 09:21:59 -0400 (EDT)
• References: <200408041446.KAA20105@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Hi,

Why not take less points, rather than more??

In:=
Clear[datay,datax,data1]
datay = Table [6 + 2Sin[3 + x], {x, -10Pi, 10Pi, 0.05}];
datax = Table [x, {x, -10Pi, 10Pi, 0.05}];
data1 = Table[{datax[[i]], datay[[i]]}, {i, 1, Length[datay]}];
Length[data1]
First[data1]

Out=
1257

Out=
{-31.4159,6.28224}

In:=
Clear[a,b,c,d,e]
sinFit1=NonlinearFit[Take[data1,40], c + a *Sin[d + e *x], x, {a, c,
d,e}]

Out=
6.\[InvisibleSpace]+2. Sin[3.\[InvisibleSpace]+1. x]

In:=
Clear[y,datay,datax,a,b,c,d,e]
datay = Table [6 + 2Sin[3 + 5 x], {x, -10Pi, 10Pi, 0.05}];
datax = Table [x, {x, -10Pi, 10Pi, 0.05}];
data2 = Table[{datax[[i]], datay[[i]]}, {i, 1, Length[datay]}];
Length[data2]
First[data2]

Out=
1257

Out=
{-31.4159,6.28224}

In:=
y=a*Sin[d+e x]+c;
sinFit2=NonlinearFit[Take[data2,40],y, x, {a,c, d,e}]

Out=
6.\[InvisibleSpace]+2. Sin[122.381\[InvisibleSpace]+5. x]

DisplayTogether[
Plot[sinFit1, {x, -10Pi, -8 Pi}, PlotStyle -> Blue],
Plot[sinFit2, {x, -10Pi, -8 Pi}, PlotStyle -> Red],

ListPlot[Take[data1, 40], PlotStyle -> {Blue, PointSize[0.011]}],
ListPlot[Take[data2, 30], PlotStyle -> {Red, PointSize[0.011]}],
Prolog -> {
{
Blue,
Text["sinFit1", {-27.5, 7}, {-1, 0},
TextStyle -> {FontFamily -> "Times",
FontWeight -> "Bold",
FontSize -> 18
}
]
},

{
Red,
Text["sinFit2", {-26.9, 6}, {-1, 0},
TextStyle -> {FontFamily -> "Times",
FontWeight -> "Bold",
FontSize -> 18
}
]
}
},
ImageSize -> 800,
Frame -> False,
AxesOrigin -> {-31.5, 4.0}

]

Cheers
Yas

On Aug 4, 2004, at 9:46 AM, 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]}];
>
> **** 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]}];
>
> ***** 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.
>
> Am I missing something fundamental here?
>
> Any assistance would be greatly appreciated.
>
> PS: I'm using Mathematica 5.0 on a MAC.
>
Dr. Yasvir A. Tesiram
Associate Research Scientist
Oklahoma Medical Research Foundation
Free Radical Biology and Ageing Research Program
825 NE 13th Street, OKC, OK, 73104

P: (405) 271 7126
F: (405) 271 1795
E: yat at omrf.ouhsc.edu

```

• Prev by Date: Re: 'NonlinearFit` confusion
• Next by Date: Re: Recursion depth
• Previous by thread: Re: 'NonlinearFit` confusion
• Next by thread: Re: Re: 'NonlinearFit` confusion