4

I'm trying to fit a sinusoidal wave to some numerical data. I can do it by hand using manipulate, so I know roughly the sort of parameter values that I should get back, but functions such as FindFit or NonlinearModel fit are hopeless for some of my datasets.

For example,

data = {0.316228, -0.316228, 0.316228, -0.316228, 0.316228, -0.316228, 0.316228, -0.316228, 0.316228, -0.316228, 0.316228}
FindFit[data, a Sin[bx + c], {a,b,c},x]

FindFit returns

{a -> 0.0687053, b -> 1.13454, c -> 1.04675}

which is hopelessly off. By hand I know I should get something along the lines of

a ~ 0.4, 
c ~ -1,
b ~ 3.1

What am I doing wrong?

For other sinusoidal datasets, FindFit works perfectly!

Mashy
  • 85
  • 1
  • 5
  • Please see this link for reasons why you should now be using NonlinearModelFit. Also, you'll find with a bit of digging on this site that nonlinear least squares fits are much more successful when you add better starting guesses. – bobthechemist Oct 19 '14 at 21:40

1 Answers1

8

Use NonlinearModelFit and put in the best guess parameters from your Manipulate.

data = {0.316228, -0.316228, 0.316228, -0.316228, 0.316228, -0.316228,
   0.316228, -0.316228, 0.316228, -0.316228, 0.316228}
eqn[x_] := a Sin[b x + c] 
nlm = NonlinearModelFit[data, eqn[x], {{a, 0.4}, {c, -1}, {b, 3.1}}, 
  x]

Plot[nlm[x] /. fit, {x, 1, 11}, 
 Epilog -> {Red, Point /@ Transpose[{Range@Length@data, data}]}]

enter image description here

bobthechemist
  • 19,693
  • 4
  • 52
  • 138