Ch19.3 Lagrange Interpolating Polynomials

Lagrange Interpolating Polynomials

  • Lagrange interpolating polynomials are used to fit data.
  • They can be constructed easily with pencil and paper; click here for YouTube video.

Lagrange Helping Functions

Suppose we are given \( n \) data points

\[ \small{ (x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n) } \]

Define the Lagrange helping function \( L_k \) as follows:

\[ \small{ L_k = \frac{(x-x_0)\cdots (x-x_{k-1})(x-x_{k+1})\cdots (x-x_n)}{(x_k-x_0)\cdots (x_k-x_{k-1})(x_k-x_{k+1})\cdots (x_k-x_n)} } \]

Lagrange Interpolating Polynomial

Suppose we are given \( n \) data points

\[ \small{ (x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n) } \]

With the Lagrange helping functions

\[ \small{ L_k = \frac{(x-x_0)\cdots (x-x_{k-1})(x-x_{k+1})\cdots (x-x_n)}{(x_k-x_0)\cdots (x_k-x_{k-1})(x_k-x_{k+1})\cdots (x_k-x_n)}, } \]

the Lagrange interpolating polynomial is defined as

\[ \small{ P(x) = y_1 L_1(x) + y_2 L_2(x) + \ldots + y_n L_n(x) } \]

Example 1: Lagrange

Suppose we are given the \( n = 3 \) data points below:

\[ \small{ (1,5), (2,6), (3,7) } \]

Lagrange helping functions:

\[ \small{ L_1 = \frac{(x-2)(x-3)}{(1-2)(1-3)}, \,\, L_2 = \frac{(x-1)(x-3)}{(2-1)(2-3)}, \,\, L_3 = \frac{(x-1)(x-2)}{(3-1)(3-2)} } \]

Lagrange interpolating polynomial:

\[ \small{ \begin{aligned} P(x) & = 5 \frac{(x-2)(x-3)}{(1-2)(1-3)} + 6 \frac{(x-1)(x-3)}{(2-1)(2-3)} + 7 \frac{(x-1)(x-2)}{(3-1)(3-2)} \\ & = x + 4 \,\, \mathrm{(after \, some \, algebra)} \end{aligned} } \]

Example 2: Lagrange

Suppose we are given the \( n = 3 \) data points below:

\[ \small{ (2,16), (5,12), (7,25) } \]

Lagrange helping functions:

\( L_1= \)

\( L_2 = \)

\( L_3 = \)

Lagrange interpolating polynomial (do not simplify):

\( P(x)= \)

Example 3: Degree One Lagrange

We are interested in estimating missing value at \( x = 0.9 \).

\( (a) \) Use \( x_3, x_4 \) to construct a degree one interpolation.

Example 3: Degree One Lagrange

Example 3: Degree Two Lagrange

We are interested in estimating missing value at \( x = 0.9 \).

\( (b) \) Use \( x_2, x_3, x_4 \) to construct a degree two interpolation.

Example 3: Degree Two Lagrange

Example 3: Degree Two using Desmos

Example 3: Degree Three Lagrange

We are interested in estimating missing value at \( x = 0.9 \).

\( (c) \) Use \( x_1, x_2, x_3, x_4 \) to construct degree three interpolation.

Example 3: Degree Three Lagrange

Example: Sine Function, page 1

  • For this example, we use \( \sin(2\pi x) \) on \( [0,1] \) to generate our data points.
  • We use scipy to obtain Lagrange interpolating polynomial.
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import lagrange

#Enter data
xdata = np.linspace(0,1,10) 
ydata = np.sin(2*np.pi*xdata)

Example: Sine Function, page 2

# Use scypy to find interpolating polynomial
f = lagrange(xdata, ydata)

# Plot commands
t = np.linspace(0,1,100)
plt.plot(xdata,ydata,'bo',t,f(t),'r')   
plt.xlabel('x')
plt.legend(('Original Data Points','Interpolating Polynomial'),loc = 0)
plt.show()   
# loc = 0 puts legend in "best" location; see
# https://tinyurl.com/aay752xt  

Example: Sine Function, page 3

plot of chunk unnamed-chunk-3

Book Example (adapted from Ch19.4)

  • Use Python to generate the interpolating polynomial for the cross section of the Shrine of the Book in Jerusalem.
  • We will write our own code to compute Lagrange polynomial interpolation at \( x = 3.5 \), and scipy to generate and plot the Lagrange interpolating polynomial.

Ch19.4 Cubic Spline

Interpolated value at 3.5000 is 1.8944.

plot of chunk unnamed-chunk-4

Lagrange Interpolation: Python Output

Interpolated value at 3.5000 is 2.6630.

plot of chunk unnamed-chunk-5

Python Program, page 1

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import lagrange

#Enter data
x = [-5.8, -5.0, -4.0, -2.5, -1.5, -0.8, 0, 0.8, 1.5, 2.5, 4.0, 5.0, 5.8]
y = [0, 1.5, 1.8, 2.2, 2.7, 3.5, 3.9, 3.5, 2.7, 2.2, 1.8, 1.5, 0]

Python Program, page 2

# Evaluate Lagrange Interpolating polyn at x = xp

def LagrangeInterp(xdata,ydata,xp):
n = len(xdata)
yp = 0

for k in range(0,n):
  factor = 1
  for i in range(0,n): 
    if i != k:
factor = factor*(xp-xdata[i])/(xdata[k]-xdata[i])
    yp = yp + factor*ydata[k]

# Displaying output
print('Interpolated value at %.4f is %.4f.' % (xp, yp)

Python Program, page 3

# Use scypy to find interpolating polynomial
f = lagrange(xdata, ydata)

# Plot commands
t = np.linspace(xdata[0],xdata[n-1],100)        
plt.plot(xdata,ydata,'bo',t,f(t),'r',xp,yp,'sg')   
plt.xlabel('x')
plt.legend(('Original Data Points','Interpolating Polynomial','Interpolation Point'),loc = 0)
plt.axis('square')
plt.ylim(-1,8)
plt.show()   
LagrangeInterp(xdata,ydata,3.5)