Lagrange polynomial

This image shows, for four data points ((−9, 5), (−4, 2), (−1, −2), (7, 9)), the (cubic) interpolation polynomial L(x) (dashed, black), which is the sum of the scaled basis polynomials y0ℓ0(x), y1ℓ1(x), y2ℓ2(x) and y3ℓ3(x). The interpolation polynomial passes through all four control points, and each scaled basis polynomial passes through its respective control point and is 0 where x corresponds to the other three control points.

In numerical analysis, the Lagrange interpolating polynomial is the unique polynomial of lowest degree that interpolates a given set of data.

Given a data set of coordinate pairs ⁠(xj,yj)⁠, the ⁠xj⁠ are called nodes and the ⁠yj⁠ are called values. The Lagrange polynomial ⁠L(x)⁠ which interpolates the data assumes each value at the corresponding node, ⁠L(xj)=yj⁠. If there are ⁠k+1⁠ data pairs, the Lagrange polynomial has degree ⁠≤k⁠.

Although named after Joseph-Louis Lagrange, who published it in 1795, the method was first discovered in 1779 by Edward Waring. It is also an easy consequence of a formula published in 1783 by Leonhard Euler.

Uses of Lagrange polynomials include the Newton–Cotes method of numerical integration, Shamir's secret sharing scheme in cryptography, and Reed–Solomon error correction in coding theory.

For equispaced nodes, Lagrange interpolation is susceptible to Runge's phenomenon of large oscillation.

Lagrange polynomials are also related to matrix eigenspace projection via Frobenius covariants.

Definition

Given a set of ⁠k+1⁠ nodes {x0,x1,…,xk}, which must all be distinct, ⁠xj≠xm⁠ for indices ⁠j≠m⁠, the Lagrange basis for polynomials of degree ⁠≤k⁠ for those nodes is the set of polynomials {ℓ0(x),ℓ1(x),…,ℓk(x)} each of degree ⁠k⁠ which take values ⁠ℓj(xm)=0⁠ if ⁠m≠j⁠ and ⁠ℓj(xj)=1⁠. Using the Kronecker delta this can be written ⁠ℓj(xm)=δjm⁠. Each basis polynomial can be explicitly described by the product:

ℓj(x)=(x−x0)(xj−x0)⋯(x−xj−1)(xj−xj−1)(x−xj+1)(xj−xj+1)⋯(x−xk)(xj−xk)=∏0≤m≤km≠jx−xmxj−xm|.

Notice that the numerator ⁠∏m≠j(x−xm)⁠ has ⁠k⁠ roots at the nodes {xm}m≠j while the denominator ⁠∏m≠j(xj−xm)⁠ scales the resulting polynomial so that ⁠ℓj(xj)=1⁠.

The Lagrange interpolating polynomial for those nodes through the corresponding values {y0,y1,…,yk} is the linear combination:

L(x)=∑j=0kyjℓj(x).

Each basis polynomial has degree ⁠k⁠, so the sum ⁠L(x)⁠ has degree ⁠≤k⁠, and it interpolates the data because ⁠L(xm)=∑j=0kyjℓj(xm)=∑j=0kyjδmj=ym⁠.

The interpolating polynomial is unique. Proof: assume some polynomial ⁠M(x)⁠ of degree ⁠≤k⁠ interpolates the data. Then the difference ⁠M(x)−L(x)⁠ is zero at ⁠k+1⁠ distinct nodes {x0,x1,…,xk}. But the only polynomial of degree ⁠≤k⁠ with more than ⁠k⁠ roots is the constant zero function, so ⁠M(x)−L(x)=0⁠, or ⁠M(x)=L(x)⁠.

The unique interpolating polynomial can alternatively be constructed using Newton polynomials as an alternative basis, or generalized to Hermite interpolating polynomials when there are confluent nodes.

Barycentric form

Each Lagrange basis polynomial ⁠ℓj(x)⁠ can be rewritten as the product of three parts, a function ⁠ℓ(x)=∏m(x−xm)⁠ common to every basis polynomial, a node-specific constant ⁠wj=∏m≠j(xj−xm)−1⁠ (called the barycentric weight), and a part representing the displacement from ⁠xj⁠ to ⁠x⁠:

ℓj(x)=ℓ(x)wjx−xj

By factoring ⁠ℓ(x)⁠ out from the sum, we can write the Lagrange polynomial in the so-called first barycentric form:

L(x)=ℓ(x)∑j=0kwjx−xjyj.

If the weights ⁠wj⁠ have been pre-computed, this requires only ⁠𝒪(k)⁠ operations compared to ⁠𝒪(k2)⁠ for evaluating each Lagrange basis polynomial ⁠ℓj(x)⁠ individually. (See Big O notation.)

The barycentric interpolation formula can also easily be updated to incorporate a new node ⁠xk+1⁠ by dividing each of the ⁠wj⁠, ⁠j=0…k⁠ by ⁠(xj−xk+1)⁠ and constructing the new ⁠wk+1⁠ as above.

For any ⁠x⁠, ∑j=0kℓj(x)=1 because the constant function g(x)=1 is the unique polynomial of degree ≤k interpolating the data {(x0,1),(x1,1),…,(xk,1)}. We can thus further simplify the barycentric formula by dividing L(x)=L(x)/g(x):

L(x)=ℓ(x)∑j=0kwjx−xjyj/ℓ(x)∑j=0kwjx−xj=∑j=0kwjx−xjyj/∑j=0kwjx−xj.

This is called the second form or true form of the barycentric interpolation formula.

This second form has advantages in computation cost and accuracy: it avoids evaluation of ℓ(x); the work to compute each term in the denominator wj/(x−xj) has already been done in computing (wj/(x−xj))yj and so computing the sum in the denominator costs only k addition operations; for evaluation points x which are close to one of the nodes xj, catastrophic cancelation would ordinarily be a problem for the value (x−xj), however this quantity appears in both numerator and denominator and the two cancel leaving good relative accuracy in the final result.

Using this formula to evaluate L(x) at one of the nodes xj will result in the indeterminate ∞yj/∞; computer implementations must replace such results by L(xj)=yj.

Each Lagrange basis polynomial can also be written in barycentric form:

ℓj(x)=wjx−xj/∑m=0kwmx−xm.

A perspective from linear algebra

Solving an interpolation problem leads to a problem in linear algebra amounting to inversion of a matrix. Using a standard monomial basis for our interpolation polynomial L(x)=∑j=0kxjmj, we must invert the Vandermonde matrix (xi)j to solve L(xi)=yi for the coefficients mj of L(x). By choosing a better basis, the Lagrange basis, L(x)=∑j=0kℓj(x)yj, we merely get the identity matrix, δij, which is its own inverse: the Lagrange basis automatically inverts the analog of the Vandermonde matrix.

More explicitly, let ⁠L⁠ be the inverse Vandermonde matrix such that: L=(L00⋯L0n⋮⋱⋮Ln0⋯Lnn). The Lagrange basis polynomial ⁠ℓj(x)⁠ can be expressed directly using the columns of ⁠L⁠: ℓj(x)=L0j+L1jx+⋯+Lnjxn.

This construction is analogous to the Chinese remainder theorem. Instead of checking for remainders of integers modulo prime numbers, we are checking for remainders of polynomials when divided by linears.

Furthermore, when the order is large, Fast Fourier transformation can be used to solve for the coefficients of the interpolated polynomial.

In numerical practice, directly computing ⁠L⁠ by inverting the Vandermonde matrix is generally avoided because Vandermonde matrices are notoriously ill-conditioned. As the number of nodes or their degree increases, their condition number grows exponentially, making direct matrix inversion numerically unstable and highly sensitive to floating-point errors.

Example

We wish to interpolate f(x)=x2 over the domain 1≤x≤3 at the three nodes {1,2,3}:

x0=1,y0=f(x0)=1,x1=2,y1=f(x1)=4,x2=3,y2=f(x2)=9.

The node polynomial ℓ is ℓ(x)=(x−1)(x−2)(x−3)=x3−6x2+11x−6.

The barycentric weights are w0=(1−2)−1(1−3)−1=12,w1=(2−1)−1(2−3)−1=−1,w2=(3−1)−1(3−2)−1=12.

The Lagrange basis polynomials are

ℓ0(x)=x−21−2⋅x−31−3=12x2−52x+3,ℓ1(x)=x−12−1⋅x−32−3=−x2+4x−3,ℓ2(x)=x−13−1⋅x−23−2=12x2−32x+1.

The Lagrange interpolating polynomial is: L(x)=y0⋅ℓ0(x)+y1⋅ℓ1(x)+y2⋅ℓ2(x)=x2.

In (second) barycentric form,

L(x)=∑j=02wjx−xjyj∑j=02wjx−xj=12x−1+−4x−2+92x−312x−1+−1x−2+12x−3.

Remainder in Lagrange interpolation formula

When interpolating a given function ⁠f⁠ by a polynomial of degree ⁠k⁠ at the nodes x0,…,xk we get the remainder R(x)=f(x)−L(x) which can be expressed as

R(x)=f[x0,…,xk,x]ℓ(x)=ℓ(x)f(k+1)(ξ)(k+1)!,x0<ξ<xk,

where f[x0,…,xk,x] is the notation for divided differences. Alternatively, the remainder can be expressed as a contour integral in complex domain as

R(x)=ℓ(x)2πi∫Cf(t)(t−x)(t−x0)⋯(t−xk)dt=ℓ(x)2πi∫Cf(t)(t−x)ℓ(t)dt.

The remainder can be bound as

|R(x)|≤(xk−x0)k+1(k+1)!maxx0≤ξ≤xk|f(k+1)(ξ)|.

Derivation

Clearly, R(x) is zero at nodes. To find R(x) at a point xp, define a new function F(x)=R(x)−R~(x)=f(x)−L(x)−R~(x) and choose R~(x)=C⋅∏i=0k(x−xi) where C is the constant we are required to determine for a given xp. We choose C so that F(x) has k+2 zeroes (at all nodes and xp) between x0 and xk (including endpoints). Assuming that f(x) is k+1-times differentiable, since L(x) and R~(x) are polynomials, and therefore, are infinitely differentiable, F(x) will be k+1-times differentiable. By Rolle's theorem, F(1)(x) has k+1 zeroes, F(2)(x) has k zeroes... F(k+1) has 1 zero, say ξ, where x0<ξ<xk. Explicitly writing F(k+1)(ξ):

F(k+1)(ξ)=f(k+1)(ξ)−L(k+1)(ξ)−R~(k+1)(ξ) L(k+1)=0,R~(k+1)=C⋅(k+1)! (Because the highest power of x in R~(x) is k+1)

0=f(k+1)(ξ)−C⋅(k+1)!

The equation can be rearranged as

C=f(k+1)(ξ)(k+1)! Since F(xp)=0 we have R(xp)=R~(xp)=fk+1(ξ)(k+1)!∏i=0k(xp−xi)

Derivatives

The ⁠d⁠th derivative of a Lagrange interpolating polynomial can be written in terms of the derivatives of the basis polynomials,

L(d)(x):=∑j=0kyjℓj(d)(x).

Recall (see § Definition above) that each Lagrange basis polynomial is

ℓj(x)=∏m=0m≠jkx−xmxj−xm.

The first derivative can be found using the product rule:

ℓj′(x)=∑i=0i≠jk[1xj−xi∏m=0m≠(i,j)kx−xmxj−xm]=ℓj(x)∑i=0i≠jk1x−xi.

The second derivative is

ℓj″(x)=∑i=0i≠jk1xj−xi[∑m=0m≠(i,j)k(1xj−xm∏n=0n≠(i,j,m)kx−xnxj−xn)]=ℓj(x)∑0≤i<m≤k2(x−xi)(x−xm)=ℓj(x)[(∑i=0i≠jk1x−xi)2−∑i=0i≠jk1(x−xi)2].

The third derivative is

ℓj‴(x)=ℓj(x)∑0≤i<m<n≤k3!(x−xi)(x−xm)(x−xn)

and likewise for higher derivatives.

Note that all of these formulas for derivatives are invalid at or near a node. A method of evaluating all orders of derivatives of a Lagrange polynomial efficiently at all points of the domain, including the nodes, is converting the Lagrange polynomial to power basis form and then evaluating the derivatives.

Multivariate case

A Lagrange basis can also be constructed in the multivariate case. Let

f(𝐱)=∑i=0U−1αi𝐱𝐤i

be a degree-d polynomial on n variables, where U=(n+dn) is the number of terms and

𝐱𝐤i=∏j=0n−1xjkij.

Let then M be the U×U matrix with elements

Mab=𝐭a𝐤b

for some set of points {𝐭i}i=0U−1. This set of points uniquely determines the polynomial f iff Δ:=det⁡(M)≠0. In this case we can construct the Lagrange basis as follows:

ℓi(𝐱)=Δi(𝐱)Δ,

where Δi(𝐱) is the determinant of the matrix M(i)(𝐱) with elements

M(i)(𝐱)ab=δai𝐱a𝐤b+(1−δai)𝐭a𝐤b.

Then it holds that

f(𝐱)=∑i=0U−1f(𝐭i)ℓi(𝐱)

Finite fields

The Lagrange polynomial can also be computed in finite fields. This has applications in cryptography, such as in Shamir's Secret Sharing scheme.

See also