Suppose we have a set of experimental data that we want to fit with a straight line. For specificity, imagine that we have measured absorbance as a function of concentration in a test of the Beer-Lambert law:

concentration (M) absorbance 0.01 0.095 0.02 0.202 0.04 0.413 0.06 0.629 0.10 1.101 0.15 1.735First let's put this data into a list of (x, y) points:

We can plot the data withdata = { {0.01, 0.095}, {0.02, 0.202}, {0.04, 0.413},{0.06, 0.629}, {0.10, 1.101}, {0.15, 1.735} }

from which we see that the data looks fairly linear at low concentrations, but starts to curve upward as the concentration increases. (Well, it's actually a bit hard to see this, but it will become more evident once we try to fit the data with a straight line.)ListPlot[data]

To fit the data to a straight line, type

This tells Mathematica to fit the data in terms of two functions of the variableFit[data, {1, conc}, {conc}]

-0.044375 + 11.6875 concfrom which we see that the slope and y-intercept of the line are 11.6875 and -0.044375, respectively. (Note that Mathematica uses its default of six significant figures even though our data has fewer significant figures.)

The general form of the **Fit** command is:

The first argument is a list of (x, y) data points, in the standard Mathematica form of a nested list. The second argument is a list of functions that Mathematica will use to try to fit the points. The third argument is a list of the independent variables in the data set. In this example, we have only one independent variable, so the variable list is a list with one element: the variableFit[ <data-list>, <function-list>, <variable-list> ]

You might be asking yourself, "Why is there a 1 in the list of functions?" Good question! Let's try leaving the 1 out:

Here we have instructed Mathematica to fit the data to a straight line thatFit[data, {conc}, {conc}]11.2461 conc

A * 1 + B * concWhen we leave out the 1, we are telling Mathematica not to look for a constant term in the fitting equation. This is equivalent to finding the best constant B such that the data is described by the line

B * concwith A implicitly set to zero.

In the example presented here, we might **want** to
force the y-intercept to be zero; after all, that is the
sensible Beer-Lambert law prediction. But if our
apparatus is dirty or malfunctioning, it may register
a "baseline" absorbance even when there is nothing
in our sample cell. This would show up as a constant
error in the absorbance, independent of concentration.
So the fit to the line

A * 1 + B * conclets us estimate this error. Notice that the slopes of the two straight lines are slightly different.

As we mentioned in class, the Beer-Lambert law can break down at high concentrations. Let's add a quadratic term to our fitting equation to see if the data is described better that way:

Now let's plot both fits, along with the experimental data:linfit = Fit[data, {1, conc}, {conc}]-0.044375 + 11.6875 concquadfit = Fit[data, {1, conc, conc^2}, {conc}]-0.00325599 + 9.91865 conc + 11.1374 conc^2

We see that the quadratic curve does seem to fit the data better. In addition, the quadratic fit leads to a y-intercept which is much closer to zero, in accord with our intuition.points = ListPlot[data]curve1 = Plot[linfit, {conc, 0, 0.15}]curve2 = Plot[quadfit, {conc, 0, 0.15}]Show[points, curve1]Show[points, curve2]

Other parts of the Mathematica tutorial:

- Mathematica's user interface
- Simple calculations
- Plotting lists of points
- Plotting functions
- Combining two or more plots
- Generating and manipulating lists of numbers
- Complex numbers
- Derivatives and integrals
- Special functions
- Contour and surface plots