(*********************************************************************** 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. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@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[ 6449, 230]*) (*NotebookOutlinePosition[ 7090, 253]*) (* CellTagsIndexPosition[ 7046, 249]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Least-Square Fits in Mathematica", "Title", TextAlignment->Center, TextJustification->0, FontSize->18], Cell[TextData[{ "In this notebook, we'll look at fitting data in ", StyleBox["Mathematica", FontSlant->"Italic"], "." }], "Text"], Cell[CellGroupData[{ Cell["Clear symbols", "Section", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ In order to avoid interference from symbols defined in other \ notebooks, we first Clear and Remove all symbols. We assume that the \ relevant symbols are in the Global` context.\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell["Clear[\"Global`*\"]", "Input", AspectRatioFixed->True], Cell["Remove[\"Global`*\"]", "Input", AspectRatioFixed->True] }, Open ]], Cell[CellGroupData[{ Cell[TextData[{ "Using Fit [from Tam, \"A Physicist's Guide to ", StyleBox["Mathematica", FontSlant->"Italic"], "\"]" }], "Section", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ Fit[data,funs,vars] finds a least-squares fit to a list of data in \ terms of a linear combination of the functions in list funs of the variables \ in list vars. The argument funs can be any list of functions that depend \ only on the variables in list vars. This is easiest to see via an example. \ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{ Cell["Example: Speed of a Volkswagen", "Subsection"], Cell["\<\ Suppose we are given data of distance versus time for a car, where \ d is in meters and t in seconds. Find the equation that gives d as a \ function of t. t d 0 0 1 1.5 2 6.0 3 13.5 4 24.0 5 37.5 6 54.0 7 73.5 8 96.0 9 121.5\ \>", "Text"], Cell["\<\ First create lists called time and distance. We'll make the first \ using Table and do the second by hand.\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(time\ = \ Table[i, \ {i, 0, 9}]\)], "Input"], Cell[BoxData[ \(distance\ = \ {0, \ 1.5, \ 6.0, \ 13.5, \ 24.0, \ 37.5, \ 54.0, \ 73.5, \ 96.0, \ 121.5}\)], "Input"], Cell["\<\ We can make a list of lists by {time,distance}, but this is not the \ form used by Fit. This list looks like:\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(MatrixForm[{time, distance}]\)], "Input"], Cell["\<\ We need the data (which we'll call vwdata) in the form of a list of \ (x,y) pairs. Use Transpose to do this.\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(vwdata\ = \ Transpose[{time, \ distance}]\)], "Input"], Cell[BoxData[ \(MatrixForm[vwdata]\)], "Input"], Cell["\<\ Take a look at the data using ListPlot. We define the plot as \"p1\ \" so we can reference it later. Check the help browser for other options to \ PlotStyle. [To suppress the display of the plot, we could have added \ DisplayFunction->Identity to the ListPlot options.]\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(\(p1\ = \ ListPlot[vwdata, \ PlotStyle \[Rule] PointSize[ 0.025], \[IndentingNewLine]\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ AxesLabel \[Rule] {"\< t (s)\>", \ "\< d (m)\>"}];\)\)], "Input"], Cell["\<\ Our physics insight tells us that distance is a simple function of \ time. Let's try a linear combination of functions: 1, t, t^2, t^3:\ \>", \ "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(Fit[vwdata, {1, t, t^2, t^3}, t]\)], "Input"], Cell["\<\ We can use the function Chop to remove terms close to zero (less \ than 10^-10) and define a function d[t_]. We use = rather than := because we don't want a delayed evaluation.\.13\ \>", "Text",\ Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(d[t_]\ = \ Chop[\ Fit[vwdata, {1, t, t^2, t^3}, t]\ ]\)], "Input"], Cell["\<\ So we find the usual relationship between distance and time for \ constant acceleration. Let's plot it:\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(\(p2\ = \ Plot[d[t], {t, 0, 10}, \ PlotRange \[Rule] {0, 120}, \[IndentingNewLine]\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ AxesLabel \[Rule] {"\< t (s)\>", \ "\< d (m)\>"}];\)\)], "Input"], Cell["\<\ Now look at our fit graphically by using Show to display both plots \ together:\ \>", "Text", Evaluatable->False, AspectRatioFixed->True], Cell[BoxData[ \(\(Show[p1, p2];\)\)], "Input"] }, Open ]] }, Open ]] }, Open ]] }, FrontEndVersion->"4.0 for X", ScreenRectangle->{{0, 1280}, {0, 1024}}, WindowSize->{691, 889}, WindowMargins->{{Automatic, 270}, {Automatic, 48}} ] (*********************************************************************** 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, 114, 3, 93, "Title"], Cell[1856, 56, 139, 5, 32, "Text"], Cell[CellGroupData[{ Cell[2020, 65, 80, 2, 60, "Section", Evaluatable->False], Cell[2103, 69, 251, 6, 50, "Text", Evaluatable->False], Cell[2357, 77, 62, 1, 27, "Input"], Cell[2422, 80, 63, 1, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[2522, 86, 190, 7, 60, "Section", Evaluatable->False], Cell[2715, 95, 371, 8, 68, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[3111, 107, 53, 0, 45, "Subsection"], Cell[3167, 109, 429, 15, 248, "Text"], Cell[3599, 126, 179, 5, 32, "Text", Evaluatable->False], Cell[3781, 133, 65, 1, 27, "Input"], Cell[3849, 136, 131, 2, 27, "Input"], Cell[3983, 140, 182, 5, 32, "Text", Evaluatable->False], Cell[4168, 147, 61, 1, 27, "Input"], Cell[4232, 150, 181, 5, 32, "Text", Evaluatable->False], Cell[4416, 157, 75, 1, 27, "Input"], Cell[4494, 160, 51, 1, 27, "Input"], Cell[4548, 163, 344, 7, 68, "Text", Evaluatable->False], Cell[4895, 172, 256, 6, 43, "Input"], Cell[5154, 180, 210, 6, 50, "Text", Evaluatable->False], Cell[5367, 188, 65, 1, 27, "Input"], Cell[5435, 191, 255, 7, 50, "Text", Evaluatable->False], Cell[5693, 200, 87, 1, 27, "Input"], Cell[5783, 203, 175, 5, 32, "Text", Evaluatable->False], Cell[5961, 210, 241, 5, 43, "Input"], Cell[6205, 217, 151, 5, 32, "Text", Evaluatable->False], Cell[6359, 224, 50, 1, 27, "Input"] }, Open ]] }, Open ]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)