Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

 Open In Colab

Introduction

Polynomial interpolation is a straightforward approach to interpolation.

Three methods to obtain polynomials are established here. For a given set of data, they all must result in the same polynomial. The difference is the means by which they are achieved, which translates to the ways that they are used.

The polynomial methods discussed here rely on a core property of linear algebra:

It is always possible to construct a unique polynomial of degree nn that passes exactly through n+1n + 1 distinct data points.

Error in Interpolation

It can be shown that the error in the interpolation is,

ytrue(x)−y(x)=[x−x1][x−x2][x−x3]...[x−xn](n+1)!f(n+1)(ξ)y^{true}(x)-y(x) = \frac{[x-x_1][x-x_2][x-x_3]...[x-x_n]}{(n+1)!} f^{(n+1)}(\xi)

where ξ\xi is in the interval (x0,xn)(x_0, x_n).

Since for nn datapoints there is a unique polynomial of degree n−1n-1, which can be expressed as a Lagrange polynomial, this analysis is universal to all polynomial interpolations!. The main takeaway is that:

The further a data point is from xx, the more it contributes to the error.

Lagrange Polynomial Interpolation

Lagrange polynomial interpolation constructs the polynomial as,

y(x)=∑i=1nyiPi(x)y(x) = \sum_{i = 1}^n y_i P_i(x)

which is a weighted sum of the Lagrange basis polynomials, Pi(x)P_i(x),

Pi(x)=∏j=1,j≠inx−xjxi−xj.P_i(x) = \prod_{j = 1, j\ne i}^n\frac{x - x_j}{x_i - x_j}.

N.B.: ∏\prod means the product of, like ∑\sum means the sum of.

Lagrange Basis Polynomials

By construction,

  • Pi(xj)=1P_i(x_j) = 1 when i=ji = j

  • Pi(xj)=0P_i(x_j) = 0 when i≠ji \ne j.

Use the data: x = [0, .5, 2] y = [1, 3, 2]

P1(x)=(x−x2)(x−x3)(x1−x2)(x1−x3)=(x−1)(x−2)(0−1)(0−2)=12(x2−3x+2),P2(x)=(x−x1)(x−x3)(x2−x1)(x2−x3)=(x−0)(x−2)(1−0)(1−2)=−x2+2x,P3(x)=(x−x1)(x−x2)(x3−x1)(x3−x2)=(x−0)(x−1)(2−0)(2−1)=12(x2−x).\begin{aligned} P_1(x) &= \frac{(x - x_2)(x - x_3)}{(x_1-x_2)(x_1-x_3)} = \frac{(x - 1)(x - 2)}{(0-1)(0-2)} = \frac{1}{2}(x^2 - 3x + 2),\\ P_2(x) &= \frac{(x - x_1)(x - x_3)}{(x_2-x_1)(x_2-x_3)} = \frac{(x - 0)(x - 2)}{(1-0)(1-2)} = -x^2 + 2x,\\ P_3(x) &= \frac{(x - x_1)(x - x_2)}{(x_3-x_1)(x_3-x_2)} = \frac{(x - 0)(x - 1)}{(2-0)(2-1)} = \frac{1}{2}(x^2 - x). \end{aligned}

Let’s plot each polynomial and verify the property that Pi(xj)=1P_i(x_j) = 1 when i=ji = j and Pi(xj)=0P_i(x_j) = 0 when i≠ji \ne j.

Assembling the Polynomial

Since Pi≠j=0P_{i\ne j}=0, and Pi=j=1P_{i = j}=1, it is trivial to see that for y(x)=∑i=1nωiPi(x) y(x) = \sum_{i = 1}^n \omega_i P_i(x) , the coefficients are simply:

y(x)=∑i=1nyiPi(x)y(x) = \sum_{i = 1}^n y_i P_i(x)

Analysis

We can observe some notes:

  • For nn data points we necessarily produce a unique polynominal that crosses each one.

  • If we have two measurements at the same input, xi=xjx_i = x_j, Pi=∼10P_i =\sim \frac{1}{0} which is undefined unless xi=xjx_i=x_j and yi=yjy_i=y_j in which case the data pair is redundant and can be removed.

  • Each evalulation of P(x)P(x) involves n−1n-1 products, and L(x)L(x) is the sum of nn bases, therefore evaluation is O(n2)O(n^2)

  • Adding new data means restarting the compuation.

Barycentric Lagrange Interpolation

Let’s try to improve the performance of Lagrange Interpolation. Let:

Ω(x)=∏j=1n[x−xj]\Omega(x) = \prod_{j = 1}^n [x - x_j]

and the barycentric weights, wiw_i:

wi=∏j=1,j≠in1xi−xj.w_i = \prod_{j = 1, j\ne i}^n\frac{1}{x_i - x_j}.

and write:

Pi(x)=Ω(x)wix−xj.P_i(x) = \Omega(x) \frac{w_i}{x - x_j}.

and factor the Ω\Omega out of the sum:

y(x)=Ω(x)∑j=1nwix−xjyi.y(x) = \Omega(x) \sum_{j = 1}^n \frac{w_i}{x - x_j} y_i.

which is O(n)O(n) for evaluation. Calculation of wiw_i can be formulated recursively, such that each wiw_i takes O(n)O(n) and the full takes O(n2)O(n^2) with updates n.

NB: The weights depend only on xix_i, not yiy_i - this means if we are measuring multiple functions on the same spacing, we can reuse the weights, leading to substantial computaitonal savings. The benefit being that the calucation of the ωi\omega_i, O(n2)O(n^2) is precomputed.

We can write one more form which is commonly implemented. Let’s add one more piece of data:

1=∑j=0nPj=Ω(x)∑j=0nwjx−xj1 = \sum_{j=0}^n P_j = \Omega(x) \sum_{j=0}^n \frac{w_j}{x-x_j}

then we divide the previous function and write:

y(x)=∑j=0nwix−xjyi∑j=0nwix−xjy(x) = \frac{\sum_{j = 0}^n \frac{w_i}{x - x_j} y_i}{\sum_{j = 0}^n \frac{w_i}{x - x_j}}

where we have cancelled Ω\Omega! Besides elegance, this avoids an issue when evaluating x→xix\rightarrow x_i where roundoff can cause subtractive cancellation. Since the term appears in the numerator and denominator this cancels out!

Newton’s Divided Difference Method

Newton’s polynomial interpolation has the form:

y(x)=a0+a1[x−x0]+a2[x−x0][x−x1]+⋯+an[x−x0][x−x1]…[x−xn]y(x) = a_0 + a_1[x-x_0] + a_2 [x-x_0][x-x_1] + \dots + a_n[x-x_0][x-x_1]\dots[x-x_n]

which has the advantage of O(n)O(n) evaluations due to recursion and nested multiplication. E.g. for 4 terms,

y(x)=a0+[x−x0][a1+[x−x1][a2+[x−x2]a3]]y(x) = a_0 + [x-x_0] \bigg[a_1 + [x-x_1] \big[a_2 + [x-x_2] a_3 \big] \bigg]

Newton’s method is also known as the divided differences

This algorithm was used to calculate function tables like logarithms and trignometry functions. It was then the basis for the difference engine, an early mechanical calculator.

Let’s generalize this. Define the two-argument function:

y[x1,x0]=y1−y0x1−x0y[x_1, x_0] = \frac{y_1 - y_0}{x_1 - x_0}

and the ternary recursively:

y[x2,x1,x0]=y2−y1x2−x1−y1−y0x1−x0x2−x0=y[x2,x1]−y[x1,x0]x2−x1y[x_2, x_1, x_0] = \frac{\frac{y_2 - y_1}{x_2 - x_1} - \frac{y_1 - y_0}{x_1 - x_0}}{x_2 - x_0} = \frac{y[x_2,x_1] - y[x_1,x_0]}{x_2-x_1}

The n-ary function is:

y[xk,xk−1,…,x1,x0]=y[xk,xk−1,…,x2,x2]−y[xk−1,xk−2,…,x1,x0]xk−x0y[x_k, x_{k-1}, \dots, x_{1}, x_0] = \frac{y[x_k, x_{k-1}, \dots, x_{2}, x_2] - y[x_{k-1}, x_{k-2}, \dots, x_{1}, x_0]}{x_k-x_0}

Tableau

We can visualize this is in a tableau:

x0y0y[x1,x0]x1y1y[x2,x1,x0]y[x2,x1]y[x3,x2,x1,x0]x2y2y[x3,x2,x1]y[x4,x3,x2,x1,x0]y[x3,x2]y[x4,x3,x2,x1]x3y3y[x4,x3,x2]y[x4,x3]x4y4\begin{aligned} \begin{array}{cccccc} x_0 & y_0 \\ & & y[x_1,x_0] \\ x_1 & y_1 & & y[x_2, x_1,x_0]\\ & & y[x_2,x_1] & & y[x_3, x_2, x_1,x_0]\\ x_2 & y_2 & & y[x_3, x_2,x_1] & & y[x_4, x_3, x_2, x_1,x_0]\\ & & y[x_3,x_2] & & y[x_4, x_3, x_2, x_1]\\ x_3 & y_3 & & y[x_4, x_3,x_2]\\ & & y[x_4,x_3] \\ x_4 & y_4 \end{array} \end{aligned}

where each element is the difference of the two to the left. The diagonal is the coefficients that we need: a0,a1,a2,a3,a4a_0, a_1, a_2, a_3, a_4.

Notes:

  • The order that the datapoints are added is arbitrary but will result in a different tableau (with the same diagonal).

  • We can build this matrix / tableau diagonal-by-diagonal which means adding new data points doesn’t require recalculation of the others.

  • Each new diagonal (datapoint) takes O(n)O(n) so assembly of the tableau takes O(n2)O(n^2).

  • Evaluation of f(x) takes O(n)O(n).

  • These coefficients are independent of xx.

Direct Solution

Lastly, there is a direct solution method that only became really practical with the advent of modern computing since it focusses on linear systems:

Consider a general, nn th order polynomial,

y(x)=anxn+an−1xn−1…a2x2+a1x+a0y(x) = a_n x^n + a_{n-1} x^{n-1} \dots a_2 x^2 + a_1 x +a_0

since

y(xi)=anxin+an−1xin−1…a2xi2+a1xi+a0=yiy(x_i) = a_n x_i^n + a_{n-1} x_i^{n-1} \dots a_2 x_i^2 + a_1 x_i +a_0 = y_i

we can write out in matrix form,

[1x1x12⋯x1m1x2x22⋯x2m⋮⋮⋮⋱⋮1xnxn2⋯xnm][a0a1a2⋮am]=[y1y2y3⋮yn]\begin{aligned} \begin{bmatrix} 1 & x_1 & x_1^2 & \cdots & x_1^m \\ 1 & x_2 & x_2^2 & \cdots & x_2^m \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & x_n & x_n^2 & \cdots & x_n^m \end{bmatrix} \begin{bmatrix} a_0 \\ a_1 \\ a_2 \\ \vdots \\ a_m \end{bmatrix} = \begin{bmatrix} y_1 \\ y_2 \\ y_3 \\ \vdots \\ y_n \end{bmatrix} \end{aligned}

where the matrix of coefficients is called a Vandermonde matrix. This system can be solved for aia_i with a dense linear solver. The issue with this method is that the system is notoriously ill-conditioned and roundoff error accumulates rapidly for large nn.

Example: Interpolate our Toy Problem

Let us now examine our toy problem. Since all the polynomial interpolation functions generate the same unique polynomial, any will suffice.

YIKES! Watch out for Runge’s phenomenon: Even for a seemingly ideal case of equally spaced samples, higher order polynomials can show huge oscillations between samples! Let’s see this in action: