Linear Fitting Routine

PHYS375 Experimental Lab

The following packages are required for this notebook.  The first is required for the fit and the second is required for plotting error bars.

Needs["Statistics`LinearRegression`"]
Needs["Graphics`Graphics`"]
Off[General::spell, General::spell1]

Contents

Sample Data

User Inputs

Fit Parameters

Fit Plot

Sample Data

x = Range[1.0, 9];

y = {15.6, 17.5, 36.6, 43.8, 58.2, 61.6, 64.2, 70.4, 
          98.8};

xlabel = "L (cm)";
ylabel  = "T °C";
plottitle  = "TEMPERATURE vs LENGTH";

model = {1, z};

User Inputs

To use your data and model function, make your insertions  below.
Insert the following:

Data

You must enter the following information otherwise the default setting will be in effect.

1.  Assign the x to a single list called x

2.  Assign the y to a single list called y

3.  Assign a string to xlabel

4.  Assign a string to ylabel

5.  Assign a string to plottitle

Be sure to use these exact names!

Model Function

The model function takes the form:

\(model = \ 
      a\_0\  + \ \(a\_1\) f \((z)\)\  + \ \(a\_1\) 
          f \((z)\)\^2 + \ \(\(·\)\(·\)\(·\)\)\)

where z is some function.  This is a linear fit in the sense that the coefficients only multiply the functions and cannot be arguments.  The proper way to assign the model function is:

model  =  {1, Sin[z], Sin[z]^2}

Insert pint  below this line.

Data Conversion

data = Transpose[{x, y}];

datplot = 
      ListPlot[data, PlotLabel ->  plottitle, 
        AxesLabel -> {xlabel, ylabel}]

Regress Fitting Routine

result = 
      Regress[data, model, z, IncludeConstant -> False, 
        RegressionReport -> {ParameterTable, CovarianceMatrix, 
            FitResiduals, EstimatedVariance}]

The fit function is given by:

params = 
      Table[result[[1, 2, 1, i, 1]], {i, Length[model]}]

linfit = model . params

The uncertainties in the fit parameters are:

errors = 
      Table[result[[1, 2, 1, i, 2]], {i, Length[model]}]

The standard error of the fit is

sigma = (Sqrt[result])[[4, 2]]

This tells us how well we can determine T for a given z.  What we have used is the standard error.

erexpdata = Map[Append[#, sigma] &, data]

errorplot = 
      ErrorListPlot[erexpdata, PlotLabel -> plottitle, 
        AxesLabel -> {xlabel, ylabel}]

fitplot = 
      Plot[linfit, {z, 0, 10.0}, PlotLabel -> plottitle, 
        AxesLabel -> {xlabel, ylabel}]

Show[errorplot, fitplot]