Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #15740 > unrolled thread
| Started by | Igor <igor.volkov@gmail.com> |
|---|---|
| First post | 2011-01-31 08:23 +0000 |
| Last post | 2011-02-01 11:54 +0000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
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
| From | Igor <igor.volkov@gmail.com> |
|---|---|
| Date | 2011-01-31 08:23 +0000 |
| Subject | FindFit bug |
| Message-ID | <ii5rhr$fh4$1@smc.vnet.net> |
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.
[toc] | [next] | [standalone]
| From | "Sjoerd C. de Vries" <sjoerd.c.devries@gmail.com> |
|---|---|
| Date | 2011-02-01 11:52 +0000 |
| Message-ID | <ii8s6e$2lo$1@smc.vnet.net> |
| In reply to | #15740 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Gary Wardall <gwardall@gmail.com> |
|---|---|
| Date | 2011-02-01 11:54 +0000 |
| Message-ID | <ii8s9j$2oc$1@smc.vnet.net> |
| In reply to | #15740 |
On Jan 31, 2: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.
Numerical procedures are like little girls. =93When they are good they
are very very good but when they are bad they are very very bad=94. In
other words when ever you use a procedure that has a numerical
component you run the risk of not getting the correct answer. In
Mathematica 7=92 look at ?NSolve "Possible Issues"
Try their example:
poly = Expand[Product[x - i, {i, 30}]];
NSolve[poly == 0, x]
Solve[poly == 0, x]
FindFit is a numerial procedure also and when used use with caution,
Gary Wardall
[toc] | [prev] | [next] | [standalone]
| From | "Kevin J. McCann" <Kevin.McCann@umbc.edu> |
|---|---|
| Date | 2011-02-01 11:52 +0000 |
| Message-ID | <ii8s5o$2l3$1@smc.vnet.net> |
| In reply to | #15740 |
The problem is that for a nonlinear fit there may be local minima. You
need to give FindFit a reasonably good initial guess. For example
fit = FindFit[dat, a Sin[w t + f], {{a, 2}, w, f}, t]
starts with a at 2, and the result gives the correct answer. I usually
use Manipulate to get a good first guess on the parameters for a 1d
function like this.
Kevin
On 1/31/2011 3:23 AM, Igor 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.
>
[toc] | [prev] | [next] | [standalone]
| From | Albert Retey <awnl@gmx-topmail.de> |
|---|---|
| Date | 2011-02-01 11:54 +0000 |
| Message-ID | <ii8s9u$2ok$1@smc.vnet.net> |
| In reply to | #15740 |
Hi,
> 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.
Which version are you using? For me it gives a warning with version 8
but not with version 7. In both cases using Method "NMinimize" yields a
correct result:
fit = FindFit[dat, a Sin[w t + f], {a, w, f}, t,
Method -> "NMinimize"]
It is interesting to see that version 8 finds the parameters with
negative signs when no additional constraints are given (which is of
course also a correct result and you can enforce the positive signs with
an additional constraint)...
hth,
albert
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web