TExcelTAD
Home › Numerical Methods › Polynomial Interpolation

Polynomial Interpolation

Interpolation constructs a function that passes exactly through given data points, so that values in between can be estimated, or so that a complicated function can be replaced by a simple one that is easy to differentiate and integrate. Polynomials are the classical choice, and the way the polynomial is represented matters a great deal numerically.

Existence and uniqueness

Through n+1n + 1 points (t0,y0),…,(tn,yn)(t_0, y_0), \ldots, (t_n, y_n) with distinct tit_i there is exactly one polynomial pp of degree at most nn with p(ti)=yip(t_i) = y_i.

The monomial basis and its trouble

Writing p(t)=c0+c1t+⋯+cntnp(t) = c_0 + c_1t + \cdots + c_nt^n gives the Vandermonde system Vc=yV\mathbf{c} = \mathbf{y} with Vij=tijV_{ij} = t_i^{j}. It is simple, but VV becomes extremely ill-conditioned as nn grows, because high powers of tt look alike on an interval.

python · try it yourself
import numpy as np

for n in (5, 10, 20, 30):
    t = np.linspace(0, 1, n + 1)
    print(f"n = {n:2d}: cond(Vandermonde) = {np.linalg.cond(np.vander(t)):.1e}")
n =  5: cond(Vandermonde) = 4.9e+03
n = 10: cond(Vandermonde) = 1.2e+08
n = 20: cond(Vandermonde) = 1.1e+17
n = 30: cond(Vandermonde) = 1.7e+19

The Lagrange form

The Lagrange basis polynomials ℓj(t)=∏k≠jt−tktj−tk\ell_j(t) = \prod_{k \ne j}\frac{t - t_k}{t_j - t_k} satisfy ℓj(ti)=1\ell_j(t_i) = 1 if i=ji = j and 00 otherwise, so the interpolant can be written down directly:

p(t)=∑j=0nyj ℓj(t).p(t) = \sum_{j=0}^{n}y_j\,\ell_j(t).

Barycentric formula

Evaluating the Lagrange form naively costs O(n2)O(n^2) per point. The barycentric formula rearranges it into an O(n)O(n), numerically stable form. With weights wj=1∏k≠j(tj−tk)w_j = \frac{1}{\prod_{k \ne j}(t_j - t_k)}:

p(t)=∑j=0nwj yjt−tj∑j=0nwjt−tj.p(t) = \frac{\displaystyle\sum_{j=0}^{n}\frac{w_j\,y_j}{t - t_j}}{\displaystyle\sum_{j=0}^{n}\frac{w_j}{t - t_j}}.
python · try it yourself
import numpy as np
from scipy.interpolate import BarycentricInterpolator

def bary_weights(t):
    n = len(t)
    w = np.ones(n)
    for j in range(n):
        w[j] = 1 / np.prod(t[j] - np.delete(t, j))
    return w

def bary_eval(t, y, w, x):
    x = np.atleast_1d(x).astype(float)
    p = np.empty_like(x)
    for i, xi in enumerate(x):
        d = xi - t
        if np.any(d == 0):
            p[i] = y[np.argmax(d == 0)]
        else:
            p[i] = np.sum(w * y / d) / np.sum(w / d)
    return p

t = np.array([0.0, 1, 2, 4])
y = np.array([1.0, 3, 2, 5])
w = bary_weights(t)
x = np.array([0.5, 1.5, 3.0])
print("barycentric:", bary_eval(t, y, w, x))
print("SciPy      :", BarycentricInterpolator(t, y)(x))
print("polyfit    :", np.polyval(np.polyfit(t, y, 3), x))
barycentric: [2.59375 2.65625 1.5    ]
SciPy      : [2.59375 2.65625 1.5    ]
polyfit    : [2.59375 2.65625 1.5    ]

Newton's divided differences

A third representation builds the polynomial incrementally, p(t)=a0+a1(t−t0)+a2(t−t0)(t−t1)+⋯p(t) = a_0 + a_1(t - t_0) + a_2(t - t_0)(t - t_1) + \cdots, with coefficients from a table of divided differences. Adding a point adds one term, which is convenient for hand computation and for deriving finite-difference formulas.

Interpolation error

If ff has n+1n + 1 continuous derivatives, for each xx there is a ξ\xi in the span of the points with

f(x)−p(x)=f(n+1)(ξ)(n+1)!∏i=0n(x−ti).f(x) - p(x) = \frac{f^{(n+1)}(\xi)}{(n + 1)!}\prod_{i=0}^{n}(x - t_i).

The error depends on the smoothness of ff and on the node polynomial ∏(x−ti)\prod(x - t_i), which is where the choice of points enters (Runge's Phenomenon and Chebyshev Points).

In software

  • scipy.interpolate.BarycentricInterpolator and KroghInterpolator; np.polyfit(t, y, n) with as many points as coefficients interpolates via the monomial basis (fine for small nn only).
  • The Chebfun system (MATLAB) and its Python ports represent functions by barycentric interpolants in Chebyshev points with hundreds or thousands of terms.

Exercises

Exercise 1: Lagrange by hand

Find the quadratic through (0,1),(1,0),(2,3)(0, 1), (1, 0), (2, 3).

Answer

p(t)=2t2−3t+1p(t) = 2t^2 - 3t + 1.

Solution

p=1⋅(t−1)(t−2)2+0+3⋅t(t−1)2=t2−3t+2+3t2−3t2=2t2−3t+1p = 1\cdot\frac{(t - 1)(t - 2)}{2} + 0 + 3\cdot\frac{t(t - 1)}{2} = \frac{t^2 - 3t + 2 + 3t^2 - 3t}{2} = 2t^2 - 3t + 1.

Exercise 2: Error bound

Bound the error of the linear interpolant of sin⁡x\sin x on [0,0.1][0, 0.1] using the endpoints.

Answer

About 1.25×10−41.25 \times 10^{-4}.

Solution

The error is ∣f′′(ξ)∣2∣x(x−0.1)∣\frac{\lvert f''(\xi)\rvert}{2}\lvert x(x - 0.1)\rvert. On [0,0.1][0, 0.1], ∣f′′∣=∣sin⁡ξ∣≤sin⁡0.1≈0.0998\lvert f''\rvert = \lvert\sin\xi\rvert \le \sin 0.1 \approx 0.0998, and ∣x(x−0.1)∣\lvert x(x - 0.1)\rvert is largest at x=0.05x = 0.05, where it is 0.00250.0025. So the error is at most 0.09982⋅0.0025≈1.25×10−4\frac{0.0998}{2} \cdot 0.0025 \approx 1.25 \times 10^{-4}.

Exercise 3: Divided differences (code)

Compute the Newton coefficients for the points of the lesson (t=0,1,2,4t = 0, 1, 2, 4, y=1,3,2,5y = 1, 3, 2, 5) with a divided-difference table and evaluate the polynomial at t=3t = 3.

Answer

Coefficients 1,2,−1.5,712≈0.58331, 2, -1.5, \frac{7}{12} \approx 0.5833; p(3)=1.5p(3) = 1.5, the same value the barycentric form gave.

Solution
python · try it yourself
import numpy as np
t = np.array([0.0, 1, 2, 4]); a = np.array([1.0, 3, 2, 5])
n = len(t)
for k in range(1, n):
    a[k:] = (a[k:] - a[k - 1:-1]) / (t[k:] - t[:n - k])
print("coefficients:", a)
x = 3.0; p = a[-1]
for k in range(n - 2, -1, -1):
    p = a[k] + (x - t[k]) * p
print("p(3) =", p)
coefficients: [ 1.          2.         -1.5         0.58333333]
p(3) = 1.5000000000000002

Test yourself

How many points determine a unique interpolating polynomial of degree at most nn?

  1. nn
  2. n+1n + 1
  3. 2n2n
  4. n−1n - 1

Why avoid solving the Vandermonde system for large nn?

  1. It has no solution
  2. It is extremely ill-conditioned
  3. It costs O(n)O(n)
  4. It only works for equally spaced points

The barycentric formula evaluates the interpolant in…

  1. O(n3)O(n^3)
  2. O(n)O(n) per point after O(n2)O(n^2) setup
  3. O(1)O(1)
  4. O(n2)O(n^2) per point always