I want to determine an approximation of a cubic polynomial that has at the points $$x_0=-2, \ x_1=-1, \ x_2=0 , \ x_3=3, \ x_4=3.5$$ the values $$y_0=-33, \ y_1=-20, \ y_2=-20.1, \ y_3=-4.3 , \ y_4=32.5$$ using the least squares method.
So we are looking for a cubic polynomial $p(x)$ such that $$\sum_{i=0}^4\left (p(x_i)-y_i\right )^2$$ is minimal, right?
Let $p(x)=a_3x^3+a_2x^2+a_1x+a_0$. Then we get the following sum:
$$\left (-8a_3+4a_2-2a_1+a_0+33\right )^2+\left (-a_3+a_2-a_1+a_0+20\right )^2+\left (a_0+20.1\right )^2+\left (27a_3+9a_2+3a_1+a_0+4.3\right )^2+\left (42.875a_3+12.25a_2+3.5a_1+a_0-32.5\right )^2$$
Now we want to calculate the values of $a_0, a_1, a_2, a_3$ such that this sum is minimal, right?
How could we do that? Could you give me a hint?
So you want to minimize $S = \sum_{i=0}^4\left (p(x_i)-y_i\right )^2 $ where $p(x) =\sum_{k=0}^3 a_kx^k $.
The parameters you want to find are the $a_k$. You need to differentiate $S$ with respect to each $a_k$ and set that expression equal to zero.
This will give you $4$ equations in the $4$ $a_k$s.
Here is a typical one:
$\begin{array}\\ \dfrac{\partial S}{\partial a_k} &=\dfrac{\partial }{\partial a_k}\sum_{i=0}^4\left( p(x_i)-y_i\right)^2\\ &=\sum_{i=0}^4 \dfrac{\partial }{\partial a_k}\left(p(x_i)-y_i\right)^2\\ &=\sum_{i=0}^4 2\dfrac{\partial }{\partial a_k}(p(x_i)-y_i)(p(x_i)-y_i)\\ &=\sum_{i=0}^4 2\dfrac{\partial }{\partial a_k}(\sum_{j=0}^3 a_jx_i^j)(p(x_i)-y_i)\\ &=\sum_{i=0}^4 2( x_i^k)(\sum_{j=0}^3 a_jx_i^j-y_i)\\ &=2(\sum_{j=0}^3 a_j\sum_{i=0}^4 x_i^{j+k}-\sum_{i=0}^4 x_i^ky_i)\\ \end{array} $
Setting $\dfrac{\partial S}{\partial a_k} = 0$, this gives $\sum_{j=0}^3 a_j\sum_{i=0}^4 x_i^{j+k} =\sum_{i=0}^4 x_i^ky_i $ for $k = 0$ to $3$.
These are the equations that determine the $a_j$.