Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Polynomial interpolation

ProblemFind the polynomial of degree through points , or approximate a function on an interval by a polynomial
Devised byNewton’s divided differences (1687, 1711); Lagrange’s form (Waring 1779, Lagrange 1795); the barycentric form (Jacobi 1825, Taylor 1945, Salzer 1972; popularised by Berrut and Trefethen 2004); Chebyshev polynomials (Pafnuty Chebyshev, 1854); the Runge phenomenon (Carl Runge, 1901)
AccuracyThrough Chebyshev points: geometric convergence for analytic . Through equally spaced points: may diverge
CostNewton: set-up, per evaluation. Barycentric: weights, per evaluation. Chebyshev series: fit, Clenshaw evaluation
Read firstPreliminaries: floating-point arithmetic
APIPolynomialInterpolator, LagrangeInterpolant, ChebyshevApproximation

Overview

Two points determine a straight line, and three points (not on a line) determine a parabola. The pattern continues: given points with distinct abscissas , there is exactly one polynomial of degree at most passing through all of them. It is called the interpolating polynomial, or interpolant. Finding it, and evaluating it between the points, is the oldest problem in numerical analysis, and the foundation of numerical integration, differentiation and much of approximation theory.

Although the interpolant is unique, it can be written down in several ways, and the ways differ in cost and in numerical stability. The library offers three.

  • Newton’s divided differences build the polynomial one point at a time and evaluate it by Horner’s rule.
  • The barycentric Lagrange formula is the numerically preferred form for evaluating the interpolant at many points.
  • A Chebyshev series approximates a function you can evaluate anywhere, not just at given data, to near machine precision with a few dozen terms.

The single most important fact about polynomial interpolation is that the choice of points matters enormously. Equally spaced points can make the interpolant diverge wildly as grows, the Runge phenomenon, while Chebyshev points converge for every Lipschitz-continuous function, and geometrically fast for analytic ones. How fast depends on how far the nearest singularity of the function lies from the interval in the complex plane, and a function that is only a few times differentiable converges only algebraically. This entry first describes the two ways of computing the interpolant, then explains where the error comes from and why the points matter so much.

Where it is used

Polynomial interpolation is the starting point of numerical analysis.

  • Derivation of other methods. Newton-Cotes quadrature, finite-difference formulas and multistep ODE methods all come from integrating or differentiating an interpolating polynomial.
  • Function approximation. Chebyshev interpolation approximates functions on an interval to near machine precision, as in function libraries and the chebfun approach.
  • A caution. Equally spaced points with high degree produce the Runge phenomenon, which is why splines and Chebyshev nodes exist.

History

Interpolation tables were the computers of earlier centuries: astronomical, navigational and mathematical tables listed values at regular steps, and users interpolated between them. Isaac Newton’s divided-difference formula (in the Principia, 1687, Book III, Lemma V, and Methodus differentialis, 1711) remains the standard way to build an interpolant incrementally. The form now named after Joseph-Louis Lagrange (1795) had been published by Edward Waring in 1779 and was known to Euler.

The barycentric form, a rearrangement of Lagrange’s formula that is both cheaper and more stable, appears in Jacobi’s 1825 thesis and was rediscovered several times during the twentieth century; Jean- Paul Berrut and Nick Trefethen’s 2004 survey “Barycentric Lagrange interpolation” made it the modern standard.

In 1901 Carl Runge showed that interpolating the innocent function at equally spaced points on gives polynomials that diverge near the ends of the interval. The cure lies in the polynomials introduced by Pafnuty Chebyshev in 1854 in his study of steam-engine linkages: they oscillate equally between and , and their extrema cluster towards the ends of the interval exactly as interpolation points should.

The idea

Newton’s divided differences

Newton’s idea is to write the interpolant so that each new data point adds one new term and leaves the earlier terms alone:

The term with coefficient contains the factors , so it vanishes at . This lets us find the coefficients one at a time.

  • At every term but the first vanishes, so , and we need .
  • At only two terms survive: , so . This is the slope of the line through the first two points.
  • At three terms survive, giving one equation for , and so on.

Solving these equations in turn produces a pattern. The coefficients are the divided differences , defined recursively by and

A divided difference over points is the difference of two divided differences over points, divided by the distance between the outermost points. They are usually laid out in a triangular table. For the three points , and :

01
13
313

The coefficients are the top entries of the columns, , , , so

and indeed , and .

The table has entries, so it costs operations; the library computes it in place, overwriting the values. To evaluate , nest the products so that each factor is multiplied in only once:

This is Horner’s rule, and it costs operations per evaluation.

The barycentric formula

Lagrange wrote the same polynomial in a different way. For each node , build a polynomial that equals 1 at and 0 at every other node:

The numerator vanishes at every node except , and the denominator scales the value at to 1. Then passes through all the data: at every term vanishes except the -th, which contributes . This is Lagrange’s formula. As written it costs per evaluation, because each of the products has factors. A little algebra removes the repetition.

Step 1: separate what depends on from what does not. Let be the product over all nodes, and let . The numerator of is with the factor removed, so

Step 2: eliminate . Apply the same formula to the data for all . The interpolant of constant data is the constant 1, so

Step 3: divide. Dividing the expression for by this expression for 1 cancels and gives the barycentric formula

The formula is a weighted average of the data values, with weights that grow as approaches . The barycentric weights depend only on the nodes and are computed once, in operations; each evaluation is then . The formula is also remarkably stable. Any error in a weight appears in both the numerator and the denominator and largely cancels, and Higham (2004) proved the formula is forward stable for well-chosen points. The one special case is exactly, where the formula would divide by zero; the code returns directly.

The interpolation error and the Runge phenomenon

So far the data were just numbers. Now suppose they are samples of a function , and ask how far the interpolant is from between the nodes.

Interpolation error. If has continuous derivatives, then for each there is a in the interval spanned by and the nodes with

The formula resembles the remainder of a Taylor series, and for good reason: a Taylor polynomial is the limit of an interpolant as all the nodes merge into one. The error is a product of two factors.

  • The first, , depends on . It is small when the high derivatives of grow more slowly than .
  • The second, the node polynomial , depends only on the points. It is zero at each node, as it must be, and its size between the nodes is something we can control by choosing the nodes.

For equally spaced points the node polynomial is far larger near the ends of the interval than in the middle, by a factor that grows exponentially with . Near the middle, is close to many nodes and the product has many small factors; near an end, most nodes are far away. If the derivatives of also grow fast, the error near the ends grows without bound as increases. Runge’s example is on . It looks harmless on the real line, but it has poles at in the complex plane, close to the interval, and these make its derivatives grow very quickly. The interpolant diverges near the ends:

Figure 3.2. The Runge phenomenon.

Adding more equally spaced points makes the oscillations worse, not better.

Chebyshev points

The cure is to choose nodes that make the node polynomial small everywhere. Take points equally spaced in angle around the upper half of the unit circle, and project them down onto the real axis. These are the Chebyshev points

They cluster near the ends of , with density proportional to . The extra nodes near the ends are exactly what is needed to tame the node polynomial there. With these points the node polynomial is a close relative of a Chebyshev polynomial. It oscillates with peaks of nearly the same height across the whole interval instead of growing towards the ends, and its maximum is within a factor of two of the smallest that any choice of nodes can achieve. For the barycentric formula the weights are known in closed form, with no products to compute: , halved at the two ends.

The resulting convergence theory is simple to state. Draw the ellipse in the complex plane with foci whose semi-axes sum to (a Bernstein ellipse). If is analytic inside it, interpolation in Chebyshev points converges like : the further the nearest singularity of lies from the interval, the larger can be and the faster the convergence. For the Runge function, the largest such ellipse passes through the poles at . Its semi-axes are and , so :

Figure 3.3. Convergence for the Runge function.

Equally spaced interpolation diverges, by a factor of at 50 points, while both Chebyshev methods converge at exactly the predicted rate.

Chebyshev series

When can be evaluated anywhere, we are free to choose the nodes, and it is natural to represent the interpolant in a basis made for Chebyshev points. The Chebyshev polynomials are defined by

That this defines a polynomial in follows from the trigonometric identities: , , and since , . The function is expanded as

where the change of variable maps the interval onto .

Substituting turns the right side into . A Chebyshev series is therefore a Fourier cosine series in disguise, and it inherits the properties of Fourier series. The coefficients are computed from samples of at Chebyshev points by a discrete cosine sum. They decay as fast as is smooth, exponentially if is analytic, so trailing coefficients below a tolerance can simply be dropped (truncate).

ChebyshevApproximation evaluates the series by Clenshaw’s recurrence, the analogue of Horner’s rule for Chebyshev polynomials: it uses the three-term recurrence to sum the series in operations without ever forming the . The series is differentiated or integrated by simple recurrences on the coefficients. Chebyshev series are the basis of the Chebfun system and of the spectral methods for differential equations.

Algorithm

Newton form. Input: points with distinct .

  1. Set-up. Copy the into an array . For , and for running downwards from to , replace by . Running downwards lets each column of the divided-difference table overwrite the previous one in the same array. At the end . Cost: , once.
  2. Evaluation at . Start with and, for down to 0, set . Cost: per point.

Barycentric form. Input as above.

  1. Set-up. For each , compute . Cost: , once.
  2. Evaluation at . If equals some node , return . Otherwise accumulate the two sums and in one loop and return their ratio. Cost: per point.

Chebyshev series. Input: a function , an interval and a number of terms .

  1. Sample. Evaluate at the points , .
  2. Coefficients. For , compute . Cost: as a direct sum.
  3. Evaluation at (Clenshaw). Map to . Set and, for down to 1, . Return . Cost: per point.

Implementation

The barycentric evaluation, shared with the Floater-Hormann rational interpolant:

/// Second-form barycentric evaluation shared with Floater-Hormann and Chebyshev interpolation.
pub(crate) fn barycentric_eval(x_nodes: VR1D, y: VR1D, w: VR1D, x: f64) -> f64 {
    let (mut num, mut den) = (0.0, 0.0);
    for i in 0..x_nodes.len() {
        let d = x - x_nodes[i];
        if d == 0.0 {
            return y[i];
        }
        let t = w[i] / d;
        num += t * y[i];
        den += t;
    }
    num / den
}
  • Both interpolating forms reject repeated abscissas. (The Newton form used to divide by zero and return infinite coefficients instead.)
  • The barycentric weights are products of differences and can overflow or underflow for hundreds of points on a wide interval; rescaling all weights by a common factor leaves the formula unchanged, which is the usual remedy.
  • The Chebyshev series uses the points (Chebyshev points of the first kind, which exclude the endpoints); the interpolation figure uses the second-kind points , which include them. Both cluster correctly.

In practice

//! Polynomial interpolation: Newton and barycentric forms, Chebyshev points and series.
//!
//! Run with `cargo run -p lib_jsl_interpolation --example interpolation_polynomial`.

use lib_jsl_core::R1D;
use lib_jsl_interpolation::interpolation_trait::InterpolationTrait;
use lib_jsl_interpolation::one_d::chebyshev_approximations::ChebyshevApproximation;
use lib_jsl_interpolation::one_d::lagrange_interpolant::LagrangeInterpolant;
use lib_jsl_interpolation::one_d::polynomial_interpolator::PolynomialInterpolator;
use std::f64::consts::PI;

fn main() {
    // The same cubic through four points, in Newton and barycentric form.
    let x = R1D::from_vec(vec![0.0, 1.0, 2.0, 3.0]);
    let y = R1D::from_vec(vec![1.0, 2.0, 0.0, -1.0]);
    let mut newton = PolynomialInterpolator::new(x.view(), y.view()).unwrap();
    let mut barycentric = LagrangeInterpolant::new(x.view(), y.view()).unwrap();
    println!("p(1.5): Newton {:.12}, barycentric {:.12}", newton.interpolate_at(1.5).unwrap(), barycentric.interpolate_at(1.5).unwrap());

    // The Runge function at 21 points: equally spaced against Chebyshev.
    let runge = |x: f64| 1.0 / (1.0 + 25.0 * x * x);
    let max_error = |nodes: Vec<f64>| {
        let (x, y) = (R1D::from_vec(nodes.clone()), R1D::from_vec(nodes.iter().map(|&v| runge(v)).collect()));
        let mut p = LagrangeInterpolant::new(x.view(), y.view()).unwrap();
        (0..=1000).map(|i| { let t = -1.0 + i as f64 / 500.0; (p.interpolate_at(t).unwrap() - runge(t)).abs() }).fold(0.0, f64::max)
    };
    let n = 21;
    println!("21 equally spaced points: max error {:.2e}", max_error((0..n).map(|i| -1.0 + 2.0 * i as f64 / (n - 1) as f64).collect()));
    println!("21 Chebyshev points:      max error {:.2e}", max_error((0..n).map(|j| (PI * j as f64 / (n - 1) as f64).cos()).collect()));

    // A Chebyshev series for a smooth function: fit 60 terms, drop the negligible tail, differentiate.
    let mut c = ChebyshevApproximation::new(|x: f64| (x * x).cos() * x.exp(), 0.0, 2.0, 60).unwrap();
    c.truncate(1e-15);
    println!("Chebyshev series for cos(x²)eˣ on [0, 2]: {} of 60 coefficients exceed 1e-15", c.coefficients().len());
    let d = c.derivative();
    let exact = (-2.0 * (1.0f64).sin() + 1f64.cos()) * 1f64.exp();
    println!("derivative at x = 1: {:.13} (exact {exact:.13})", d.evaluate(1.0));
}

Strengths

  • One global formula, smooth and easy to differentiate and integrate.
  • With Chebyshev points, geometric convergence for analytic functions: often machine precision with a few dozen points.

Weaknesses

  • With equally spaced points, high degrees are useless: the Runge phenomenon, and huge sensitivity to noise in the data. Use piecewise interpolation or rational methods for such data.
  • A single global polynomial cannot follow a function with a kink or a jump; the error then falls only algebraically, with oscillations near the discontinuity.

Exercises

  1. Show that the Newton coefficients of through are , and use them to check .
  2. Show that the barycentric weights for the Chebyshev points are proportional to , halved at and .
  3. Compute the maximum of on for 11 equally spaced and 11 Chebyshev points. How different are they?
  4. Interpolate in Chebyshev points. How does the error decrease with ? Compare with .
  5. Add noise of size to samples of at 30 equally spaced points and interpolate. How large is the error? Repeat with Chebyshev points.
Hints and answers
  1. Divided differences: , , ; then , ; then . At : .
  2. The weight is with , which for these points is a multiple of . Evaluating the derivative at gives a quantity proportional to , with the end points picking up half the value.
  3. Equally spaced: about . Chebyshev points of the first kind: , the smallest possible value for monic degree 11 polynomials. Chebyshev extrema: about . The Chebyshev choice is about nine times better here, and the gap grows exponentially with .
  4. For the error falls only like (the kink limits smoothness). For it falls faster than any power, essentially geometrically, until rounding level.
  5. Noise is amplified by the Lebesgue constant, which for 30 equally spaced points is of the order of (it grows like ), so the error near the ends is of order one. For Chebyshev points the constant is about and the error stays a few times .

Further reading

  • C. Runge, “Über empirische Funktionen und die Interpolation zwischen äquidistanten Ordinaten”, Zeitschrift für Mathematik und Physik 46 (1901) 224–243.
  • J.-P. Berrut and L. N. Trefethen, “Barycentric Lagrange interpolation”, SIAM Review 46 (2004) 501–517.
  • N. J. Higham, “The numerical stability of barycentric Lagrange interpolation”, IMA Journal of Numerical Analysis 24 (2004) 547–556.
  • L. N. Trefethen, Approximation Theory and Approximation Practice, extended ed., SIAM, 2019. The modern account of Chebyshev interpolation and its convergence.
  • W. H. Press, S. A. Teukolsky, W. T. Vetterling and B. P. Flannery, Numerical Recipes, 3rd ed., Cambridge University Press, 2007, sections 3.2 and 5.8.