# Obtaining quadratic equation using Least Squares Method

This question is most likely extremely trivial, but I'm having some difficulty obtaining the least squares equation from the following data points:

{{1.08, 0}, {1.07, 0.0659232}, {0.97, 0.1695168}, {0.77, 0.188352}, {0.84, 0.0847584}}

In particular, I'm trying to obtain a quadratic equation using least squares, so I was wondering if someone could show me the method they used to obtain it.

I have based my working (and obtained the data above) from the following link: http://www.maths.manchester.ac.uk/~pjohnson/resources/math60082/lecture-monte-carlo-ls.pdf

and I have tried to obtain the least squares equation by using the method as given on slide 14.

My working is given as follows:

Firstly, since I am trying to find a quadratic least squares form, I must solve three equations and attempt to find the coefficients $a_{0}, a_{1}, a_{2}$. Using the equations on slide 14 given in the link above, I then plug in the data points I was given into the equations, which are now given below:

(0 + .07*.94176 + .18*.94176 +.20*.94176 + .09*.94176) = 5*$a_{0}$ + $a_{1}$*(1.08 + 1.07 + .97 + .77 + .84)+$a_{2}$*(1.08^2 + 1.07^2 + .97^2 + .77^2 + .84^2)

(0*1.08 + .07*.94176*1.07 + .18*.94176*.97 +.20*.94176* .77 + .09*.94176*.84) = $a_{0}$*(1.08 + 1.07 + .97 + .77 + .84)+$a_{1}$*(1.08^2 + 1.07^2 +.97^2 + .77^2 + .84^2)+$a_{2}$*(1.08^3 + 1.07^3 + .97^3 + .77^3 + .84^3)

(.07*.94176*1.07^2+.18*.94176*.97^2+.20*.94176*.77^2+.09*.94176*.84^2) = $a_{0}$*(1.08^2 + 1.07^2 + .97^2 + .77^2 + .84^2)+$a_{1}$*(1.08^3 + 1.07^3 + .97^3 + .77^3+.84^3)+$a_{2}$*(1.08^4+1.07^4+.97^4 + .77^4 + .84^4)

However, putting this into Wolfram Alpha gives coefficient values -1.13685, 3.12955, and -1.89201, which are incorrect, since the quadratic equation should be -1.81357x^2 + 2.9834x - 1.06998.

If it's not too much trouble could someone please show me how to obtain the quadratic equation using least squares, or at least show me what I've done wrong?

• Quadratic Fit WA . Maybe you are wondering the opposite, why your calculations gave an incorrect result?
– Moo
Feb 3, 2016 at 3:52
• Yeah pretty much this - I just want to know what I've done wrong in my calculations. As far as I can tell I've followed the equations (as given in the link above) correctly, but the final quadratic fit comes out incorrect. By plugging the data points into wolfram alpha (or basically any solver) I'm able to obtain the correct equation, but I want to try and obtain the equation by hand and I can't seem to get it... Feb 3, 2016 at 4:12
• How did you get an $a_3$ in your equations?
– Moo
Feb 3, 2016 at 4:21
• Sorry, that was supposed to be $a_{2}$, I've fixed it now Feb 3, 2016 at 4:33
• Why in the world did someone downvote this? The OP showed all of his work, a link to the notes he used for the calculations, what is supposed to be the correct result and more. Unreal!
– Moo
Feb 3, 2016 at 5:04

I formulated it a bit different, but you should be able to follow along as all of these calculations will be identical to what you should be getting. I used page 30 as a guide.

We have $m = 2, n = 5$ and the data $(x_i, y_i)$ pairs are:

$$\text{data}=\left( \begin{array}{cc} 1.08 & 0 \\ 1.07 & 0.0659232 \\ 0.97 & 0.169517 \\ 0.77 & 0.188352 \\ 0.84 & 0.0847584 \\ \end{array} \right)$$

We have:

• $\displaystyle \sum_{i=1}^5 1 = 5, \sum_{i=1}^5 x_i = 4.73, \sum_{i=1}^5 x^2_i = 4.5507, \sum_{i=1}^5 x^3_i = 4.44666, \sum_{i=1}^5 x^4_i = 4.40598$
• $\displaystyle \sum_{i=1}^5 y_i = 0.50855, \sum_{i=1}^5 x_i y_i = 0.451197, \sum_{i=1}^5 x^2_i y_i = 0.406453$

We now solve the linear system:

$$\left( \begin{array}{ccc} 5 & 4.73 & 4.5507 \\ 4.73 & 4.5507 & 4.44666 \\ 4.5507 & 4.44666 & 4.40598 \\ \end{array} \right)\left( \begin{array}{ccc} a_0 \\ a_1 \\ a_2 \\ \end{array} \right) = \left( \begin{array}{ccc} 0.5085504000000001 \\ 0.451197216 \\ 0.40645325664 \end{array} \right)$$

This gives the least squares coefficients as:

$$a_0 = -1.06998, a_1 = 2.9834, a_2 = -1.81357$$

Update

I used your equations (with some slight editing) in Mathematica and got the correct result. Are you sure Wolfram Alpha isn't cutting off part of the equations?

Here is the Mathematica input and results:

  Solve[{(0 + .07*.94176 + .18*.94176 + .20*.94176 + .09*.94176) ==
5*a0 + a1*(1.08 + 1.07 + .97 + .77 + .84) +
a2*(1.08^2 + 1.07^2 + .97^2 + .77^2 + .84^2), (0*1.08 + .07*.94176*1.07 +
.18*.94176*.97 + .20*.94176*.77 + .09*.94176*.84) ==
a0*(1.08 + 1.07 + .97 + .77 + .84) +
a1*(1.08^2 + 1.07^2 + .97^2 + .77^2 + .84^2) +
a2*(1.08^3 + 1.07^3 + .97^3 + .77^3 + .84^3), (.07*.94176*1.07^2 +
.18*.94176*.97^2 + .20*.94176*.77^2 + .09*.94176*.84^2) ==
a0*(1.08^2 + 1.07^2 + .97^2 + .77^2 + .84^2) +
a1 (1.08^3 + 1.07^3 + .97^3 + .77^3 + .84^3) +
a2*(1.08^4 + 1.07^4 + .97^4 + .77^4 + .84^4)}, {a0, a1, a2}]


Output:

$$\{\{\text{a0}\to -1.06998,\text{a1}\to 2.9834,\text{a2}\to -1.81357\}\}$$

• Thanks for that, this method was much more helpful in computing the equation. Were you able to find out what was wrong with my equations? If not then it's no issue, I'm mostly just curious Feb 3, 2016 at 5:28
• The method I used is just much cleaner in my opinion and that makes it less error prone, especially if the data set increases. You can compare each of your numbers to the calculations I show (they should match exactly) and it will be easy to spot the error.
– Moo
Feb 3, 2016 at 5:30
• Haha... I honestly have no idea what I've done wrong, I've compared the calculations and they appear to be identical. Your method appears to work perfectly though, so I'll definitely being using that in the future :) Thanks for all the help, I would have voted up your comment but I don't have enough reputation points unfortunately... Feb 3, 2016 at 5:45
• All of your method calculations are correct! There must be some issue in the method or setup for solving the system. How are you doing that?
– Moo
Feb 3, 2016 at 5:52
• @ThePlowKing45: See my update using Mathematica. Something basic is going wrong when you are solving the resulting system (maybe digits getting cut off by WA or something like that.
– Moo
Feb 3, 2016 at 6:03