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 points with distinct there is exactly one polynomial of degree at most with .
The monomial basis and its trouble
Writing gives the Vandermonde system with . It is simple, but becomes extremely ill-conditioned as grows, because high powers of look alike on an interval.
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 satisfy if and otherwise, so the interpolant can be written down directly:
Barycentric formula
Evaluating the Lagrange form naively costs per point. The barycentric formula rearranges it into an , numerically stable form. With weights :
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, , 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 has continuous derivatives, for each there is a in the span of the points with
The error depends on the smoothness of and on the node polynomial , which is where the choice of points enters (Runge's Phenomenon and Chebyshev Points).
In software
scipy.interpolate.BarycentricInterpolatorandKroghInterpolator;np.polyfit(t, y, n)with as many points as coefficients interpolates via the monomial basis (fine for small 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 .
Answer
.
Solution
.
Exercise 2: Error bound
Bound the error of the linear interpolant of on using the endpoints.
Answer
About .
Solution
The error is . On , , and is largest at , where it is . So the error is at most .
Exercise 3: Divided differences (code)
Compute the Newton coefficients for the points of the lesson (, ) with a divided-difference table and evaluate the polynomial at .
Answer
Coefficients ; , the same value the barycentric form gave.
Solution
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 ?
Why avoid solving the Vandermonde system for large ?
- It has no solution
- It is extremely ill-conditioned
- It costs
- It only works for equally spaced points
The barycentric formula evaluates the interpolant in…
- per point after setup
- per point always