# Least Squares Fits

### Assignment :

Start Homework 13, Read Section 18.8 in the text, and the Web notes on Numerical Integration

### New Fortran:

none

The least squares method is used to determine parameters for a chosen family of curves to best fit a set of data points. It is commonly used for two related applications. The first is as a way to save computer time over the use of interpolation. There are times when you have a set of data that is appropriate for interpolation (look at what I did with viscl.f), but have determined that you don't need as much accuracy as interpolation could give you. Given the acceptable levels of uncertainty in your calculation, you would be happy with a simple function (perhaps a polynomial) that passes close to, but not through all of the data points. Homework 13 is an example of such an exercise. Evaluating viscosity with the homework function (a cubic polynomial) is quicker and requires less programming than the use of an interpolation based subroutine.

A second valuable use of least squares fitting occurs when your data points include a significant amount of error, and are not directly useful for making predictions. Based on some theoretical considerations, you determine the general form of a function that should match the behaviour of the source of your data. You then apply the least squares technique to choose values of undetermined parameters in the function, that result in a "best fit" of the function to the data.

As a warm-up to understanding this very general technique, take a look at my description of how to fit a simple straight line to a scattered set of data points. Once you are comfortable with the process of fitting a straight line, take a look at my Postscript description of fitting a quadratic function to data from a standard high school physics experiment, used to measure the value of gravitational acceleration "g". A sample program for this quadratic fit is available for study. The core of the fitting subroutine has been programmed to follow the general summation notation in my detailed discussion of a quadratic least squares fit. It is in fact fairly inefficient. You would want to look at tricks such as those demonstrated in changing lsq.f to lsq2.f or lsq3.f.

Once linear and quadratic fitting makes sense to you, take a look at my discussion of general polynomial fits and use of the least squares method for fitting other functional forms.