Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #15811

Re: FindFit bug

From "Sjoerd C. de Vries" <sjoerd.c.devries@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: FindFit bug
Date 2011-02-01 11:52 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <ii8s6e$2lo$1@smc.vnet.net> (permalink)
References <ii5rhr$fh4$1@smc.vnet.net>

Show all headers | View raw


This is not a bug.

As the documentation of FindFit clearly states: "In the nonlinear
case, it finds in general only a locally optimal fit."

So, Mathematica has found a local optimum but not a global one due to
its starting values of the parameters. Try providing some starting
parameters that are in the right neighbourhod and you'll see it finds
a much better fit:

fit = FindFit[dat, a Sin[w t + f], {{a, 3.3}, {w, 2.5}, {f, 1.5}}, t]
{a -> 3., w -> 3., f -> 1.}

Another option would be to experiment with different methods:

In[10]:= FindFit[dat, a Sin[w t + f], {a, w, f}, t,
   Method -> #] & /@ {"ConjugateGradient", "Gradient",
  "LevenbergMarquardt", "Newton", "NMinimize", "QuasiNewton"}


Out[10]= {{a -> 8.68943, w -> 0.0199648,
  f -> 0.00865477}, {a -> 2.54082, w -> 0.0678487,
  f -> 0.0294072}, {a -> 0.599211, w -> 1.51494,
  f -> 3.80421}, {a -> 53.7406, w -> 0.00322877,
  f -> 0.00139811}, {a -> -3., w -> -3., f -> -1.}, {a -> 24.9028,
  w -> 0.00696196, f -> 0.00301903}}

In this case NMinimize seems to be the best choice.

Cheers -- Sjoerd

On Jan 31, 9:23 am, Igor <igor.vol...@gmail.com> wrote:
> Hello,
> It seems that FindFit cannot fit a sine function.
> It produces no warnings and gives a totally
> wrong answer:
>
> dat = Table[{t, 3 Sin[3 t + 1]}, {t, -3, 3, 0.1}];
>
> fit = FindFit[dat, a Sin[w t + f], {a, w, f}, t]
>
> Show[ListPlot[dat], Plot[a Sin[w t + f] /. fit, {t, -3, 3}]]
>
> Output: {a -> 0.599211, w -> 1.51494, f -> 3.80421}
>
> At the same time it fits Sin[3t+1] just fine.

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

FindFit bug Igor <igor.volkov@gmail.com> - 2011-01-31 08:23 +0000
  Re: FindFit bug "Sjoerd C. de Vries" <sjoerd.c.devries@gmail.com> - 2011-02-01 11:52 +0000
  Re: FindFit bug Gary Wardall <gwardall@gmail.com> - 2011-02-01 11:54 +0000
  Re: FindFit bug "Kevin J. McCann" <Kevin.McCann@umbc.edu> - 2011-02-01 11:52 +0000
  Re: FindFit bug Albert Retey <awnl@gmx-topmail.de> - 2011-02-01 11:54 +0000

csiph-web