Course Webpage

Exercise

Given the function $f\left(x\right)=x^{4}$ in $\left[-1,1\right]$ calculate the parable that approximates the function in the interval:

  • Using the polynomial base $\left\{ 1,x,x^{2}\right\}.$
  • Using the orthogonal polynomial basis $\left\{ 1,x,1-3x^{2}\right\}.$
  • Approximate the value of the function in $x=0.8$ and calculate the absolute and relative error.

Polynomial base $\left\{ 1,x,x^{2}\right\} $

As an optimization problem

The problem can be posed as a minimization problem.

A first proposal would be to minimize the area between the two functions, which is

$$ E(a_0,a_1,a_2)=\int_{-1}^1 |P(x)-f(x)|\,dx $$

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

$$ E(a_0,a_1,a_2)=\int_{-1}^1 (P(x)-f(x))^2\,dx =\int_{-1}^1 (a_0+a_1x+a_2x^2-f(x))^2\,dx $$

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.

Approach as a projection 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}$

$$ \left(\begin{array}{ccc} 2 & 0 & 2/3\\ 0 & 2/3 & 0 \\ 0 & 0 & 8/45 \end{array}\right)\left(\begin{array}{c} a_{0}\\ a_{1}\\ a_{2} \end{array}\right)=\left(\begin{array}{c} 2/5\\ 0\\ 16/105 \end{array}\right) $$

that is the triangular system

$$\begin{array}{ccccl} 2a_0 & & +2/3a_2 & = & 2/5\\ & 2/3a_1 & & = & 0\\ & & +(8/45)a_2 & = & 16/105 \end{array}$$

And we solve it starting with the last equation

$$\begin{array}{l} a_2=\dfrac{(16)(45)}{(8)(105)}=\dfrac{6}{7}\approx 0.8571\\ a_1 = 0\\ a_0 = \dfrac{1}{2}\left(\dfrac{2}{5}-\dfrac{2}{3}\dfrac{6}{7}\right)=-\dfrac{3}{35}\approx -0.08571 \end{array}$$

And since the polynomial of degree two that approximates this function in the interval $[-1,1]$ was

$$ 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 $$

substituting the values of $a_0,$ $a_1$ and $a_2.$

$$P(x) = -\frac{3}{35}+\frac{6}{7}x^2$$

Orthogonal polynomial basis $\left\{ 1,x,1-3x^{2}\right\}$.

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

$$ \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) $$

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

$$ \left(\begin{array}{ccc} \left\langle P_0,P_0\right\rangle & 0 & 0\\ 0 & \left\langle P_1,P_1\right\rangle & 0\\ 0 & 0 & \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) $$

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

$$ a_0=\frac{2/5}{2}=\frac{1}{5} \quad a_1=\frac{0}{2/3}=0 \quad a_2=\frac{-16/35}{8/5}=-\frac{2}{7} $$

And since the polynomial was

$$ P(x)= a_{0} \cdot 1 + a_{1} \cdot x + a_{2} \cdot (1-3x^2) $$

the solution is

$$ P(x)=\frac{1}{5} -\frac{2}{7} (1-3x^2) $$

Let's write it in the form of the polynomial of the first section to compare them

$$ P(x)=\frac{1}{5} -\frac{2}{7} +\frac{2}{7}3x^2 =\frac{7}{35} -\frac{10}{35} +\frac{6}{7}x^2= -\frac{3}{35}+\frac{6}{7}x^2 $$

And, indeed, it is the same polynomial as in the previous case because we have solved the same problem in another way.

Approximate the value of the function in $x=0.8$ and calculate the absolute and relative error.

If $x$ is the actual value and $x^*$ is the approximate value:

  • Absolute error $$e_a=|x-x^*|$$
  • Relative error $$e_r=\frac{e_a}{|x|}$$

The relative error is expressed as a fraction of one. If we multiply it by 100 it will be expressed as a percentage.

$$x = f(0.8)=0.8^4 = 0.4096 \quad x^*=P(0.8)=-\frac{3}{35}+\frac{6}{7}0.8^2=0.4629$$

Thus

  • Absolute error $$e_a=|0.4096-0.4629|=0.0533$$
  • Relative error $$e_r=\frac{e_a}{|x|}=\frac{0.053}{0.4096}=0.13=13\%$$