Consider the nodes $x_{1}=-1,$ $x_{2}=-0.5,$ $x_{3}=0,$ $x_{4}=0.5$ and $x_{5}=1$ and the function $f\left(x\right)=x^{4}$
The problem can be posed as a minimization problem. We will minimize
where
And obtaining the partial derivatives of $E$ with respect to $a_0,$ $a_1$ and $a_2$ and equating them to zero we would obtain a linear system of three equations with three unknowns that would give us the solution to the problem.
We want to approximate the function using the basis of polynomials
$$ B=\left\{P_0(x),P_1(x),P_2(x)\right\} =\left\{1,x,x^2\right\} $$That is, we want to obtain a polynomial
$$ P(x)=a_{0} \cdot P_0(x) + a_{1} \cdot P_1(x)+ a_{2} \cdot P_2(x)= a_{0} \cdot 1 + a_{1} \cdot x + a_{2} \cdot x^2 $$The base generates the space of the polynomials of degree 2 and we are looking for the polynomial of degree two that best fits the function.
In the discrete case, the most common dot product is
$$ \left\langle g(x),h(x)\right\rangle=\sum_{k=1}^n g(x_k)h(x_k)$$We obtain the coefficients $a_0$, $a_1$ y $a_2$ as a solution of the linear system
$$ \left(\begin{array}{ccc} \left\langle P_0,P_0\right\rangle & \left\langle P_0,P_1\right\rangle & \left\langle P_0,P_2\right\rangle\\ \left\langle P_1,P_0\right\rangle & \left\langle P_1,P_1\right\rangle & \left\langle P_1,P_2\right\rangle\\ \left\langle P_2,P_0\right\rangle & \left\langle P_2,P_1\right\rangle & \left\langle P_2,P_2\right\rangle \end{array}\right)\left(\begin{array}{c} a_{0}\\ a_{1}\\ a_{2} \end{array}\right)=\left(\begin{array}{c} \left\langle P_0,f(x)\right\rangle\\ \left\langle P_1,f(x)\right\rangle\\ \left\langle P_2,f(x)\right\rangle \end{array}\right) $$That for this case is
or
We compute the additions
And substituting them in the system
$$ \left(\begin{array}{ccc} 5 & 0 & 2.5\\ 0 & 2.5 & 0 \\ 2.5 & 0 & 2.125 \end{array}\right)\left(\begin{array}{c} a_{0}\\ a_{1}\\ a_{2} \end{array}\right)=\left(\begin{array}{c} 2.125\\ 0\\ 2.03125 \end{array}\right) $$If we multiply the first row by $-\dfrac{1}{2}$ and we add it to the third one, that is, $e_3 \leftarrow e_3-\dfrac{e_1}{2}$
that is the triangular system
And we solve it starting with the last equation
And since the polynomial of degree two that approximates the points is
substituting the values of $a_0,$ $a_1$ and $a_2$ the polynomial that approximates the points by the least-squares mtehod is
If $x$ is the actual value and $x^*$ is the approximate value:
The relative error is expressed as a fraction of one. If we multiply it by 100 it will be expressed as a percentage.
Thus
which is quite large. But we are approximating a function with only 5 points and a polynomial of degree 2 is not necessarily the best approximation in this case.