\input{labpreamble}
\begin{document}

\parskip=.15in

\subsubsection*{PHSX 221L\hfill Method of Least Squares\hfill Name:\underline{\hspace*{2in}}}

% \underline{Title:} Polynomial Curve Fitting: Method of Least Squares

\underline{Object:} To obtain a ``least squares fit'' for different 
sets of data, using both a linear fit and a parabolic (or second 
order) fit.

\underline{Theory:} Often in laboratory work a scientist or engineer
makes a series of measurements and the data $(x_{i},y_{i})$ is
displayed by plotting it on a graph.  In order to show a correletation
or a relationship among the data points it is usually desirable to
obtain a line or a curve that will represent the data as closely as
possible.

However, measured data contain errors of one type or another which 
cause the data points to be scattered randomly around some 
hypothetical line or curve.  Thus, in order to find this 
representative curve we must use some averaging technique.  The 
\underline{method of least squares} is one such technique.  To obtain 
the curve which best fits the data, we calculate the sum of the 
squares of the differences between the hypothetical curve and each 
data point, and then minimize this sum.

\begin{center}
\begin{picture}(250,140)
\thicklines
\put(15,15){\line(1,0){180}} % x-axis
\put(15,15){\line(0,1){125}} % y-axis
\put(80,-5){\makebox(0,0)[b]{abscissa}} % axis label
\put(-30,30){\shortstack{o\\r\\d\\i\\i\\n\\a\\t\\e}} % axis label
\thinlines
\put(15,15){\line(1,1){130}}% best fit line
\put(28,24){\circle*{3}} % data point
\put(53,58){\circle*{3}}  % data point
\put(78,82){\circle*{3}}  % data point
\put(90,100){\circle*{3}}  % data point
\put(128,110){\circle*{3}} % data point
\put(139,136){\circle*{3}} % data point
\put(153,95){\makebox(0,0)[b]{$(x_{i},y_{i})$}}
\put(128,15){\line(0,1){5}} % tic mark
\put(128,1){\makebox(0,0)[b]{$x_{i}$}}
\put(15,110){\line(1,0){5}} % tic mark
\put(7,105){\makebox(0,0)[b]{$y_{i}$}}
\put(15,128){\line(1,0){5}} % tic mark
\put(-2,120){\makebox(0,0)[b]{$f(x_{i})$}}
\put(128,110){\line(0,1){18}}
\put(55,25){\shortstack{best fit\\line or curve\\is $f(x)$}}
\end{picture}
\end{center}

If the representative curve is expected to be linear, we write the 
equation of a line
\begin{equation}
f(x)=Ax + B;
\end{equation}
if it is thought to be parabolic, then
\begin{equation}
f(x)=Ax^{2} + Bx + C.
\end{equation}
More complicated relationships can be fit to higher-order polynomial 
curves:
\begin{equation}
f(x)=Ax^{n} + Bx^{n-1} +Cx^{n-2} + \cdots + Gx + H.
\end{equation}

In any case, the quantity to be minimized is
\begin{equation}
E = \sum_{i=1}^{N}[f(x_{i}) - y_{i}]^{2}
\end{equation}
where $y_{i}$ is the measured data value (ordinate) corresponding to 
the abscissa value $x_{i}$, and the sum is over the $N$ data points.

The quantity $E$ is a function of the unknown coefficients $A, B, C,$ 
etc., and therefore to minimize $E$ we take a partial derivative with 
respect to each of these coefficients and equate the result to zero.

\[ 
\frac{\partial E}{\partial A}=0,\ \ \ \ \ \frac{\partial E}{\partial
B}=0,\ \ \ \ \ \frac{\partial E}{\partial C}=0,\ \cdots
\]

This procedure results in a set of simultaneous equations for the 
unknown coefficients $A, B, C,\ldots$ which can then be solved by 
standard techniques.  Thus, the polynomial function $f(x)$ that best 
fits the data is found.

If the above procedure is carried out for the straight line case, the 
two simultaneous equations for $A$ and $B$ in $f(x) = Ax + B$ are:
\begin{eqnarray}
A\sum x_{i}^{2} + B\sum x_{i} & = & \sum y_{i}x_{i}\\
A\sum x_{i} \, + \, B\sum 1 & = & \sum y_{i}.
\end{eqnarray}


Or, in the case of a second order curve:
\begin{eqnarray}
A\sum x_{i}^{4}  +  B\sum x_{i}^{3}  +  C\sum x_{i}^{2} & = & \sum y_{i}x_{i}^{2}\\
A\sum x_{i}^{3}  +  B\sum x_{i}^{2}  +  C\sum x_{i} & = & \sum y_{i}x_{i}\\
A\sum x_{i}^{2}  \, + \, B\sum x_{i} \, + \, C\sum 1 & = & \sum y_{i}
\end{eqnarray}
where each sum is still from $i=1$ to $i=N$.

% \begin{equation}
% A\sum x_{i}^{4} + B\sum x_{i}^{3} + C\sum x_{i}^{2} = \sum y_{i}x_{i}^{2}
% \end{equation}
% \begin{equation}
% A\sum x_{i}^{3} + B\sum x_{i}^{2} + C\sum x_{i}= \sum y_{i}x_{i}
% \end{equation}
% \begin{equation}
% A\sum x_{i}^{2} + B\sum x_{i} + C\sum 1 = \sum y_{i}
% \end{equation}
% where each sum is still from $i=1$ to $i=N$.

 Note that while these summations can be performed by hand, 
they are easier with a modern scientific calculator, and easier 
still with a spreadsheet program on a computer.  

 The solution of the system of linear equations can be
accomplished by a variety of techniques.  You'll recall from algebra
class that such systems can be solved by substitution, elimination,
row-echelon form with back substitution, Gaussian elinimation,
Cramer's rule, and matrices.  Your calculator probably has a solver
for linear systems, and you should be able to get your spreadsheet to
invert the appropriate matrix as well.  Use these tools for the sums and for 
the solution to the linear systems in this lab.

 Furthermore, your calculator, no doubt, has a built-in
linear regression function, and it is performing this least squares
calculation; your spreadsheet program probably also has the ability to
do linear (and maybe higher order) regressions (again using this
method of least squares).  However, by learning the details of this
method in this lab you are equipped to extend the method to other
situations and kinds of functional fits.  Once you have learned how
this method works in detail you are welcome to use the built-in
regression functions of your calculator or spreadsheet on future labs.

\newpage

\underline{Procedure:} 
\vspace{-.25in}
\begin{enumerate}

\item\label{line} Use the data from your previous experiment, in which 
the circumference of a set of disks versus the radius of each disk was 
measured, to obtain a least squares straight line fit for the plot of 
circumference versus radius graph that was obtained in that 
experiment.  Make a graph with the original data points and the best 
fit line.  Compare the value you obtain for $A$ with $2\pi$.

\item Suppose you have some compelling theoretical reason to believe 
that the straight line in procedure \ref{line} \emph{must} go through 
the origin (\emph{i.e.} $B=0$).  Go through the mathematical procedure 
described above to arrive at an equation for $A$ if the best fit line 
is of the form $f(x)=Ax$.  Find $A$ (from the same data set) and 
compare with the $A$ from procedure \ref{line}.  (For this to work 
well, you need a data set without a lot of systematic error.  A data 
set with systematic error could give you a better slope if you 
{\em don't} force the line through the origin, even if you have 
reason to believe it should go through the origin.)

\item Do a second order least squares procedure on your mass versus
radius data from the same previous experiment to find the equation of
the best fit parabola.  From your feeling for the physics of the
situation, what would you predict $B$ and $C$ to be (before you
calculate them)?  Compare $A$ with $\rho\pi t_{\rm ave}$.  Make a
graph with the original data points and the best fit curve.

\item Do a second order least squares procedure on your displacement
versus time data from the previous experiment in which you measured
the acceleration of a freely-falling body to find the equation of the
best fit parabola.  From your feeling for the physics of the
situation, what would you predict $B$ and $C$ to be (before you
calculate them)?  (See equation 2.11 in Serway, replacing $x$'s with
$y$'s.)  Compare $A$ with $\frac{1}{2}g$.  Make a graph with the
original data points and the best fit curve.

\item\label{hyp} Use the following hypothetical set of data to find a
least squares straight line and a least squares parabola.  Give the
values of $A, B, C,$ and $E$. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%										  %
%	\begin{center}						  %
%	\begin{tabular}{c|c}				  %
%	   $x_{i}$ &   $ y_{i}$	  \\ \hline	  %
%		   4   &	   2	  \\		  %
%		  12   &	   4	  \\		  %
%		  20   &	   5	  \\		  %
%		  28   &	   6	  \\		  %
%		  32   &	  10	  \\		  %
%		  40   &	  11	  \\		  %
%		  48   &	  16	  \\		  %
%		  60   &	  18	  \\		  %
%		  68   &	  24	  \\		  %
%		  76   &	  26	  \\		  %
%		  80   &	  30	  \\		  %
%	\end{tabular}						  %
%	\end{center}						  %
%										  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{center}
\begin{tabular}{c|ccccccccccc}
   $x_{i}$ &   4& 12& 20& 28& 32& 40& 48& 60& 68& 76& 80  \\ \hline
  $ y_{i}$ &   2&  4&  5&  6& 10& 11& 16& 18& 24& 26& 30  \\
\end{tabular}
\end{center}

\item Plot both curves and the data from procedure \ref{hyp} on the 
same graph.  (It would be a good idea to use a computer to plot the 
functions.)  Determine which curve fits the data best 
(\emph{i.e.} which $E$ is smallest?). The strength of the \emph{linear} 
association between two variables is often indicated by the 
correlation coefficient, or Pearson product-moment correlation 
coefficient, $r$, which can be expressed as 
\begin{equation}
	r=\frac{\sum xy - \frac{(\sum x)(\sum y)}{N}}
	{\sqrt{\left (\sum x^{2} - \frac{(\sum x)^{2}}{N}\right )\left (\sum 
	y^{2} - \frac{(\sum y)^{2}}{N}\right )}}.
\end{equation}
Your calculator or spreadsheet probably reports this correlation 
coefficient and/or its sqaure (also known as the coefficient of 
determination).

\end{enumerate}

\newpage

\underline{Discussion:} 
\vspace{-.25in}
\begin{enumerate}
\item How well do your least squares curves appear to fit the data?

\item What would the least squares polynomial of degree 0 (a 
horizontal line) be for $N$ points?

\item What did you learn from this lab exercise?

\end{enumerate}

% \newpage \ 

\end{document}
