I want to curve fit some data points using a specific linear combination of trigonometric functions with the help of NonLinearModelfit as per the following code:

points = {{-5, 0.1768}, {-4, 0}, {-3, 0.5863},
{-2,0}, {-1, 0.3535}, {0, 0}, {1, 0.3535}, {2, 0}, {3,0.5863}, {4, 0}, 
model = a*Sin[b*x] + c*Sin[d*x];
fit = NonlinearModelFit[points, model, {a, b, c, d}, x]
Show[ListPlot[points, PlotStyle -> Red], Plot[fit[x], {x, -5, 5}]]

However, I got the following output which doesn't fit the data altogether:

FittedModel[-2.2204*10^-16 Sin[2.6912 x] - 1.1102*10^-16 Sin[4.6912 x]]

A guidance on how to go about evaluating this would be appreciated.


closed as off-topic by Michael Seifert, MarcoB, LCarvalho, Alexey Popkov, LLlAMnYP Oct 23 '17 at 12:36

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – MarcoB, LCarvalho
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ Your data is symmetric about $x = 0$: for example, you have the points {-1, 0.3535} and {1, 0.3535}. Meanwhile, your model necessarily satisfies $f(x) = -f(-x)$, regardless of the values of $a, b, c, d$. Since NonlinearModelFit minimizes the least-squares distance from the data points to the curve, the best fit for this model will set the amplitude of the sine waves to zero, so that the curve becomes $f(x) = 0$, which is equally far from the points at $x = \pm 1$. (Any non-zero amplitude would give a larger least-squares error.) This is exactly the result that Mathematica has given you. $\endgroup$ – Michael Seifert Oct 19 '17 at 17:25
  • 2
    $\begingroup$ I'm voting to close this question as off-topic because it deals with a mathematical issue in curve-fitting, not a problem with Mathematica. $\endgroup$ – Michael Seifert Oct 19 '17 at 17:27
  • 1
    $\begingroup$ This is not a serious question: the data size 11 is too small to make reliable conclusions. $\endgroup$ – user64494 Oct 19 '17 at 19:27
  • $\begingroup$ Thank you all for the comments. I have now understood the issue in my question. $\endgroup$ – Jayanth Jayakumar Oct 20 '17 at 6:52
  • $\begingroup$ I'm voting to close this question as off-topic because the model chosen is inadequate for the dataset (this is a variation on VTC due to simple mistake). As such, this isn't really a problem with Mathematica. $\endgroup$ – LLlAMnYP Oct 23 '17 at 12:36

You need to choose a model that can fit the data. First, the data is not zero mean, so you need to add a constant. Second, sinusoids have phase, so you need to add a phase term.

model = e + a*Sin[b*x + f] + c*Sin[d*x + g];
fit = NonlinearModelFit[points, model, {a, {b, 2}, c, {d, 4}, e, f, g}, x]

With that said, there is a reason that one usually uses Fourier methods to do decompositions of functions into sinusoids.


Not the answer you're looking for? Browse other questions tagged or ask your own question.