MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: NonlinearFit works not so good

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49651] Re: [mg49587] Re: NonlinearFit works not so good
  • From: stephen layland <layland at wolfram.com>
  • Date: Mon, 26 Jul 2004 04:01:55 -0400 (EDT)
  • Mail-followup-to: mathgroup@smc.vnet.net
  • References: <cdqqa4$kkd$1@smc.vnet.net> <200407240747.DAA05792@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

so spake Kevin J. McCann [2004.07.24 @ 06:56]:
> I am a simple kinda guy; so, I tend to use a "roll-your-own" LSQ whenever
> possible. I do this especially when I have a multivariate fit. The real
> problem is that it is that there is no single answer due to the periodicity
> of the sine. This makes a numerical search complicated. Anyway, here is my
> version.

[...]

Although it's always good to roll your own numerical methods,
Mathematica 5's FindFit function will do exactly what you want:

In[1]:= p1 = Plot[100 Sin[3 x + 1] + 2, {x, 0, 5},
    DisplayFunction->Identity];
    
In[2]:= data = p1[[1,1,1,1]];

In[3]:= form = a Sin[ b x + c] + d;

In[4]:= sol = FindFit[data,form,{a,b,c,d},x,Method->QuasiNewton]

Out[4]= {a -> 100., b -> -3., c -> 2.14159, d -> 2.}

note that Sin[3x + 1] = -Sin[-3x -1] = Sin[-3x -1+Pi].

If you want the original, you can give a starting parameter to FindFit.

In[5]:= sol2 = FindFit[data,form,{a,{b,5},c,d},x,Method->QuasiNewton]

Out[5]= {a -> 100., b -> 3., c -> 1., d -> 2.}

In[6]:= Plot[form/.sol,{x,0,5},Epilog->Join[{Hue@0,PointSize[0.02]},
    Point/@data]]

    -ste\/e
--
/*-----------------------------*\
|       stephen layland         |
|  Technical Support Engineer   |
\*-----------------------------*/



  • Prev by Date: Re: Re: If-statement problems
  • Next by Date: debugger woes with MathLink r12 on Windows: "The breakpoint will not currently be hit."
  • Previous by thread: Re: Re: NonlinearFit works not so good
  • Next by thread: Integration of product of 3 MeijerG functions