Runge's Phenomenon and Chebyshev Points
It seems natural that interpolating at more equally spaced points should give a better approximation. For many perfectly smooth functions it does the opposite: the interpolant oscillates wildly near the ends of the interval, and the error grows without bound. This is Runge's phenomenon. The cure is to cluster the points near the ends, using Chebyshev points.
Runge's example
Interpolate on :
import numpy as np
from scipy.interpolate import BarycentricInterpolator
f = lambda x: 1 / (1 + 25 * x**2)
xx = np.linspace(-1, 1, 2001)
print(" n equispaced error Chebyshev error")
for n in (5, 10, 20, 40):
t_eq = np.linspace(-1, 1, n + 1)
t_ch = -np.cos(np.pi * np.arange(n + 1) / n) # Chebyshev points
e_eq = np.max(np.abs(BarycentricInterpolator(t_eq, f(t_eq))(xx) - f(xx)))
e_ch = np.max(np.abs(BarycentricInterpolator(t_ch, f(t_ch))(xx) - f(xx)))
print(f"{n:2d} {e_eq:16.3e} {e_ch:15.3e}") n equispaced error Chebyshev error
5 4.327e-01 6.386e-01
10 1.916e+00 1.322e-01
20 5.982e+01 1.774e-02
40 1.046e+05 3.399e-04
With equally spaced points the error grows exponentially (to about at ); with Chebyshev points it decreases steadily.
Why: the node polynomial
The error formula from Polynomial Interpolation contains . For equally spaced points is far larger near the ends than in the middle. Chebyshev points make as uniform as possible:
import numpy as np
n = 16
xx = np.linspace(-1, 1, 4001)
for name, t in (("equispaced", np.linspace(-1, 1, n + 1)), ("Chebyshev ", -np.cos(np.pi * np.arange(n + 1) / n))):
Phi = np.prod(xx[:, None] - t[None, :], axis=1)
middle = np.max(np.abs(Phi[np.abs(xx) < 0.2]))
print(f"{name}: max |Phi| = {np.max(np.abs(Phi)):.2e}, max near the middle = {middle:.2e}")equispaced: max |Phi| = 9.43e-04, max near the middle = 3.02e-07
Chebyshev : max |Phi| = 3.04e-05, max near the middle = 3.04e-05
Chebyshev points
The Chebyshev points (of the second kind) are the projections onto the -axis of equally spaced points on the unit circle:
They cluster near with density proportional to . For any interval map them linearly. Their barycentric weights have a closed form, (halved at the ends), so evaluation is fast and stable.
Convergence rates
For Chebyshev interpolation:
- If has derivatives (the -th of bounded variation), the error is : algebraic convergence.
- If is analytic in an ellipse around , the error is for some : spectral (geometric) convergence. Runge's function has poles at , which limits to about .
- Entire functions like converge faster than any geometric rate.
import numpy as np
from scipy.interpolate import BarycentricInterpolator
xx = np.linspace(-1, 1, 2001)
funcs = {"exp(x)": np.exp, "|x|^3": lambda x: np.abs(x) ** 3, "Runge": lambda x: 1 / (1 + 25 * x**2)}
for name, f in funcs.items():
errs = []
for n in (8, 16, 32, 64):
t = -np.cos(np.pi * np.arange(n + 1) / n)
errs.append(np.max(np.abs(BarycentricInterpolator(t, f(t))(xx) - f(xx))))
print(f"{name:7s}:", " ".join(f"{e:.1e}" if e > 1e-13 else " <1e-13" for e in errs)) # below 1e-13 is rounding noise
exp(x) : 2.2e-08 <1e-13 <1e-13 <1e-13
|x|^3 : 2.1e-03 2.6e-04 3.3e-05 4.1e-06
Runge : 2.0e-01 3.7e-02 1.6e-03 2.9e-06
In software
numpy.polynomial.chebyshev.chebinterpolate(f, deg)builds a Chebyshev series interpolant;Chebyshev.fitdoes least squares in that basis.- Spectral methods for PDEs, and libraries like Chebfun, rely on Chebyshev interpolation to get 15-digit accuracy with modest .
Exercises
Exercise 1: The points
List the Chebyshev points for .
Answer
.
Solution
for : .
Exercise 2: Map to an interval
Map the Chebyshev points to .
Answer
.
Solution
The affine map sends to .
Exercise 3: Degree needed (code)
Find the smallest for which Chebyshev interpolation of on has maximum error below .
Answer
.
Solution
import numpy as np
from scipy.interpolate import BarycentricInterpolator
xx = np.linspace(-1, 1, 3001); f = lambda x: np.sin(10 * x)
for n in range(5, 80):
t = -np.cos(np.pi * np.arange(n + 1) / n)
if np.max(np.abs(BarycentricInterpolator(t, f(t))(xx) - f(xx))) < 1e-10:
print("n =", n); breakn = 27
Test yourself
Runge's phenomenon is caused by…
- rounding error
- interpolating at equally spaced points with high degree
- using Chebyshev points
- discontinuous functions only
Chebyshev points cluster…
- in the middle
- near the endpoints
- uniformly
- randomly
For an analytic function, Chebyshev interpolation error decreases…
- like
- geometrically, like
- not at all
- like