Graphing and Curve Fitting

Introduction
	This notebook will help you get started with some basic tasks in Mathematica:  importing a text file containing data, doing simple manipulations of that list, graphing the data, and performing simple curve fitting.
	The first cell below is how you import the tab-delimited text file (which is what you get if you save a spreadsheet file as .txt) that contains your data.  Note that I’ve chosen to give the imported data the name rawData, and Mathematica will refer to it by that name from now on.

rawData=Import["/Users/ghr/Desktop/TestData.txt","Table"]

The first argument to the Import command is the full path to the data file you wish to work on.  The one I sent with this notebook is called “TestData.txt”.  You can get the path to the file’s location on your machine by using the “Insert...File Path” from the menu bar and selecting the file in the dialog box that comes up.  Note that, unless you put a semicolon at the end of a Mathematica command, you will get some kind of output, even if it’s only en echo of the input line.  In this case, it’s a listing of rawData.

graph1=ListPlot[rawData, PlotMarkers->{Automatic, Medium}, Frame->True,FrameLabel->{Independent Variable, Dependent Variable},PlotLabel->Style["A Typical Plot",Blue,Large,FontFamily->Times]]

Note that you use ListPlot to make a scatter plot of data; Plot is used to graph a mathematical function.  I’ve added a few bells and whistles just to jazz it up; feel free to consult the help pages for more.  To make a bare-bones plot, you really only need ListPlot[rawData].  By the way, you can make that right arrow symbol with the Typesetting menu on the Writing Assistant palette, or you can just type - followed by > and let Mathematica put them together.

fixedData=Take[rawData,10]

Since that last point is obviously an outlier, we used the Take function to take the first 10 data point pairs (thus leaving out that last suspicious point) and made a new list called fixedData.  Here’s the graph.

graph2=ListPlot[fixedData]

Since the graph looks vaguely like a parabola, I’m going to choose that functional form to fit the data.  First I define a function named fitFun as a general parabola with parameters A, B, and C.

fitFun=A x^2 + B x + C

Now I’m going to let Mathematica perform the curve fitting and put the best fit parameters in a list called soln (so I can refer to the three paramater values in a shorthand way later).  Note that FindFit wants four inputs:  the data list to fit, the function to fit with, the list of adjustable parameters, and the name(s) of the independent variable(s).

soln=FindFit[fixedData,fitFun,{A,B,C},x]

So now my best fit parameters are known, but I’d like Mathematica to construct the finished function for me as a complete function of x.  The /. command means “evaluated at”, so the following command tells Mathematica to take the general function fitFun and put in the parameter values found in soln.

y=fitFun/.soln

Now graph it using Plot, since it’s a function.  Note that I give it a list in curly brackets containing the name of the independent variable and the starting and ending values for the plot.

graph3=Plot[y,{x,0,9}]

Not I’d like to see how well I did, so I use the Show command to superimpose the last two plots.

Show[graph2,graph3]

If you want to cut to the chase, follow the steps below.  FindFit is good for learning, but NonlinearModelFit is the way to go...

But if you want more quantitative information about how good your fit is, you need to learn a slightly more complicated (but much more powerful) fit routine:  NonlinearModelFit

soln2=NonlinearModelFit[fixedData,fitFun,{A,B,C},x]

The information returned by NonlinearModelFit is different from that returned by FindFit, so you have to do the graph in a slightly different way.

graph4=Plot[soln2[x],{x,0,9}]

Show[graph2,graph4]

Looks like the same good fit we got with FindFit, but now we can quantify that.  (Check out the help page for NonlinearModelFit and go down a few pages into the Details and Options tab to find Properties.)

soln2["ParameterTable"]

Note that now we get an idea of the errors (at least the statistical errors) in our fit parameters.

soln2["RSquared"]

The value of the square of the correlation coefficient (that’s what (r^2)is called) gives another quantitative idea of the “goodness of fit”.  The closer this value gets to 1, the better the fit.  In the physical sciences, we typically want this value to be bigger than 0.9 for any firm conclusion.

ListPlot[soln2["FitResiduals"],Filling->Axis]

The fit residuals are just the amounts by which the curve fit misses the data value at each point.  You want theses residuals to be small compared to the y-values in the graph, and you want them to be ideally scattered about zero (some above, some below).  If these values are large, and especially if they show a smooth trend (smoothly going up or down, or making some smooth curve), this means that your fit function is probably not the correct one.  Choose another one and see what the residuals look like.