Re: FindFit bug
- To: mathgroup at smc.vnet.net
- Subject: [mg116083] Re: FindFit bug
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Tue, 1 Feb 2011 06:54:02 -0500 (EST)
On 1/31/11 at 3:23 AM, igor.volkov at gmail.com (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. Doing non-linear fitting is inherently difficult. FindFit can achieve the desired result when the initial starting point is sufficiently close to the optimum solution. That is In[10]:= FindFit[dat, a Sin[w t + f], {{a, 2}, {w, 2}, {f, 2}}, t] Out[10]= {a->3.,w->3.,f->1.} works. Alternatively, you can have FindFit use NMinimize which uses other fitting algorithms that work better in some cases. That is: In[11]:= FindFit[dat, a Sin[w t + f], {a, w, f}, t, Method -> "NMinimize"] Out[11]= {a->-3.,w->-3.,f->-1.} Note, when you fit to Sin[[3 t+1] you change the problem from a non-linear problem to a linear problem which has explicit solutions and should never cause FindFit to have problems.