Skip to article frontmatterSkip to article content

The barycentric formula

The Lagrange formula (9.1.4) is useful theoretically but not ideal for computation. For each new value of xx, all of the cardinal functions k\ell_k must be evaluated at xx, which requires a product of nn terms. Thus the total work is O(n2)O(n^2) for every value of xx. Moreover, the formula is numerically unstable. An alternative version of the formula improves on both issues.

9.2.1Derivation

We again will use the error indicator function Φ from Definition 9.1.2,

Φ(x)=j=0n(xtj), \Phi(x) = \prod_{j=0}^n (x-t_j),

as well as a set of values derived from the nodes.

The following formula is the key to efficient and stable evaluation of a polynomial interpolant.

Equation (9.2.3) is certainly an odd-looking way to write a polynomial! Indeed, it is technically undefined when xx equals one of the nodes, but in fact, limxtkp(x)=yk\lim_{x\to t_k} p(x) = y_k, so a continuous extension to the nodes is justified. (See Exercise 3.)

9.2.2Implementation

For certain important node distributions, simple formulas for the weights wkw_k are known. Otherwise, the first task of an implementation is to compute the weights wkw_k, or more conveniently, wk1w_k^{-1}.

We begin with the singleton node set {t0}\{t_0\}, for which one gets the single weight w0=1w_0=1. The idea is to grow this singleton into the set of all nodes through a recursive formula. Define ωk,m1\omega_{k,m-1} (for k<mk< m) as the inverse of the weight for node kk using the set {t0,,tm1}\{t_0,\ldots,t_{m-1}\}. Then

ωk,m=j=0jkm(tktj)=ωk,m1(tktm),k=0,1,,m1.\omega_{k,m} = \displaystyle \prod_{\substack{j=0\\j\neq k}}^{m} (t_k - t_j) = \omega_{k,m-1} \cdot (t_k-t_{m}), \qquad k=0,1,\ldots,m-1.

A direct application of (9.2.2) can be used to find ωm,m\omega_{m,m}. This process is iterated over m=1,,nm=1,\ldots,n to find wk=ωk,n1w_k=\omega_{k,n}^{-1}.

In Function 9.2.1 we give an implementation of the barycentric formula for polynomial interpolation.

Computing all n+1n+1 weights in Function 9.2.1 takes O(n2)O(n^2) operations. Fortunately, the weights depend only on the nodes, not the data, and once they are known, computing p(x)p(x) at a particular value of xx takes just O(n)O(n) operations.

9.2.3Stability

You might suspect that as the evaluation point xx approaches a node tkt_k, subtractive cancellation error will creep into the barycentric formula because of the term 1/(xtk)1/(x-t_k). While such errors do occur, they turn out not to cause trouble, because the same cancellation happens in the numerator and denominator. In fact, the stability of the barycentric formula has been proved, though we do not give the details.

9.2.4Exercises

  1. (a) Find the barycentric weights for the nodes t0=0t_0=0, t1=1t_1=1, t2=3t_2=3.

    (b) Compute the interpolant at x=2x=2 for the nodes in part (a) and the data y0=2y_0=-2, y1=2y_1=2, y2=1y_2=1.

  2. ✍ For each case of Exercise 9.1.1, write out the barycentric form of the interpolating polynomial.

  1. ✍ Show using L’Hôpital’s rule on (9.2.3) that p(ti)=yip(t_i)=y_i for all i=0,,ni=0,\ldots,n.

  2. ⌨ In each case, use Function 9.2.1 to interpolate the given function using n+1n+1 evenly spaced nodes in the given interval. Plot each interpolant together with the exact function.

    (a) f(x)=ln(x),n=2,3,4,x[1,10]f(x) = \ln (x), \quad n = 2,3,4, \quad x\in [1,10]

    (b) f(x)=tanh(x),n=2,3,4,x[03,2]f(x) = \tanh (x), \quad n = 2,3,4, \quad x \in [0-3,2]

    (c) f(x)=cosh(x),n=2,3,4,x[1,3]f(x) = \cosh (x), \quad n = 2,3,4, \quad x \in [-1,3]

    (d) f(x)=x,n=3,5,7,x[2,1]f(x) = |x|, \quad n = 3,5,7, \quad x \in [-2,1]

  3. ⌨ Using code from Function 9.2.1, compute the barycentric weights numerically using n+1n+1 equally spaced nodes in [1,1][-1,1] for n=30n=30, n=60n=60, and n=90n=90. On a single graph, plot wi|w_i| as a function of tit_i on a log-linear scale. (The resulting graphs are an indication of the trouble with equally spaced nodes that is explored in Stability of polynomial interpolation.)

  4. ✍ Derive this fact stated implicitly in (9.2.2):

    Φ(xk)=j=0jkn(tktj).\Phi'(x_k) = \prod_{\substack{j=0\\j\neq k}}^n (t_k - t_j).
  5. ✍ Use (9.2.4) to show that if jkj\neq k,

    k(xj)=wkwj(xjxk).\ell_k'(x_j) = \frac{w_k}{w_j(x_j-x_k)}.