You are on page 1of 9

Plotting Lab Data using Mathematica

The purpose of these notes is to show how Mathematica can be used to plot and analyze
laboratory data. This is a shortened version of the document, Graph Plotting and Data
Analysis using Mathematica, which you should read for a more complete description
of Mathematica plotting commands.
The graphs which you obtain will, usually, be one of the following forms:
a) straight line; f (x) = mx + b
b) simple polynomial; f (x) = a + bx + cx2 +
c) power law; f (x) = Axn
d) other functions not described by any of the above
It is good practice to reset everything before you begin a Mathematica session:
In[1]:= Clear["Global * "]

Reading data from a laboratory experiment

Most of the data that you obtain in the laboratory will consists of pairs of (x,y) values,
for example:
In[2]:= data = {{0, 6.62}, {1, 6.73}, {2, 6.86},
{3, 6.98}, {4, 7.03}};

One problem with this method of data entry is that is becomes laborious to type many
curly brackets and commas, as well as increasing the possibility of making mistakes.
An alternate method is to first create a data file using a text editor. A file which consists
of two columns of data might look like this, with a space between each column of
numbers:
.5 8.1
1 9.2
1.5 10.5
2 13.1
2.5 15.4
3 18
3.5 20.4
4 22.9
4.5 24.5
5 26.3
Save the file under a meaningful name, such as labdata.dat. The .dat file extension
tells you that this is a data file, as opposed any other kind of file, like text (.txt), a
picture (.jpg, .gif, .bmp), or a program (.exe).

1
There are several methods for telling Mathematica how to read a set of data. If the
file is not already in your default working directory, you will need to use SetDirectory
to make sure that Mathematica reads the data from the directory where you keep your
files. For example (the exact syntax will depend on your operating system - Windows,
Macintosh or Linux/Unix):
In[3]:= SetDirectory["c : labssample data"];

Lets read in the data file labdata.dat.


In[4]:= labdata = Import["labdata.dat"];

Plot this using ListPlot:


In[5]:= rawdata = ListPlot[labdata]

25
22.5
20
17.5
15
12.5
10
2 3 4 5
Out[5]= -Graphics-

You can add extra parameters if you like. Use whichever is most appropriate for your
situation. Fit these points to a straight line:
In[6]:= result = Fit[labdata, {1, x}, x]
Out[6]= 4.92667 + 4.33212 x

Obtain the line of best fit


In[7]:= bestline = Plot[result, {x, 0, 5}]

2
25

20

15

10

1 2 3 4 5

Plot the data and line of best fit on the same axes. Add a title and axis labels:
In[8]:= Show[rawdata, bestline,
AxesLabel {"x values", "y values"},
PlotLabel "Mathematica Graph"]

y values Mathematica Graph

25

20

15

10

x values
1 2 3 4 5

Out[8]= -Graphics-

A Shortcut

You can combine commands to produce a plot from a single line of input:
In[9]:= Plot[Fit[labdata, {1, x}, x], {x, 0, 5},
Epilog {PointSize[0.02],
Map[Point, labdata]}]

3
25

20

15

10

1 2 3 4 5
Out[9]= -Graphics-

For a more detailed statistical analysis, including the errors in the slope and intercept,
use the Regress command. You will need to load the LinearRegression package first.
In[10]:= << StatisticsLinearRegression

In[11]:= Regress[labdata, {1, x}, x]


Estimate SE TStat PValue
Out[11]= :ParameterTable 1 4.92667 0.407664 12.0851 2.03141 10-6 , RSquared 0.992694,
x 4.33212 0.131402 32.9685 7.81561 10-10
AdjustedRSquared 0.99178,
EstimatedVariance 0.356121,
DF SumOfSq MeanSq FRatio PValue
Model 1 387.075 387.075 1086.92 7.81561 10-10
ANOVATable
Error 8 2.84897 0.356121
Total 9 389.924

Polynomial Curve Fitting

You can fit data to any polynomial by including as many terms as you need inside the
curly brackets. The straight line is just a special case. Thus to fit a quadratic function
to the data file data2.dat, type
In[12]:= labdata2 = Import["data2.dat"]
Out[12]= {{0, 53.9}, {1, 39.3}, {2., 23.}, {3, 15.4},
{4., 9.7}, {5., 10.4}, {6., 12.4},
{7., 19.8}, {8, 24.2}, {9, 41.3}, {10, 63.9}}
In[13]:= abc = Fit[labdata2, {1, x, x2}, x]
Out[13]= 54.6923 - 19.0243 x + 1.96888 x2

In[14]:= Plot[abc, {x, 0, 10},


Epilog {PointSize[0.02],
Map[Point, labdata2]}]

4
60

50

40

30

20

10

2 4 6 8 10
Out[14]= -Graphics-

What does output from the Regress function look like?

Power Law Fit

Data which obeys a simple power law can be described by a mathematical function of
the form y = Axn . Taking the logarithm of each side gives log(y) = log(A) + n log(x).
Thus a plot of log(y) versus log(x) will give a straight line of slope n.
In[15]:= mydat = {{1, 249.9}, {2, 111.1}, {3, 56.2},
{4, 35.5}, {5, 27.1}, {6, 17.2}, {7, 14.1},
{8, 11.4}, {9, 8.7}, {10, 7.7}};

Calculate the natural logarithm and display:


In[16]:= logmydat = Log[mydat];

In[17]:= ListPlot[logmydat,
PlotStyle PointSize[0.02]]

5.5
5
4.5
4
3.5
3
2.5

0.5 1 1.5 2
Out[17]= -Graphics-

5
Fit to a straight line
In[18]:= logfit = Fit[logmydat, {1, x}, x]
Out[18]= 5.67613 - 1.55601 x

Hence n is equal to -1.55, and the functional relation is y = 291.8x-1.55 .


Display data ponts and line of best fit together:
In[19]:= Plot[logfit, {x, 0, 3},
Epilog {PointSize[0.02],
Map[Point, logmydat]}]

0.5 1 1.5 2 2.5 3


Out[19]= -Graphics-

OR
In[20]:= PlotB291.8 x-1.556 , {x, 1, 10},
Epilog {PointSize[0.02],
Map[Point, mydat]}F

250

200

150

100

50

2 4 6 8 10
Out[20]= -Graphics-

6
Nonlinear Curve Fitting

If your data does not follow a straight line or simple polynomial, you will need to use
Mathematicas NonlinearFit functions:
In[21]:= << StatisticsNonlinearFit

Example: Charging a Capacitor


In this experiment we measure the voltage across the capacitor as a function of time.
As usual, well read the data from a file:
In[22]:= chargedata = Import["capch.dat"]
Out[22]= {{15, 0.7}, {30, 1.2}, {45, 1.71}, {60, 2.13},
{75, 2.48}, {90, 2.78}, {120, 3.29},
{150, 3.66}, {180, 3.96}, {210, 4.19},
{240, 4.29}, {270, 4.49}, {300, 4.6}}

Plot in the usual way:


In[23]:= cdata = ListPlot[chargedata]

50 100 150 200 250 300


Out[23]= -Graphics-

Define the function and ask Mathematica to solve for a and b:


In[24]:= chrgft = NonlinearFit[chargedata,
a(1 - Exp[-x/ b]), x, {a, b}]
Out[24]= 4.83133 (1 - -0.00957528 x )

Plot it, adding a few extra features:


In[25]:= Plot[chrgft, {x, 0, 300},
AxesLabel- > {"Time (s)", "Voltage"},
PlotLabel "Capacitor Charging Up",
PlotStyle
{{Dashing[{0.03}], Thickness[0.005]}},
Epilog {PointSize[0.02],
Map[Point, chargedata]}]

7
Voltage Capacitor Charging Up

Time HsL
50 100 150 200 250 300

Out[25]= -Graphics-

The NonLinearRegress function gives output similar to Regress. You may not want
to display all the output.
In[26]:= chrgft = NonlinearRegress[chargedata,
a(1 - Exp[-x/ b]), x, {a, b}]
Out[26]= :BestFitParameters
{a 4.83133, b 104.436},
Estimate Asymptotic SE CI
ParameterCITable a 4.83133 0.0275347 {4.77073, 4.89194} ,
b 104.436 1.43567 {101.276, 107.595}
EstimatedVariance 0.000897664,

DF SumOfSq MeanSq
Model 2 140.442 70.2208
ANOVATable Error 11 0.0098743 0.000897664 ,
Uncorrected Total 13 140.451
Corrected Total 12 20.5537

AsymptoticCorrelationMatrix
1. 0.896223
J N, FitCurvatureTable
0.896223 1.
Curvature
Max Intrinsic 0.00800664
Max Parameter - Effects 0.0288556
95. % Confidence Region 0.50111

8
Memorial University of Newfoundland
Department of Physics and Physical Oceanography

Plotting Graphs by Computer: Exercises


1.) Fit the following data to a straight line. Determine the slope, intercept and their
uncertainties.

x : 12.23, 12.66, 13.04, 13.44, 13.78, 14.14, 14.5, 14.83, 15.19, 15.65;
y : 0.862, 0.904, 0.930, 0.962, 0.980, 1.017, 1.037, 1.056, 1.091, 1.144;

2.) Fit the following data to a quadratic function (y = a + bx + cx2 ) and also to a cubic
function (y = a + bx + cx2 + dx3 ). Which, in your opinion, gives the better fit? If this was
data from one of your experiments, how would you decide which result to use?

x : 22.3, 27.3, 29.7, 33.2, 39.7, 44.7, 49.6, 62.1, 67, 74.5, 84.4, 94.9, 99.3
y : 1501, 1298, 1054, 987, 905, 824, 643, 581, 555, 505, 398, 344, 327

3.) The force between two current-carrying conductors is described by the equation

I1 I2
F = `
2a

When the two currents are equal, we expect F to be proportional to I 2 . Plot the following
data on a suitable graph and test this hypothesis.

Force (mN) : 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55;
Current (A) : 2.6, 3.08, 3.75, 4.3, 4.74, 5.36, 5.64, 5.67, 6.29, 7.0;

4.) The number of counts measured from the decay of a radioactive isotope in the
laboratory is described by the equation

I = I et + B

where B represents additional counts due to background radiation. The following list shows
the number of counts measured in successive intervals of thirty seconds: 566, 445, 396, 331,
303, 282, 225, 220, 179, 176, 137, 145, 131, 109, 102, 83, 76, 75, 62, 76, 62, 55, 53, 51, 45,
40, 38, 29, 47, 36, 35, 28, 31, 27, 33, 38, 24, 27, 17, 27.
Use a nonlinear curve fitting procedure to determine the half-life of the isotope and also
determine an average value for the number of background counts measured in each interval.

Plotting Graphs by Computer: Exercises

You might also like