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 necessary packages. Enter this command once and only before using any other fitting commands.

Needs["NonlinearRegression`"]
Needs["ErrorBarPlots`"]

Now 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.

myfit=NonlinearRegress[data2,N0*Exp[-lambda*xt],{N0,lambda},xt,RegressionReport->{BestFit,BestFitParameters,EstimatedVariance,ParameterTable}]

2. Plotting the Results

To display the data2 array and a curve on a single plot use the commands shown below. Notice the DisplayFunction->Identity option 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 using the DisplayFunction->$DisplayFunction option.

plot1 = ListPlot[data2,
PlotStyle->{RGBColor[0,0,1],PointSize[0.015]},
DisplayFunction->Identity]
plot2 = Plot[4802.271 (E^(-0.245147x)),{x,0,60},
PlotStyle->{Thickness[0.02],RGBColor[1,0,0]},
PlotRange->{0,5000},
DisplayFunction->Identity,
Frame->True,
FrameLabel->{"time (min)","Counts in 10 seconds","Nuclear Decay",""}]
Show[{plot2,plot1},DisplayFunction->$DisplayFunction]