Re: Fitting a function to a list (newbie)

• To: mathgroup at smc.vnet.net
• Subject: [mg21869] Re: Fitting a function to a list (newbie)
• From: "Kevin J. McCann" <kevin.mccann at jhuapl.edu>
• Date: Wed, 2 Feb 2000 22:54:25 -0500 (EST)
• Organization: Johns Hopkins University Applied Physics Lab, Laurel, MD, USA
• References: <872jej\$civ@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```I believe that what you want is a least squares fit to your data.

I have attached a NB at the bottom. Let me know if it helps.

Kevin
--

Kevin J. McCann
Johns Hopkins University APL

<mathprof at bigfoot.com> wrote in message news:872jej\$civ at smc.vnet.net...
> I'm new to Mathematica 4.0, so I apologize if this question is overly
> trivial (and please let me know if there's a place for trivial
> question).
>
> I have a list that I'd like to fit to the function:
>
> A + B*Sin[C*x+D]
>
> In other words, I'd like to find the (approximate numeric) values of
> A,B,C,D that yield the best fit to my data.
>
> I tried using Fit[], but it only applies to linear combinations of
> functions, and couldn't optomize the values of C and D.
>
> For this specific problem, I'm guessing some sort of Fourier transform
> might work, but I'm interested in a more general solution -- for example
> if Sin[] were replaced with a more complicated function.
>
> Any help appreciated!
>
>
> Sent via Deja.com http://www.deja.com/
>
(***********************************************************************

Mathematica-Compatible Notebook

This notebook can be used on any computer system with Mathematica 4.0,
MathReader 4.0, or any compatible application. The data for the notebook
starts with the line containing stars above.

To get the notebook into a Mathematica-compatible application, do one of
the following:

* Save the data starting with the line of stars above into a file
with a name ending in .nb, then open the file inside the application;

* Copy the data starting with the line of stars above to the
clipboard, then use the Paste menu command inside the application.

Data for notebooks contains only printable 7-bit ASCII and can be
sent directly in email or through ftp in text mode.  Newlines can be
CR, LF or CRLF (Unix, Macintosh or MS-DOS style).

NOTE: If you modify the data for this notebook not in a Mathematica-
compatible application, you must delete the line below containing the
word CacheID, otherwise Mathematica-compatible applications may try to
use invalid cache data.

applications, contact Wolfram Research:
web: http://www.wolfram.com
email: info at wolfram.com
phone: +1-217-398-0700 (U.S.)

Notebook reader applications are available free of charge from
Wolfram Research.
***********************************************************************)

(*CacheID: 232*)

(*NotebookFileLineBreakTest
NotebookFileLineBreakTest*)
(*NotebookOptionsPosition[      3910,        134]*)
(*NotebookOutlinePosition[      4669,        160]*)
(*  CellTagsIndexPosition[      4625,        156]*)
(*WindowFrame->Normal*)

Notebook[{

Cell[CellGroupData[{
Cell["Multivariate LSQ fit", "Title"],

Cell["\<\
by Kevin McCann
kevinmccann at Home.com\
\>", "Author"],

Cell["Here is a \"data\" function", "Text"],

Cell[BoxData[
\(\(g[x_] = 4 + 3\ Sin[2 + x];\)\)], "Input",
CellLabel->"In[91]:="],

Cell["Generate a random list of x-values", "Text"],

Cell[BoxData[
\(\(xlist = Table[Random[Real, {0, 4  \[Pi]}], {100}];\)\)], "Input",
CellLabel->"In[92]:="],

Cell["and the corresponding data list with some noise added", "Text"],

Cell[BoxData[
\(\(dataList = \(({#, g[#] + Random[Real, {\(-1.5\), 1.5}]} &)\)\  /@ \
xlist;\)\)], "Input",
CellLabel->"In[93]:="],

Cell["and here is a plot of the data", "Text"],

Cell[BoxData[
\(\(pdata =
ListPlot[dataList,
PlotStyle \[Rule] {RGBColor[0, 0, 1],
AbsolutePointSize[5]}];\)\)], "Input",
CellLabel->"In[94]:="],

Cell["Here is the function we wish to fit to.", "Text"],

Cell[BoxData[
\(f[x_, a_, b_, c_, d_] := a\  + \ b\ Sin[c + d\ x]\)], "Input",
CellLabel->"In[97]:="],

Cell["\<\
and a Module to do a least-squares sum for a given set of a,b,c,d.\
\>", "Text"],

Cell[BoxData[
\(lsFit[a_, b_, c_, d_] :=
Module[{}, \[IndentingNewLine]Plus\  @@ \
Apply[\((f[#1, a, b, c, d]\  - \ #2)\)\^2 &,
dataList, {1}]\[IndentingNewLine]]\)], "Input",
CellLabel->"In[98]:="],

Cell["e.g.", "Text"],

Cell[BoxData[
\(lsFit[1, 1, 1, 1]\)], "Input",
CellLabel->"In[99]:="],

Cell["\<\
Now just find the minimum of that. I have given each of the parameters an \
initial guess of one.\
\>", "Text"],

Cell[BoxData[
\(ans =
FindMinimum[lsFit[a, b, c, d], {a, 1}, {b, 1}, {c, 1}, {d, 1},
MaxIterations -> 1000]\)], "Input",
CellLabel->"In[100]:="],

Cell["Define a function for the best fit:", "Text"],

Cell[BoxData[
\(ff[x_] = f[x, a, b, c, d] /. ans[\([2]\)]\)], "Input",
CellLabel->"In[101]:="],

Cell["Plot it along with the original data", "Text"],

Cell[BoxData[
\(\(Plot[ff[x], {x, 0, 4  \[Pi]}, PlotStyle -> {RGBColor[1, 0, 0]},
Epilog -> pdata[\([1]\)]\[IndentingNewLine]];\)\)], "Input",
CellLabel->"In[102]:="]
}, Open  ]]
},
FrontEndVersion->"4.0 for Microsoft Windows",
ScreenRectangle->{{0, 1152}, {0, 791}},
WindowToolbars->{"RulerBar", "EditBar"},
WindowSize->{496, 695},
WindowMargins->{{36, Automatic}, {23, Automatic}},
ShowCellLabel->False,
StyleDefinitions -> "ArticleClassic.nb"
]

(***********************************************************************
Cached data follows.  If you edit this Notebook file directly, not using
Mathematica, you must remove the line containing CacheID at the top of
the file.  The cache data will then be recreated when you save this file
from within Mathematica.
***********************************************************************)

(*CellTagsOutline
CellTagsIndex->{}
*)

(*CellTagsIndex
CellTagsIndex->{}
*)

(*NotebookFileOutline
Notebook[{

Cell[CellGroupData[{
Cell[1739, 51, 37, 0, 72, "Title"],
Cell[1779, 53, 62, 3, 60, "Author"],
Cell[1844, 58, 43, 0, 27, "Text"],
Cell[1890, 60, 88, 2, 30, "Input"],
Cell[1981, 64, 50, 0, 27, "Text"],
Cell[2034, 66, 112, 2, 30, "Input"],
Cell[2149, 70, 69, 0, 27, "Text"],
Cell[2221, 72, 147, 3, 30, "Input"],
Cell[2371, 77, 46, 0, 27, "Text"],
Cell[2420, 79, 185, 5, 46, "Input"],
Cell[2608, 86, 55, 0, 27, "Text"],
Cell[2666, 88, 107, 2, 30, "Input"],
Cell[2776, 92, 90, 2, 27, "Text"],
Cell[2869, 96, 235, 5, 65, "Input"],
Cell[3107, 103, 20, 0, 27, "Text"],
Cell[3130, 105, 75, 2, 30, "Input"],
Cell[3208, 109, 121, 3, 44, "Text"],
Cell[3332, 114, 166, 4, 46, "Input"],
Cell[3501, 120, 51, 0, 27, "Text"],
Cell[3555, 122, 100, 2, 30, "Input"],
Cell[3658, 126, 52, 0, 27, "Text"],
Cell[3713, 128, 181, 3, 62, "Input"]
}, Open  ]]
}
]
*)

(***********************************************************************
End of Mathematica Notebook file.
***********************************************************************)

```

• Prev by Date: Re: ContourPlot in arbitrary shape domain.
• Next by Date: Re: Re: Problem with evaluation of delayed rules
• Previous by thread: Re: Fitting a function to a list (newbie)
• Next by thread: ErrorListPlot point size control?