TITLE: Finding a best fit second order polynomial
QUESTION [5 upvotes]: Problem:
Assume we have the following points:
$(x_0,y_0), (x_1,y_1), (x_2,y_2), (x_3,y_3)$ where $x_0 = -3$, $x_1 = -2$,
$x_2 = -1$ and $x_3 = 0$.
Given the function $f(x) = Ax^2 + Bx + C$
find the constants $A$,$B$ and $C$ such that
$f(0) = y_3$ and
$$d = \sum_{i = 0}^{2} (f(x_i) - y_{i})^2$$
is minimized.
Answer:
First we apply the requirement that $f(0) = y_3$.
\begin{align*}
f(0) &= A(0) + B(0) + C = y_3 \\
C &= y_3 \\
d &= \sum_{i = 0}^{2} (A(x_i)^2 + B(x_i) + y_3 - y_{i})^2
\end{align*}
We write $d_A$ to represent the partial derivative of $d$ with respect
to $A$.
\begin{align*}
d_A &= \sum_{i = 0}^{2} 2(x_i)^2(A(x_i)^2 + B(x_i) + y_3 - y_{i}) \\
d_B &= \sum_{i = 0}^{2} 2(x_i)(A(x_i)^2 + B(x_i) + y_3 - y_{i}) \\
\end{align*}
Now we set the partial derivatives to $0$ to find a minimum.
\begin{align*}
\sum_{i = 0}^{2} 2(x_i)^2(A(x_i)^2 \\
 +& B(x_i) + y_3 - y_{i}) &= 0 \\
\sum_{i = 0}^{2} 2(x_i)(A(x_i)^2 + B(x_i) \\
  +& y_3 - y_{i}) &= 0 \\
\sum_{i = 0}^{2} (A(x_i)^2 + B(x_i) + y_3 - y_{i}) &= 0 \\
\sum_{i = 0}^{2} (A(x_i)^2 + B(x_i) + y_3 - y_{i}) &= 0 \\
\end{align*}
Am I right so far? Now how do I proceed?
Now I solve the equation $d_A = 0$. Here is what I get:
\begin{align*}
 (A(-3)^2 + B(-3) + y_3 - y_{0})
  +  (A(-2)^2 + B(-2) + y_3 - y_{1})
 +  (A(-1)^2 + B(-1) + y_3 - y_{2})
 &= 0 \\
%
9A - 3B + y_3 - y_{0} +  4A - 2B + y_3 - y_{1} + A - B + y_3 - y_{2}
 &= 0 \\
%
14A - 6B  + 3y_3 - y_{0} - y_{1} - y_{2} &= 0 \\
\end{align*}
Now, I need to solve the equation $d_B = 0$. However, that will produce the same equation as $d_A = 0$. Therefore, I do not know how to find a unique value for $A$ and $B$.
Based upon the group's comments. I have updated my solution again. I believe my formula for A is correct. However, my formula for B
is wrong.
Answer:
First, I apply the requirement that $f(0) = y_3$.
\begin{align*}
f(0) = y_3 &= (A)(0) + (B)(0) + C \\
C &= y_3 \\
f(x) &= Ax^2 + Bx + y_3 \\
d &= (f(x_0) - y_{0})^2 + (f(x_1) - y_{1})^2 + (f(x_2) - y_{2})^2  \\
d &= ( Ax_0^2 + Bx_0 + y_3 - y_{0})^2 + ( Ax_1^2 + Bx_1 + y_3 - y_{1})^2
 + ( Ax_2^2 + Bx_2 + y_3 - y_{2})^2 
\end{align*}
I will write $d_A$ for the partial derivative of $d$ with respect to $A$.
\begin{align*}
d_A &=  2x_0^2( Ax_0^2 + Bx_0 + y_3 - y_{0})
 + 2x_1^2( Ax_1^2 + Bx_1 + y_3 - y_{1})
 + 2x_2^2( Ax_2^2 + Bx_2 + y_3 - y_{2}) \\
d_B &=  2x_0( Ax_0^2 + Bx_0 + y_3 - y_{0})
 + 2x_1( Ax_1^2 + Bx_1 + y_3 - y_{1})
 + 2x_2( Ax_2^2 + Bx_2 + y_3 - y_{2}) \\
\end{align*}
Now I set $d_A = 0$ and $d_B = 0$. I then solve for $A$ and $B$.
\begin{align*}
x_0^2( Ax_0^2 + Bx_0 + y_3 - y_{0})
 + x_1^2( Ax_1^2 + Bx_1 + y_3 - y_{1})
 + x_2^2( Ax_2^2 + Bx_2 + y_3 - y_{2})
&= 0 \\
x_0( Ax_0^2 + Bx_0 + y_3 - y_{0})
 + x_1( Ax_1^2 + Bx_1 + y_3 - y_{1})
 + x_2( Ax_2^2 + Bx_2 + y_3 - y_{2})
&= 0 \\
\end{align*}
Now, I am going to work on the first equation. I will substitute values
for $x_0$, $x_1$ and $x_2$
\begin{align*}
9( 9A - 3B + y_3 - y_0)
 + 4( 4A - 2B + y_3 - y_{1})
 + 1( A - B + y_3 - y_{2})
 &= 0 \\
98A - 36B + 14y_3 - 9y_0 - 4y_1 - y_2 &= 0 \\
\end{align*}
Now, I work on the second equation.
\begin{align*}
-3( 9A  - 3B + y_3 - y_{0})
 - 2( 4A - 2B + y_3 - y_{1})
 - ( A - B + y_3 - y_{2})
&= 0 \\
-36A + 14B - 6y_3 + 3y_0 + 2y_1 + y_2 &= 0 \\
\end{align*}
\begin{align*}
14B &= 36A + 6y_3 - 3y_0 - 2y_1 - y_2 \\
B &= \dfrac{ 36A + 6y_3 - 3y_0 - 2y_1 - y_2  } { 14 }
\end{align*}
Now we can solve the first equation for $A$.
\begin{align*}
98A - 36 \left( \dfrac{ 36A + 6y_3 - 3y_0 - 2y_1 - y_2  } { 14 } \right) + 14y_3 - 9y_0 - 4y_1 - y_2 &= 0 \\
%
98A - 18 \left( \dfrac{ 36A + 6y_3 - 3y_0 - 2y_1 - y_2  } { 7 } \right)
 + 14y_3 - 9y_0 - 4y_1 - y_2 &= 0 \\
%
686A - 18 \left( 36A + 6y_3 - 3y_0 - 2y_1 - y_2 \right)
+ (7)(14)y_3 - 63y_0 - 28y_1 - 7y_2 &= 0 \\
%
686A - 18 \left( 36A + 6y_3 - 3y_0 - 2y_1 - y_2 \right)
 + 98y_3 - 63y_0 - 28y_1 - 7y_2 &= 0 \\
%
38A - 18( 6y_3 - 3y_0 - 2y_1 - y_2 ) + 98y_3 - 63y_0 - 28y_1 - 7y_2 &= 0 \\
%
38A - 10y_3 - 18( - 3y_0 - 2y_1 - y_2 ) - 63y_0 - 28y_1 - 7y_2 &= 0 \\
38A - 10y_3 + 18( 3y_0 + 2y_1 + y_2 ) - 63y_0 - 28y_1 - 7y_2 &= 0 \\
38A - 10y_3 - 9y_0 + 8y_1 + 11y_2 &= 0 \\
\end{align*}
\begin{align*}
38A &= 10y_3 + 9y_0 - 8y_1 - 11y_2 \\
A &= \dfrac{ 10y_3 + 9y_0 - 8y_1 - 11y_2 }{38 }
\end{align*}
Now we solve for $B$.
\begin{align*}
B &=
 \dfrac{ 36 \left(  \dfrac{ 10y_3 + 9y_0 - 8y_1 - 11y_2 }{38 }\right)  + 6y_3 - 3y_0 - 2y_1 - y_2  } { 14 } \\
%
B &=
 \dfrac{ 18 \left(  \dfrac{ 10y_3 + 9y_0 - 8y_1 - 11y_2 }{19 }\right)  + 6y_3 - 3y_0 - 2y_1 - y_2  } { 14 } \\
B &=
 \dfrac{ 18(  10y_3 + 9y_0 - 8y_1 - 11y_2 ) + 19( 6y_3 - 3y_0 - 2y_1 - y_2  ) }{ 19(14)} \\
B &= \dfrac{  180y_3 +162y_0 - 144y_1 - 198y_2
  + 6(19)y_3 - 3(19)y_0 - 28y_1 - 19y_2  } { 19(14)} \\%
B &= \dfrac{  180y_3 +162y_0 - 144y_1 - 198y_2
  + 114y_3 - 57y_0 - 28y_1 - 19y_2  } {266} \\
B &= \dfrac{  294y_3 + (162-57)y_0 -144y_1 - 28y_1 - 198y_2 - 19y_2  } {266} \\
%
B &= \dfrac{  294y_3 + 105 y_0 - 172y_1 - 217y_2  } {266} \\
\end{align*}
Now I will try out the formula on the following set of points:
$$( -3, 0), (-2,1), (-1,2), (0,5)$$
We have:
\begin{align*}
y_0 &= 0 \\
y_1 &= 1 \\
y_2 &= 2 \\
y_3 &= 5 \\
C &= 5 \\
A &= \dfrac{ 10y_3 + 9y_0 - 8y_1 - 11y_2 }{38 } \\
A &= \dfrac{ 10(5) + 9(0) - 8(1) - 11(2) }{38 }
 = \dfrac{ 50 - 8 - 22 }{38 } \\
A &= \dfrac{ 10 }{ 19 } \\
B &= \dfrac{  294y_3 + 105 y_0 - 172y_1 - 217y_2  } {266} \\
B &= \dfrac{  294(5) + 105(0) - 172(1) - 217(2)  } {266} \\
B &= \dfrac{  1470 - 172 - 217(2)  } {266} \\
B &= \dfrac{  1470 - 172 - 434  } {266} = \dfrac{ 864 }{ 266} \\
B &= \dfrac{ 432 } { 133 } \\
B &\doteq 3.2481203
\end{align*}
Now I will try out the formula on the following set of points:
$$( -3, -1), (-2,1), (-1,2), (0,10)$$
We have:
\begin{align*}
y_0 &= -1 \\
y_1 &= 1 \\
y_2 &= 2 \\
y_3 &= 10 \\
C &= 5 \\
A &= \dfrac{ 10y_3 + 9y_0 - 8y_1 - 11y_2 }{38 } \\
A &= \dfrac{ 10(10) + 9(-1) - 8(1) - 11(2) }{38 } \\
A &= \dfrac{ 100 - 9 - 8 - 22 }{38 } \\
A &= \dfrac{ 61}{38} \\
B &\doteq 1.6052632 \\
B &= \dfrac{  294y_3 + 105 y_0 - 172y_1 - 217y_2  } {266} \\
B &= \dfrac{  294(10) + 105(-1) - 172(1) - 217(2)  } {266} \\
B &= \dfrac{  2940 - 105 - 172 - 434 } {266}   \\
B &= \dfrac{ 2229 } {266} \\
B &\doteq 8.3796992
\end{align*}
According to R, the correct value for $A$ is $1.605263$ and the
correct value for $B$ is $8.34210475$.

REPLY [2 votes]: You have obtained
$$A = \dfrac{ 10y_3 + 9y_0 - 8y_1 - 11y_2 }{38 }$$
I have not found any errors in your calculations for finding $A$, so I think that you have correctly got $A$.
There is an error in the step from
$$B=\dfrac{ 18(  10y_3 + 9y_0 - 8y_1 - 11y_2 ) + 19( 6y_3 - 3y_0 - 2y_1 - y_2  ) }{ 19(14)}$$
to
$$B = \dfrac{  180y_3 +162y_0 - 144y_1 - 198y_2
  + 6(19)y_3 - 3(19)y_0 - 28y_1 - 19y_2  } { 19(14)}$$
It should be
$$B = \dfrac{  180y_3 +162y_0 - 144y_1 - 198y_2
  + 6(19)y_3 - 3(19)y_0 - \color{red}{38}y_1 - 19y_2  } { 19(14)}$$
$$B=\frac{15 y_0 - 26 y_1 - 31 y_2 + 42 y_3}{38}$$
For $y_0 = 0,y_1 = 1,y_2 = 2$ and $y_3 = 5$, one has
$$B=\frac{15\times 0 - 26\times 1 - 31\times 2 + 42\times 5}{38}=\frac{61}{19}$$
For $y_0= -1, y_1 = 1, y_2= 2$ and $y_3 = 10$, one has
$$B=\frac{15\times(-1) - 26\times 1 - 31\times 2 + 42\times 10}{38}=\frac{317}{38}$$