Open Educational Resources

Polynomial Interpolation: Lagrange Interpolating Polynomials

Lagrange Interpolating Polynomials

Another equivalent method to find the interpolating polynomials is using the Lagrange Polynomials. Given n+1 data points: (x_1,y_1),(x_2,y_2),\cdots,(x_{n+1},y_{n+1}), then the Lagrange polynomial of degree n that fits through the n+1 data points has the form:

    \[ f_n(x)=L_1y_1+L_2y_2+L_3y_3+\cdots+L_{n+1}y_{n+1}=\sum_{i=1}^{n+1}L_iy_i \]

where

    \[ L_i=\prod_{j=1,j\neq i}^{n+1}\frac{x-x_j}{x_i-x_j} \]

In the following Mathematica code a Lagrange polynomial procedure is created to output the Lagrange polynomials.

View Mathematica Code
Data = {{1, 3}, {4, 5}, {5, 6}, {6, 8}, {7, 8}};
Lagrange[anydata_] := Sum[anydata[[i, 2]]*LP[anydata, i], {i, 1, Length[anydata]}]
LP[anydata_, i_] := (newdata = Drop[anydata, {i}]; Product[(x - newdata[[j, 1]])/(anydata[[i, 1]] - newdata[[j, 1]]), {j, 1, Length[newdata]}])
(*Using the Lagrange polynomial function*)
y = Expand[Lagrange[Data]]
(*Using the interpolating polynomial built-in function*)
y2 = Expand[InterpolatingPolynomial[Data, x]]
View Python Code
import numpy as np
import sympy as sp
sp.init_printing(use_latex=True)

x = sp.symbols('x')
Data = [(1, 3), (4, 5), (5, 6), (6, 8), (7, 8)]
def Lagrange(anydata):
  return sum([anydata[i][1]*LP(anydata,i) for i in range(len(anydata))])
def LP(anydata, i):
  newdata = anydata.copy()
  newdata.pop(i)
  return np.prod([(x - newdata[j][0])/(anydata[i][0] - newdata[j][0]) for j in range(len(newdata))])
# Using the Lagrange polynomial function
display("y:  ",sp.expand(Lagrange(Data)))
# Using the interpolating polynomial built-in function
display("y2: ",sp.interpolate(Data, x))

The Lagrange interpolating polynomials produce the same polynomial as the general method and the Newton’s interpolating polynomials. The examples used for the Newton’s interpolating polynomials will be repeated here.

Example 1

Using Lagrange interpolating polynomials, find the interpolating polynomial to the data: (1,1),(2,5).

Solution

We have two data points, so, we will create a polynomial of the first degree. Therefore, the interpolating polynomial has the form:

    \[\begin{split} y&=L_1y_1+L_2y_2=\left(\frac{x-x_2}{x_1-x_2}\right)y_1+\left(\frac{x-x_1}{x_2-x_1}\right)y_2\\ &=\left(\frac{x-2}{1-2}\right)\times 1+\left(\frac{x-1}{2-1}\right)\times 5\\ &=4x-3 \end{split} \]

Example 2

Using Lagrange interpolating polynomials, find the interpolating polynomial to the data: (1,1),(2,5),(3,2).

Solution

We have three data points, so, we will create a polynomial of the second degree. Using Lagrange polynomials:

    \[\begin{split} y&=L_1y_1+L_2y_2+L_3y_3\\ &=\frac{(x-x_2)(x-x_3)}{(x_1-x_2)(x_1-x_3)}y_1+\frac{(x-x_1)(x-x_3)}{(x_2-x_1)(x_2-x_3)}y_2+\frac{(x-x_1)(x-x_2)}{(x_3-x_1)(x_3-x_2)}y_3\\ &=\frac{(x-2)(x-3)}{(1-2)(1-3)}(1)+\frac{(x-1)(x-3)}{(2-1)(2-3)}(5)+\frac{(x-1)(x-2)}{(3-1)(3-2)}(2)\\ &=-3.5x^2+14.5x-10 \end{split} \]

Example 3

Using Lagrange polynomials, find the interpolating polynomial to the data: (1,1),(2,5),(3,2),(3.2,7),(3.9,4).

Solution

A fourth order polynomial would be needed to pass through five data points. Using Lagrange polynomials, the required function has the form:

    \[\begin{split} y=&\frac{(x-x_2)(x-x_3)(x-x_4)(x-x_5)}{(x_1-x_2)(x_1-x_3)(x_1-x_4)(x_1-x_5)}y_1+\frac{(x-x_1)(x-x_3)(x-x_4)(x-x_5)}{(x_2-x_1)(x_2-x_3)(x_2-x_4)(x_2-x_5)}y_2\\ &+\frac{(x-x_1)(x-x_2)(x-x_4)(x-x_5)}{(x_3-x_1)(x_3-x_2)(x_3-x_4)(x_3-x_5)}y_3+\frac{(x-x_1)(x-x_2)(x-x_3)(x-x_5)}{(x_4-x_1)(x_4-x_2)(x_4-x_3)(x_4-x_5)}y_4\\ &+\frac{(x-x_1)(x-x_2)(x-x_3)(x-x_4)}{(x_5-x_1)(x_5-x_2)(x_5-x_3)(x_5-x_4)}y_5\\ =&-14.3461x^4+144.181x^3-509.935x^2+739.728x-358.628 \end{split} \]

MATLAB file for the above examples: Download

Lecture Video



Leave a Reply

Your email address will not be published. Required fields are marked *