Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: NonlinearFit works not so good

  • To: mathgroup at
  • Subject: [mg49587] Re: NonlinearFit works not so good
  • From: "Kevin J. McCann" <kmccann at>
  • Date: Sat, 24 Jul 2004 03:47:17 -0400 (EDT)
  • Organization: University of Maryland, Baltimore County
  • References: <cdqqa4$kkd$>
  • Sender: owner-wri-mathgroup at

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

  RowBox[{\(p1 = Plot[100\ Sin[3\ x + 1] + 2, {x, 0, 5}];\),
    "\n", \(data = p1[\([\)\(1, 1, 1, 1\)\(]\)];\),
    "\n", \(p2 = ListPlot[data];\), "\n",
    RowBox[{\(f[a_, b_, c_, d_, x_]\), ":=",
      StyleBox[\(a\ Sin[b\ \ x + c] + d\),
    "\n", \(lsfit[a_, b_, c_, d_]\  := \
      Plus\  @@ Apply[\ \((f[a, b, c, d, #1] - #2)\)\^2 &, data, {1}]\),
    "\n", \(lsfit[100, 3, 0, 2]\),
    "\n", \(sol =
        lsfit[a, b, c, d], {a, 90, 91}, {b, 2.5, 2.6}, {c, 0, 1}, {d, 0, 1},
        MaxIterations \[Rule] 1000]\),
    "\n", \(Plot[f[a, b, c, d, x] /. sol[\([\)\(2\)\(]\)], {x, 0, 5},
        Epilog \[Rule] {AbsolutePointSize[4], p2[\([\)\(1\)\(]\)]}];\)}]\)

Kevin J. McCann
Joint Center for Earth Systems Technology (JCET)
Department of Physics
Baltimore MD 21250

"Daohua Song" <ds2081 at> wrote in message
news:cdqqa4$kkd$1 at
> Dear group,
>       I find the NonlinearFit give an answer far away from the truth; Here
> is an example:
>      Plot[100 Sin[3 x+1]+2,{x,0,5}];
> data=Cases[Graphics[%],Line[x___]?x,Infinity];
> <<Statistics`NonlinearFit`
> NonlinearFit[data//First,A Sin[B x+C]+D,{x},{A,B,C,D}];
> Plot[%,{x,0,5}]
> ListPlot[data//First]
>      If i chop the +2 from above, it works fine. but The whole thing gives
> bad fit again if you change {x,0,5}->{x,0,10}
>      So NonlinearFit also is not stable!
>      I hope it should be improved
> Daohua

  • Prev by Date: Re: New GUIKit for Mathematica
  • Next by Date: Re: Fundamental theorem problem
  • Previous by thread: Re: NonlinearFit works not so good
  • Next by thread: Re: Re: NonlinearFit works not so good