Polynomial Interpolation: Lagrange Interpolating Polynomials
Lagrange Interpolating Polynomials
Another equivalent method to find the interpolating polynomials is using the Lagrange Polynomials. Given
data points:
, then the Lagrange polynomial of degree
that fits through the
data points has the form:
![]()
where
![]()
In the following Mathematica code a Lagrange polynomial procedure is created to output the Lagrange polynomials.
View Mathematica CodeData = {{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]]
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:
![Rendered by QuickLaTeX.com \[\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} \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-42aa52be6aec9303aebcd4f875765139_l3.png)
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:
![Rendered by QuickLaTeX.com \[\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} \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-dcfb410585446daf2f95fd2028ac0638_l3.png)
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:
![Rendered by QuickLaTeX.com \[\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} \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-9e01d63ce3b6e26d4cea4d2e26c3f281_l3.png)
