next up previous
Next: About this document ... Up: tutorial10 Previous: Further pachydermal spline fitting

Least Squares Fitting

When data is subject to random error, or when there are many data points and it is desirable to approximate the data by a simple function, least squares approximation is used.

\bgroup\color{red}\framebox{\em MAKE A NEW FILE}\egroup \bgroup\color{black}$\phantom{0}$\egroupCreate a new file called lsquare.m.

Imagine that data in a `perfect' experiment has the form \bgroup\color{black}$y=1+2t^2$\egroup. Generate data with this `perfect' form at points \bgroup\color{black}$t = 0,0.1,\dots,1.0$\egroup. In a real experiment, such perfect measurement is not possible, and typically there is some random error. To simulate such errors, we can use the random number generator available in MATLAB. The command ynoisy = y + randn(1,11) will add normally distributed random noise, with mean zero and variance one, to our data. Plot y and ynoisy against \bgroup\color{black}$t$\egroup.

\bgroup\color{red}\framebox{\em ADD CODE TO FILE}\egroup \bgroup\color{black}$\phantom{0}$\egroupNow use the MATLAB commands

c2 = polyfit(t,ynoisy,2); y2 = polyval(c2,t) ;

to generate a least-squares quadratic fit to the noisy data. Plot this on your graph as well. How good is the fit? In a new figure plot the error, given by abserror = abs(y -y2) . Try the experiment again, but this time reduce the noise level by a factor of ten. Note that even a moderate amount of noise can affect the accuracy of the least squares fit to the experimental data quite significantly.

\bgroup\color{blue}\framebox{\em CHECKPOINT: submit solution}\egroup \bgroup\color{black}$\phantom{0}$\egroup \bgroup\color{red}\framebox{ 3.}\egroup Give the values of the maximum absolute error for the two cases treated above (use a column vector).


next up previous
Next: About this document ... Up: tutorial10 Previous: Further pachydermal spline fitting
Charlie Macaskill 2004-07-26