Trigonometric fit not working in Mathematica?

I'm trying to fit some periodic experimental data and found that methods that work for fitting a curve to other functions don't work for trigonometric - at least - the way I do it.

Here is my code:

falseData = Table[{x, N[3*Sin[4*x]]}, {x, 10}];
model = a*Sin[b*x];
fit = NonlinearModelFit[falseData, model, {a, b}, x]
Show[ListPlot[falseData, PlotStyle -> Red], Plot[fit[x], {x, 1, 10}]]

      

And this is what the code generates:

FittedModel[-0.184706 Sin[1.00073 x]]

      

A failure of a curve fit

It works great if I switch the Sin functions in this example to Log or another type of function, but it fails when I try to use Sin or Cos.

Any suggestions?

+3


source to share


1 answer


Try using NMinimize method:

falseData = Table[{x, N[3*Sin[4*x]]}, {x, 10}];
model = a*Sin[b*x];
fit = NonlinearModelFit[falseData, model, {a, b}, x, Method -> NMinimize]
Show[ListPlot[falseData, PlotStyle -> Red], Plot[fit[x], {x, 1, 10}]]

      

Here's the result:



FittedModel[-3. Sin[2.28319 x]]

      

And here is the resulting curve:

result of fitting

+3


source







All Articles