[Math] Least Squares Fitting Quadratic Equation to a set of points

least squaresquadraticsregression analysis

My math skills from my college days are a bit rusty, so if my terminology is wrong, I apologize. I will try to be as clear as I can.

  • I have a set of 50 points on a plane that roughly follow the pattern for a curve described by the function $y = ax^2 + bx + c$.
  • I know I can use some sort of Least Squares Fitting algorithm to determine the best possible a, b, and c values.
  • "Math" language confuses the heck out of me at times. I've tried Googling for an answer to how to accomplish this, but everything seems to start off simple, then delve into theory involving partial derivatives and things I can't seem to follow to save my life.
  • I have a basic understanding of matrices.

My understanding based on what I have been able to get from online sources is that I can set up two matrices:

| {r1c1} {r1c2} {r1c3} |     | {y1} |
| {r2c1} {r2c2} {r2c3} |     | {y2} |
| {r3c1} {r3c2} {r3c3} |     | {y3} | 

And then perform basic matrix operations so that my first matrix ends up being an identity matrix, and if I do the same operations to the other matrix, the resulting values are my a, b, and c values.

My questions are:

  • Is my understanding and approach incorrect? If okay, awesome!
  • How do I use the 50 X and Y values to derive values for {r1c1} and {y1} and so on?

All the examples I find seem to want to show an example of 50 equations and 50 unknowns. I can't seem to find an example using 50 data points fitted to 3 unknowns, so all the i, n, k, talk of degrees, etc. just leaves me boggled – . the article Least Squares Fitting–Polynomial for example. Don't I need a 3 by 3 matrix to find a solution since I have 3 unknowns?

I thought based on what I was reading, that the values should be (sorry about the programmer-ish symbols, not sure how to make math symbols in the post):

| {Sum(1:50, x[i]^0)} {Sum(1:50, x[i]^1)} {Sum(1:50, x[i]^2} |  | {Sum(1:50, y[i]*x[i]^0} |
| {Sum(1:50, x[i]^1)} {Sum(1:50, x[i]^2)} {Sum(1:50, x[i]^3} |  | {Sum(1:50, y[i]*x[i]^1} |
| {Sum(1:50, x[i]^2)} {Sum(1:50, x[i]^3)} {Sum(1:50, x[i]^4} |  | {Sum(1:50, y[i]*x[i]^2} |

And then performing matrix operations on both matrices so that the left matrix ends up being the identity matrix and the right matrix ends up with my values for a, b, and c. But my results are wildly different than what Excel produces.

So either I am doing something wrong, or there is an error in my program to do this. Anyone that can make sense of this, please help! Thank you so much!

Best Answer

It seems that the mistake is to forget to inverse the matrix.

To make it understandable, a molre general example is shown below. Then, it is shown in the case of the quadratic function.

Note that $[M]^{-1}$ means the inverse of the matrix, not $\frac{1}{[M]}$.

enter image description here

Related Question