Nonlinear Fitting

One of the fundamental tasks in experimental physics is to compare theory with data. This comparison is often done by fitting a theory-motivated curve to data. In other words, some mathematical function produced by the physical theory has unknown parameters that can be varied so the function more closely resembles the data. In this laboratory you will be given a data set and develop such a procedure to find a best fit curve to the data.

1. Fit Commands

This section demonstrates the commands needed to fit a  function to a data. First get the data.  You have to make sure you set the full path correctly.

(* on a unix machine. *)
data1 = ReadList["/home/gilfoyle/tmp/Nuke.dat",{Number,Number}];

(* on a windows machine desktop. *)
data1 = ReadList["C:\\Documents and Settings\\ggilfoyl\\Desktop\\Nuke.dat",{Number,Number}];

Convert the time from seconds to minutes and create a new data array. For reasons yet to be understood this has to be done or the fit to the raw data array (data1) fails.

data2 = Table[{data1[[i,1]]/60,data1[[i,2]]},{i,1,Length[data1]}];

Fit the data to an exponential function of the form

f(t) = (Subscript[N, 0])(e^(-λt))

where (Subscript[N, 0])is a scale factor and λ is the decay constant and then print out the results.

myfit=NonlinearModelFit[data2,N0*Exp[-lambda*xt],{N0,lambda},xt]; (* Does the fit. *)
myfit["ParameterTable"] (* Prints fit parameters and uncertainties. *)

2. Plotting the Results

To display the data2 array and a curve on a single plot use the commands shown below. Notice the semicolon at the end of the line turns off plotting for the first two plots that have the data and curve. The last, combined-curve-and-data plot is displayed with the Show command.

plot1 = ListPlot[data2,
PlotStyle->{RGBColor[0,0,1],PointSize[0.015]}];
plot2 = Plot[myfit[x],{x,0,60},
PlotStyle->{Thickness[0.02],RGBColor[1,0,0]},
PlotRange->{0,5000},
Frame->True,
FrameLabel->{"time (min)","Counts in 10 seconds","Nuclear Decay",""},
BaseStyle->FontSize->16];
Show[{plot2,plot1}]

3. Plotting the Results with Uncertainties

We need a special software package to plot the data with uncertainties. To get that package enter the following command once and only before using the ErrorListPlot command.

Needs["ErrorBarPlots`"]

Create a new data array that includes the uncertainties on each point.

data3 = Table[{data2[[i]],ErrorBar[(Sqrt[data2[[i,2]]])]},{i,1,Length[data2]}];
plot3 = ErrorListPlot[data3,PlotStyle->{RGBColor[0,0,1],PointSize[0.010]}];
Show[{plot2,plot3},ImageSize->8.0*72,PlotRange->{{10,20},{0,500}}]