Skip to article frontmatterSkip to article content

6.2Interpolation using polynomials

#%config InlineBackend.figure_format = 'svg'
from pylab import *
from scipy.interpolate import barycentric_interpolate

Polynomials are easy to evluate since they require only basic arithmetic operations: ,+,÷-,+,\div. They can provide a good approximation to continuous functions as shown by Weirstrass Theorem.

6.2.1Interpolation using monomials

Given N+1N+1 data

(xi,yi),i=0,1,,Nwithyi=f(xi)(x_i, y_i), \quad i=0,1,\ldots,N \qquad \textrm{with} \qquad y_i = f(x_i)

we can try to find a polynomial of degree NN

p(x)=a0+a1x++aNxNp(x) = a_0 + a_1 x + \ldots + a_N x^N

that satisfies the interpolation conditions

p(xi)=yi,i=0,1,,Np(x_i) = y_i, \qquad i=0,1,\ldots, N

This is a linear system of N+1N+1 equations that can be written in matrix form

[1x0x02x0N1x1x12x1N1xNxN2xNN]V[a0a1aN]=[y0y1yN]\underbrace{\begin{bmatrix} 1 & x_0 & x_0^2 & \ldots & x_0^N \\ 1 & x_1 & x_1^2 & \ldots & x_1^N \\ \vdots & \vdots & \ldots & \vdots \\ 1 & x_N & x_N^2 & \ldots & x_N^N \end{bmatrix}}_{V} \begin{bmatrix} a_0 \\ a_1 \\ \vdots \\ a_N \end{bmatrix} = \begin{bmatrix} y_0 \\ y_1 \\ \vdots \\ y_N \end{bmatrix}

This has a unique solution if the Vandermonde matrix VV is non-singular. Since

detV=j=0Nk=j+1N(xkxj)\det V = \prod_{j=0}^N \prod_{k=j+1}^N (x_k - x_j)

we can solve the problem provided the points {xi}\{ x_i \} are distinct.

VV is non-singular. We can show this without computing the determinant. Assume that the {xi}\{ x_i \} are distinct. It is enough to show that the only solution of Va=0Va=0 is a=0a=0. Note that the set of N+1N+1 equations Va=0Va=0 is of the form

p(xi)=0,i=0,1,,Np(x_i) = 0, \qquad i=0,1,\ldots,N

which implies that p(x)p(x) has N+1N+1 distinct roots. But since pp is a polynomial of degree NN, this implies that p(x)0p(x) \equiv 0 and hence each ai=0a_i = 0; the matrix VV is non-singular.

6.2.1.1Condition number of VV

If we want to solve the interpolation problem by solving the matrix problem, then the condition number of the matrix becomes important. The condition number of a square matrix wrt some matrix norm is defined as

κ(A)=AA1\kappa(A) = \norm{A} \cdot \norm{A^{-1}}

Matrices with large condition numbers cannot be solved accurately on a computer by Gaussian elimination due to possible growth of round-off errors.

6.2.2Interpolating polynomials

6.2.3Lagrange interpolation

Lagrange interpolation provides the solution without having to solve a matrix problem. Define

πi(x)=(xx0)(xxi1)(xxi+1)(xxN)\pi_i(x) = (x-x_0) \ldots (x-x_{i-1})(x-x_{i+1})\ldots (x-x_N)

and

i(x)=πi(x)πi(xi)\ell_i(x) = \frac{\pi_i(x)}{\pi_i(x_i)}

Note that

  • each i\ell_i is a polynomial of degree NN, and
  • i(xj)=δij\ell_i(x_j) = \delta_{ij}, i.e.,
i(xi)=1andi(xj)=0,ji\ell_i(x_i) = 1 \qquad \textrm{and} \qquad \ell_i(x_j) = 0, \quad j \ne i

Then consider the polynomial of degree NN given by

pN(x)=j=0Nyjj(x)p_N(x) = \sum_{j=0}^N y_j \ell_j(x)

By construction this satisfies

pN(xi)=yi,i=0,1,,Np_N(x_i) = y_i, \qquad i=0,1,\ldots,N

and hence is the solution to the interpolation problem.

6.2.4Error estimate

6.2.5Uniformly spaced points

Let us specialize the error estimate to the case of uniformly spaced points in the interval [a,b][a,b] with

xi=a+ih,0iN,h=baNx_i = a + i h, \qquad 0 \le i \le N, \qquad h = \frac{b-a}{N}

We have x0=ax_0 = a and xN=bx_N = b.

6.2.6Difficulty of polynomial interpolation

Do the polynomial approximations pNp_N converge to the true function ff as NN \to \infty ? The error formula seems to suggest so, due to the factor 1(N+1)!\frac{1}{(N+1)!} provided

  • higher order derivatives of ff are small
  • the function ωN(x)\omega_N(x) which depends on point distribution is small

6.2.6.1Size of derivatives

On uniformly spaced points, we have seen the interpolants of cos(x)\cos(x) converge but those of the rational function 11+16x2\frac{1}{1+16x^2} do not. This must be related to the behaviour of the derivatives of these functions.

This is the general situation; for most functions, some higher order derivatives tend to grow as n!n!. Even for a polynomial pN(x)p_N(x), the derivatives grow in size until the NN’th one, which is aNN!a_N N!, after which they suddenly all become zero.

6.2.6.2Polynomial factor in error bound

The error of polynomial interpolation is given by

f(x)pN(x)=ωN(x)(N+1)!f(N+1)(ξ)f(x) - p_N(x) = \frac{\omega_N(x)}{(N+1)!} f^{(N+1)}(\xi)

where ξI(x0,x1,,xN,x)\xi \in I(x_0,x_1,\ldots,x_N,x).

Case N=1N=1. In case of linear interpolation

ω1(x)=(xx0)(xx1),h=x1x0\omega_1(x) = (x-x_0)(x-x_1), \qquad h = x_1 - x_0

Then

maxx0xx1ω1(x)=h24\max_{x_0 \le x \le x_1} |\omega_1(x)| = \frac{h^2}{4}

and the interpolation error bound is

maxx0xx1f(x)p1(x)h28maxx0xx1f(x)\max_{x_0 \le x \le x_1} |f(x) - p_1(x)| \le \frac{h^2}{8} \max_{x_0 \le x \le x_1}| f''(x)|

Case N=2N=2. To bound ω2(x)\omega_2(x) on [x0,x2][x_0,x_2], shift the origin to x1x_1 so that

ω2(x)=(xh)x(x+h)\omega_2(x) = (x-h)x(x+h)

Near the center of the data

maxx112hxx1+12hω2(x)=0.375h3\max_{x_1-\half h \le x \le x_1 + \half h} |\omega_2(x)| = 0.375 h^3

whereas on the whole interval

maxx0xx2ω2(x)=239h30.385h3\max_{x_0 \le x \le x_2} |\omega_2(x)| = \frac{2\sqrt{3}}{9}h^3 \approx 0.385 h^3

In this case, the error is of similar magnitude whether xx is near the center or near the edge.

Case N=3N=3. We can shift the nodes so that they are symmetric about the origin. Then

ω3(x)=(x294h2)(x214h2)\omega_3(x) = \left( x^2 - \frac{9}{4}h^2 \right) \left( x^2 - \frac{1}{4}h^2 \right)

and

maxx1xx2ω3(x)=916h40.56h4maxx0xx3ω3(x)=h4\begin{align} \max_{x_1 \le x \le x_2}|\omega_3(x)| &= \frac{9}{16}h^4 \approx 0.56 h^4 \\ \max_{x_0 \le x \le x_3}|\omega_3(x)| &= h^4 \end{align}

In this case, the error near the endpoints can be twice as large as the error near the middle.

Case N=6N = 6. The behaviour exhibited for N=3N=3 is accentuated for larger degree. For N=6N=6,

maxx2xx4ω6(x)12.36h7,maxx0xx6ω6(x)95.8h7\max_{x_2 \le x \le x_4}|\omega_6(x)| \approx 12.36 h^7, \qquad \max_{x_0 \le x \le x_6}|\omega_6(x)| \approx 95.8 h^7

and the error near the ends can be almost 8 times that near the center.

The next functions evaluate ωN(x)\omega_N(x) and plot it.

# x  = (x0,x1,x2,...,xN)
# xp = array of points where to evaluate
def omega(x,xp):
    fp = ones_like(xp)
    for xi in x:
        fp = fp * (xp - xi)
    return fp

def plot_omega(x):
    M  = 1000
    xx = linspace(-1.0,1.0,M)
    f = omega(x, xx)
    plot(xx,f,'b-',x,0*x,'o')
    title("N = "+str(N)), grid(True);

6.2.7Distribution of data points

The error in polynomial interpolation is

f(x)pN(x)ωN(x)(N+1)!maxξ[a,b]f(N+1)(ξ)|f(x) - p_N(x)| \le \frac{|\omega_N(x)|}{(N+1)!} \max_{\xi \in [a,b]} |f^{(N+1)}(\xi)|

For a given function f(x)f(x), we cannot do anything about the derivative term in the error estimate. For uniformly spaced data points, ωN\omega_N has large value near the end points which is also where the Runge phenomenon is observed. But we can try to minimize the magnitude of ωN(x)\omega_N(x) by choosing a different set of nodes for interpolation. For the following discussion, let us assume that the xix_i are ordered and contained in the interval [1,+1][-1,+1].

We will show that

min{xi}maxx[1,+1]ωN(x)=2N\min_{\{x_i\}} \max_{x \in [-1,+1]} |\omega_N(x)| = 2^{-N}

and the minimum is achieved for following set of nodes

xi=cos(2(Ni)+12N+2π),i=0,1,,Nx_i = \cos\left( \frac{2(N-i)+1}{2N+2} \pi \right), \qquad i=0,1,\ldots,N

These are called Chebyshev points of first kind.

6.2.8Chebyshev polynomials

The Chebyshev polynomials are defined on the interval [1,+1][-1,+1] and the first few polynomials are

T0(x)=1T1(x)=x\begin{aligned} T_0(x) &= 1 \\ T_1(x) &= x \end{aligned}

The remaining polynomials can be defined by recursion

Tn+1(x)=2xTn(x)Tn1(x)T_{n+1}(x) = 2 x T_n(x) - T_{n-1}(x)

so that

T2(x)=2x21T3(x)=4x33xT4(x)=8x48x2+1\begin{aligned} T_2(x) &= 2x^2 - 1 \\ T_3(x) &= 4x^3 - 3x \\ T_4(x) &= 8x^4 - 8x^2 + 1 \end{aligned}

In Python, we can use function recursion to compute the Chebyshev polynomials.

def chebyshev(n, x):
    if n == 0:
        y = ones_like(x)
    elif n == 1:
        y = x.copy()
    else:
        y = 2 * x * chebyshev(n-1,x) - chebyshev(n-2,x)
    return y

The first few of these are shown below.

Source
N = 200
x = linspace(-1.0,1.0,N)
figure(figsize=(8,6))
for n in range(0,6):
    y = chebyshev(n,x)
    plot(x,y,label='n='+str(n))
legend(), grid(True), xlabel('x'), ylabel('$T_n(x)$');
<Figure size 800x600 with 1 Axes>

We can write x[1,+1]x \in [-1,+1] in terms of an angle θ[0,π]\theta \in [0,\pi]

x=cosθx=\cos\theta

Properties of Chebyshev polynomials

  • Tn(x)T_n(x) is a polynomial of degree nn.

  • Tn(cosθ)=cos(nθ)T_n(\cos\theta) = \cos(n\theta). (Fourier cosine series)

  • Tn(x)=cos(ncos1(x))T_n(x) = \cos(n\cos^{-1}(x))

  • Tn(x)1|T_n(x)| \le 1

  • Extrema

    Tn(cos(jπn))=(1)j,0jnT_n\left(\cos\left(\frac{j\pi}{n}\right)\right) = (-1)^j, \qquad 0 \le j \le n
  • Roots

    Tn(cos(2j+12nπ))=0,0jn1T_n\left( \cos \left(\frac{2j+1}{2n}\pi\right) \right) = 0, \qquad 0 \le j \le n-1

6.2.9Optimal nodes

Since ωN(x)\omega_N(x) is a monic polynomial of degree N+1N+1, we know from previous theorem that

max1x+1ωN(x)2N\max_{-1 \le x \le +1} |\omega_N(x)| \ge 2^{-N}

Question. Can we choose the xix_i so that the minimum value of 2N2^{-N} is achieved ?

Answer. If {xi}\{x_i\} are the N+1N+1 distinct roots of TN+1(x)T_{N+1}(x), then

ωN(x)=2NTN+1(x)\omega_N(x) = 2^{-N} T_{N+1}(x)

so that

max1x+1ωN(x)=2Nmax1x+1TN+1(x)=2N\max_{-1 \le x \le +1} |\omega_N(x)| = 2^{-N} \max_{-1 \le x \le +1} |T_{N+1}(x)| = 2^{-N}

Thus the optimal nodes are Chebyshev points of first kind

xi=cos(2i+12N+2π),0iNx_i = \cos\left( \frac{2i+1}{2N+2} \pi \right), \qquad 0 \le i \le N

In terms of θ variable, the spacing between these nodes

θi+1θi=πN+1\theta_{i+1} - \theta_i = \frac{\pi}{N+1}

is constant. With these points, we have

12NωN(x)12N,x[1,1]-\frac{1}{2^{N}} \le \omega_N(x) \le \frac{1}{2^{N}}, \qquad x \in [-1,1]