Articles/Mathematics/Other/Least Squares Method

Have you ever wondered how programs like Microsoft Excel are able to extract equations for polynomials and lines from a series of points (aka trend line)? If so, you may find the following information enlightening. Excel uses a method of numerical analysis called the "least squares method".

First of all, you need a series of two or more points of the format indicated in figure 1.1. The more points you have, the better the equation approximation is going to be. Also, a polynomial can usually fit a series of points better than a line, assuming the data is nonlinear. If you are solving for a polynomial equation, the accuracy of the function will increase greatly if you increase the order (number of terms).

Point Format:

We will start with the line method, since linear equations are the simplest. As I have mentioned before, you will need a series of points. Now you will need to find the sum of all of the x-values, the sum of all the y-values, and the sum of all of the squared x-values (squared prior to addition). Once you have these values, you can easily calculate the line equation using the method diagrammed below:

Equation for a Line:

Equation for A:

Equation for B:

Now for the polynomial method! In this case, things are getting a little more complex. You need to decide how many terms you want (remember that the number of terms determines the accuracy), but you need at least three (a second order polynomial). Once you have determined that, you can build your matrices using the template shown below:

Equation for Polynomial:


Once you have your matrices constructed, you need to solve for the constants. This can be accomplished by using a calculator with matrix operations, a computer program, Gaussian elimination, and programs like MATLAB.

I also have written a program in C++ that will perform all of this automatically, I should be able to post it shortly.