Course Webpage

Exercise

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

  • Compute the parable that approximates the function.
  • Approximate the value of the function in $x=0.8$ and calculate the absolute and relative error.

Compute the parable that approximates the function

As an optimization problem

The problem can be posed as a minimization problem. We will minimize

$$E(a_0,a_1,a_2)=r_1^2+r_2^2+r_3^2+r_4^2+r_5^2$$

where

$$r_k = y_k-P(x_k)\quad \mathrm{with}\; P(x)= a_0+a_1x+a_2x^2\quad k=1,\ldots,5$$

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

$$ \left(\begin{array}{ccc} \sum_{k=1}^{5}1\cdot 1 & \sum_{k=1}^{5}1\cdot x_k & \sum_{k=1}^{5}1\cdot x_k^2\\ \sum_{k=1}^{5}x_k\cdot 1 & \sum_{k=1}^{5}x_k\cdot x_k & \sum_{k=1}^{5}x_k\cdot x_k^2\\ \sum_{k=1}^{5}x_k^2\cdot 1 & \sum_{k=1}^{5}x_k^2\cdot x_k & \sum_{k=1}^{5}x_k^2\cdot x_k^2 \end{array}\right)\left(\begin{array}{c} a_{0}\\ a_{1}\\ a_{2} \end{array}\right)=\left(\begin{array}{c} \sum_{k=1}^{5}1 \cdot f(x_k)\\ \sum_{k=1}^{5}x_k \cdot f(x_k)\\ \sum_{k=1}^{5}x_k^2 \cdot f(x_k) \end{array}\right) $$

or

$$ \left(\begin{array}{ccc} \sum_{k=1}^{5}1 & \sum_{k=1}^{5}x_k & \sum_{k=1}^{5}x_k^2\\ \sum_{k=1}^{5}x_k & \sum_{k=1}^{5}x_k^2 & \sum_{k=1}^{5}x_k^3\\ \sum_{k=1}^{5}x_k^2 & \sum_{k=1}^{5}x_k^3 & \sum_{k=1}^{5}x_k^4 \end{array}\right)\left(\begin{array}{c} a_{0}\\ a_{1}\\ a_{2} \end{array}\right)=\left(\begin{array}{c} \sum_{k=1}^{5}1 \cdot x_k^4\\ \sum_{k=1}^{5}x_k \cdot x_k^4\\ \sum_{k=1}^{5}x_k^2 \cdot x_k^4 \end{array}\right) $$

We compute the additions

\begin{array}{|r|rrrrrrr|} \hline k & 1 & x_k & x_k^2 & x_k^3 & x_k^4 & x_k^5 & x_k^6\\ \hline 1 & 1.0 & -1.0 & 1.00 & -1.000 & 1.0000 & -1.00000 & 1.000000\\ 2 & 1.0 & -0.5 & 0.25 & -0.125 & 0.0625 & -0.03125 & 0.015625\\ 3 & 1.0 & 0.0 & 0.00 & 0.000 & 0.0000 & 0.00000 & 0.000000\\ 4 & 1.0 & 0.5 & 0.25 & 0.125 & 0.0625 & 0.03125 & 0.015625\\ 5 & 1.0 & 1.0 & 1.00 & 1.000 & 1.0000 & 1.00000 & 1.000000\\ \hline \sum & 5.0 & 0.0 & 2.50 & 0.000 & 2.1250 & 0.00000 & 2.031250\\ \hline \end{array}

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

$$ \left(\begin{array}{ccc} 5 & 0 & 2.5\\ 0 & 2.5 & 0 \\ 0 & 0 & 0.875 \end{array}\right)\left(\begin{array}{c} a_{0}\\ a_{1}\\ a_{2} \end{array}\right)=\left(\begin{array}{c} 2.125\\ 0\\ 0.96875 \end{array}\right) $$

that is the triangular system

$$\begin{array}{ccccl} 5a_0 & & +2.5a_2 & = & 2.125\\ & 2.5a_1 & & = & 0\\ & & 0.875a_2 & = & 0.96875 \end{array}$$

And we solve it starting with the last equation

$$\begin{array}{l} a_2=\dfrac{0.96875}{0.875}=1.10714\\ a_1 = 0\\ a_0 = \dfrac{2.125-(2.5)(1.10714)}{5}= -0.12856 \end{array}$$

And since the polynomial of degree two that approximates the points is

$$ 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$ the polynomial that approximates the points by the least-squares mtehod is

$$P(x) = -0.12856+1.10714x^2$$

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)= -0.12856+1.10714(0.8)^2=0.58$$

Thus

  • Absolute error $$e_a=|0.4096-0.5800|=0.1704$$
  • Relative error $$e_r=\frac{e_a}{|x|}=\frac{0.1704}{0.4096}=0.41=41\%$$

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.