Course Webpage

Piecewise linear interpolation

We have already seen how the Lagrange interpolation polynomial is constructed. How could the result be improved? That is, how to make the error smaller? Adding nodes? The problem is that if we increase the number of nodes, the degree of the interpolation polynomial increases. And high degree polynomials can give very large errors due to large oscillations.

For example, in the following example, we interpolate the function $$f(x) = \frac{1}{1+25x^2}$$ with $11$ equally spaced nodes in the interval $[-1,1]$ and the relative error at points near the extremes of the interval is huge.

For this reason, high degree interpolation polynomials are rarely used. A solution is to use the Chebysev nodes, which are the zeros of the Chebysev polynomial (they are in the interval $[-1,1]$ but with a linear transformation they can be obtained for any closed interval).

The problem is that we cannot always choose the nodes.

The most commonly used solution is piecewise interpolation. The interval is divided into subintervals and in each of them, it is interpolated with a polynomial of degree 1, 2 or 3. In practice the most common are

  • Piecewise linear interpolation: Polynomials of degree one are used in each subinterval. This method has the advantage that it is very simple and requires very few resources.
  • Interpolation with splines: polynomials of degree three are used in each subinterval. This method is more complex but usually gives very good results.

Exercise

For the nodes $x_{0}=-1,$ $x_{1}=1,$ $x_{2}=3$ and $x_{3}=5$ and the function $$f\left(x\right)=\sin\left(\dfrac{\pi}{6}x\right)$$

  • Approximate the value of $f$ in $x=2$ using piecewise linear interpolation.
  • Compute the error bound and compare it with the error.

Approximate the value of $f$ in $x=2$ using piecewise linear interpolation

Piecewise linear interpolation consists of joining the nodes with line segments and approximating the value of the function with its value on these lines.

We want to approximate the function at $x = 2.$ First we have to choose the nodes that we will use to interpolate with a line. We will take the two nodes closest to $x = 2,$ which are $1$ and $3.$ From now on we forget about the other nodes and rename these nodes as

$$x_0 = 1 \quad x_1=3$$

The function values at these nodes are

$$f\left(x\right)=\sin\left(\dfrac{\pi}{6}x\right)\\[0.7cm] y_0=f\left(1\right)=\sin\left(\dfrac{\pi}{6}\right)=\frac{1}{2} \quad y_1=f\left(3\right)=\sin\left(3\dfrac{\pi}{6}\right)=\sin\left(\dfrac{\pi}{2}\right)=1$$

Lagrange's form

We can interpolate using the Lagrange form and then the linear interpolation polynomial on the interval $[1,3]$ is

$$P_1(x)=y_0\,L_0(x)+y_1\,L_1(x)$$
$$P_1(x)=y_0\,\frac{x-x_1}{x_0-x_1}+y_1\,\frac{x-x_0}{x_1-x_0}$$
$$P_1(x)=\frac{1}{2}\,\frac{x-3}{1-3}+(1)\,\frac{x-1}{3-1}$$

And the value in $x=2$ es

$$P_1(2)=\frac{1}{2}\,\frac{2-3}{1-3}+(1)\,\frac{2-1}{3-1}=\frac{1}{2}\,\left(\frac{-1}{-2}\right)+(1)\,\frac{1}{2}=\frac{1}{4}+\frac{1}{2}=\frac{3}{4}=0.75$$

Newton's form

The divided difference table is

$$ \begin{array}{ccc} x & y & \\ 1 & \fbox{$\dfrac{1}{2}$}^{\;\large{c_0}} \\ & & \dfrac{1-\frac{1}{2}}{3-1}=\fbox{$\dfrac{1}{4}$}^{\;\large{c_1}}\\ 3 & 1 & \\ \end{array} $$

And the interpolation polynomial in Newton's form

$$ P_1\left(x\right)= c_0+c_1\left(x-x_{0}\right)$$
$$ P_1\left(x\right)= \frac{1}{2}+\frac{1}{4}\left(x-1\right)$$

And the value in $x=2$ is

$$ P_1\left(2\right)= \frac{1}{2}+\frac{1}{4}\left(2-1\right)=\frac{1}{2}+\frac{1}{4}=\frac{3}{4}=0.75$$

Compute the error bound and compare it with the error

The interpolation error is given by the formula

$$ E(x)=f(x)-P_{n}(x)=f^{(n+1)}(c)\dfrac{(x-x_{0})\ldots(x-x_{n})}{(n+1)!} $$

which is the same as in the Lagrangian interpolation since we only consider the interval $[1,3]$ and we use only two nodes, it is a Lagrangian interpolation. For two nodes, this formula is

$$ E(x)=f(x)-P_{1}(x)=f''(c)\dfrac{(x-x_{0})(x-x_{1})}{2!} $$

The value $c$ is unknown, although we know it is in the interpolation interval, in this case $c\in\left(1,3\right)$ and we have to find a bound for that value. We calculate the second derivative of the function

$$f(x)=\sin\left(\frac{\pi}{6} x\right) \quad f'(x)=\frac{\pi}{6}\,\cos\left(\frac{\pi}{6} x\right) \quad f''(x)=-\frac{\pi^2}{6^2}\,\sin\left(\frac{\pi}{6} x\right)$$

As $c\in(1,3)$ the value $$\dfrac{\pi}{6} c\in \left(\dfrac{\pi}{6}(1) , \dfrac{\pi}{6}(3)\right) = \left(\dfrac{\pi}{6} , \dfrac{\pi}{2}\right) = (30^o,90^o)$$

And since the sine function takes the maximum value for $90^o$ $$\left|\sin\left(\dfrac{\pi}{6}c\right)\right|\lt 1$$

Then $$\left|f''(c)\right|\lt \frac{\pi^2}{6^2}$$

And therefore we can give as error bound

$$ \left|E(2)\right|\lt\frac{\pi^2}{6^2}\dfrac{\left|(2-1)(2-3)\right|}{2!}=0.13707 $$

As $f(2)=\sin\left(\dfrac{\pi}{6}(2)\right)=\sin\left(\dfrac{\pi}{3}\right)=0.86603$

$$\mathrm{Error} = |f(2)-P_1(2)|=|0.86603-0.75000|=0.11603\lt 0.13707$$

We see that

  • The error is much higher than that obtained using the four nodes, but the procedure is also much simpler.
  • We have a good error bound, because it is of the same order of magnitude as the error.

Can we improve this result? Yes, increasing the number of nodes. For example, if instead of 4 equally spaced nodes we use 8 equally spaced nodes the result of the linear interpolation, graphically, will be

Let us now compare, for the same nodes, interpolation with a high degree polynomial and piecewise linear interpolation. We see that, in general, piecewise linear interpolation is better.