Given the function $f\left(x\right)=x^{4}$ in $\left[-1,1\right]$ calculate the parable that approximates the function in the interval:
The problem can be posed as a minimization problem.
A first proposal would be to minimize the area between the two functions, which is
obtaining the partial derivatives of $E$ with respect to $a_0,$ $a_1$ and $a_2.$ But the problem is that the absolute value function is not differentiable at all points.
As in the discrete case, a simpler solution is to minimize the quadratic error, which is
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 continuous functions case, the most common dot product is
$$ \left\langle g(x),h(x)\right\rangle=\int_{-1}^ 1 g(x)h(x)dx$$We obtain the coefficients $a_0$, $a_1$ and $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) $$Substituting in the dot product with the appropriate functions
$$ \left(\begin{array}{ccc} \int_{-1}^{1}1\cdot 1 \, dx & \int_{-1}^{1}1\cdot x \,dx & \int_{-1}^{1}1\cdot x^2 \,dx\\ \int_{-1}^{1}x\cdot 1 \,dx & \int_{-1}^{1}x\cdot x \,dx & \int_{-1}^{1}x\cdot x^2 \,dx\\ \int_{-1}^{1}x^2\cdot 1 \,dx & \int_{-1}^{1}x^2\cdot x \,dx & \int_{-1}^{1}x^2\cdot x^2 \,dx \end{array}\right)\left(\begin{array}{c} a_{0}\\ a_{1}\\ a_{2} \end{array}\right)=\left(\begin{array}{c} \int_{-1}^{1}1 \cdot f(x)\,dx\\ \int_{-1}^{1}x \cdot f(x)\,dx\\ \int_{-1}^{1}x^2 \cdot f(x)\,dx \end{array}\right) $$And
$$ \left(\begin{array}{ccc} \int_{-1}^{1}1 dx & \int_{-1}^{1}x dx & \int_{-1}^{1}x^2 dx\\ \int_{-1}^{1}x dx & \int_{-1}^{1}x^2 dx & \int_{-1}^{1}x^3 dx\\ \int_{-1}^{1}x^2 dx & \int_{-1}^{1}x^3 dx & \int_{-1}^{1}x^4 dx \end{array}\right)\left(\begin{array}{c} a_{0}\\ a_{1}\\ a_{2} \end{array}\right)=\left(\begin{array}{c} \int_{-1}^{1}\,x^4dx\\ \int_{-1}^{1}x \,x^4dx\\ \int_{-1}^{1}x^2 \,x^4dx \end{array}\right) $$Computing the integrals
$$ \left(\begin{array}{ccc} 2 & 0 & 2/3\\ 0 & 2/3 & 0 \\ 2/3 & 0 & 2/5 \end{array}\right)\left(\begin{array}{c} a_{0}\\ a_{1}\\ a_{2} \end{array}\right)=\left(\begin{array}{c} 2/5\\ 0\\ 2/7 \end{array}\right) $$If we multiply the first row by $-\dfrac{1}{3}$ and we add it to the third one, that is, $e_3 \leftarrow e_3-\dfrac{e_1}{3}$
that is the triangular system
And we solve it starting with the last equation
And since the polynomial of degree two that approximates this function in the interval $[-1,1]$ was
substituting the values of $a_0,$ $a_1$ and $a_2.$
The system proposed in the previous section, in general, is ill-conditioned (small errors in the data can produce large errors in the results) so it is advisable to look for an alternative solution that could be to use a basis of orthogonal polynomials. That is, polynomials, which for the given scalar product are orthogonal
$$\left\langle P_i,P_j\right\rangle=0\quad \mathrm{si} \quad i\ne j$$We will approximate the function using the basis of polynomial functions
$$ B=\left\{P_0(x),P_1(x),P_2(x)\right\} =\left\{1,x,1-3x^2\right\} $$That is, we are looking for 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 (1-3x^2) $$We obtain the coefficients $a_0$, $a_1$ and $a_2$ as a solution of the linear system
Since in this case the base of polynomials is orthogonal, the scalar product between two polynomials of the base is zero and the system is
And the solution of this diagonal system is
$$ a_0=\frac{\left\langle P_0,f(x)\right\rangle}{\left\langle P_0,P_0\right\rangle} \quad a_1=\frac{\left\langle P_1,f(x)\right\rangle}{\left\langle P_1,P_1\right\rangle} \quad a_2=\frac{\left\langle P_2,f(x)\right\rangle}{\left\langle P_2,P_2\right\rangle} $$Using the dot product
$$ \left\langle g(x),h(x)\right\rangle=\int_{-1}^ 1 g(x)h(x)dx $$we have
$$ a_0=\frac{\int_{-1}^1 P_0\,f(x)\,dx}{\int_{-1}^1 P_0\,P_0\,dx} \quad a_1=\frac{\int_{-1}^1 P_1\,f(x)\,dx}{\int_{-1}^1 P_1\,P_1\,dx} \quad a_2=\frac{\int_{-1}^1 P_2\,f(x)\,dx}{\int_{-1}^1 P_2\,P_2\,dx} $$That is
$$ a_0=\frac{\int_{-1}^1 x^4\,dx}{\int_{-1}^1 1\,dx} \quad a_1=\frac{\int_{-1}^1 x^5\,dx}{\int_{-1}^1 x^2\,dx} \quad a_2=\frac{\int_{-1}^1 (1-3x^2)\,x^4\,dx}{\int_{-1}^1 (1-3x^2)^2\,dx} $$Calculating these integrals and substituting their value, we have
And since the polynomial was
the solution is
Let's write it in the form of the polynomial of the first section to compare them
And, indeed, it is the same polynomial as in the previous case because we have solved the same problem in another way.
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