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)} } \]
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) } \]
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} } \]
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)= \)
We are interested in estimating missing value at \( x = 0.9 \).
\( (a) \) Use \( x_3, x_4 \) to construct a degree one interpolation.
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.
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.
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)
# 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
scipy
to generate and plot the Lagrange interpolating polynomial.
Interpolated value at 3.5000 is 1.8944.
Interpolated value at 3.5000 is 2.6630.
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]
# 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)
# 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)