0% found this document useful (0 votes)
11 views

2024 Week 4 Interpolation - Jupyter Notebook

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

2024 Week 4 Interpolation - Jupyter Notebook

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

In [1]: 1 from sympy import *

2 x = symbols('x')

Interpolation is a method of : Estimating

Lagrange Interpolation Formula


Given 𝑛 known data points (𝑥0 , 𝑦0 ),(𝑥1 , 𝑦1 ),…,(𝑥𝑛−1 , 𝑦𝑛−1 ), the Lagrange interpolation formula is:
𝑛−1
𝑓𝑛 (𝑥) = ∑ 𝑦𝑗 𝐿𝑗 (𝑥)
𝑗=0
where each 𝐿𝑗 (𝑥) is the Lagrange basis polynomial given by:
𝐿𝑗 (𝑥) = ∏ 𝑥𝑥𝑗 −−𝑥𝑥𝑖𝑖
𝑛−1
𝑖=0
𝑖≠𝑗
Here:

𝑓𝑛 (𝑥) is the interpolating polynomial of degree 𝑛 − 1.


𝑦𝑗 are the values of the function at the known points 𝑥𝑗 .
𝐿𝑗 (𝑥) is the Lagrange basis polynomial for the 𝑗-th point, which is constructed in such a way that 𝐿𝑗 (𝑥𝑖 ) = 0 for all 𝑖 ≠ 𝑗 and 𝐿𝑗 (𝑥𝑗 ) = 1.
In [2]: 1 def lagrange_interpolation(data_points, x):
2 n = len(data_points)
3 interpolation_poly = 0
4 for i in range(n):
5 xi, yi = data_points[i]
6 term = yi
7 for j in range(n):
8 if i != j:
9 xj = data_points[j][0]
10 term *= (x - xj) / (xi - xj)
11 interpolation_poly += term
12 return interpolation_poly

What is the degree of polynomial for 𝑛 data point in lagrange interpolation?


𝑛−1
What is the error formula for the lagrangian interpolation of function 𝑓(𝑥) with 𝑛 data points, where 𝜁 is the intermediate point?
𝐸𝑛 = 𝑓(𝑥) − 𝑃𝑛 (𝑥) = 𝑓𝑛 (𝜁) Π(𝑥 − 𝑥 )
𝑛! 𝑖
Find 𝑓(7) by using Lagrange’s interpolation formula given the following data : 𝑓(1) = 4,𝑓(3) = 6,𝑓(5) = 12

x y

1 4

3 6

5 12

𝑓(𝑥) = 𝑦0 ⋅ (𝑥(𝑥−0−𝑥𝑥11)(𝑥−
)(𝑥0 −𝑥2 ) + 𝑦1 ⋅ (𝑥1 −𝑥0 )(𝑥1 −𝑥2 ) + 𝑦2 ⋅ (𝑥2 −𝑥0 )(𝑥2 −𝑥1 ) = 4 ⋅ (1−3)(1−5) + 6 ⋅ (3−1)(3−5) + 12 ⋅ (5−1)(5−3)
𝑥2 ) (𝑥−𝑥0 )(𝑥−𝑥2 ) (𝑥−𝑥0 )(𝑥−𝑥1 ) (𝑥−3)(𝑥−5) (𝑥−1)(𝑥−5) (𝑥−1)(𝑥−3)

𝑓(7) = 4 ⋅ (7−3)(7−5)
(1−3)(1−5) + 6 ⋅ (3−1)(3−5) + 12 ⋅ (5−1)(5−3) = 4 − 18 + 36 = 22
(7−1)(7−5) (7−1)(7−3)

In [3]: 1 data_points = [(1, 4), (3, 6), (5, 12)]


2 polynomial = lagrange_interpolation(data_points, x)
3 f_7 = polynomial.subs(x, 7) # Evaluate the polynomial at x = 7
4 simplify(f_7)

Out[3]: 22
The lagrange polynomial that passes through the 3 data points is given by 𝑓(15) = 24,𝑓(18) = 37, and 𝑓(22) = 25.
𝑓2 (𝑥) = 𝐿0 (𝑥)(24) + 𝐿1 (𝑥)(37) + 𝐿2 (𝑥)(25) . What is the value of 𝐿1 (𝑥) at 𝑥 = 16?
x y

15 24

18 37

22 25

𝑓2 (𝑥) = 𝑦0 ⋅ (𝑥(𝑥−0−𝑥𝑥11)(𝑥−
)(𝑥0 −𝑥2 ) + 𝑦1 ⋅ (𝑥1 −𝑥0 )(𝑥1 −𝑥2 ) + 𝑦2 ⋅ (𝑥2 −𝑥0 )(𝑥2 −𝑥1 ) = 𝐿0 (𝑥)𝑦0 + 𝐿1 (𝑥)𝑦1 + 𝐿2 (𝑥)𝑦2
𝑥2 ) (𝑥−𝑥0 )(𝑥−𝑥2 ) (𝑥−𝑥0 )(𝑥−𝑥1 )

𝐿1 (𝑥) = (𝑥(𝑥−1−𝑥𝑥00)(𝑥−
)(𝑥1 −𝑥2 ) = (18−15)(18−22) = −12
𝑥2 ) (𝑥−15)(𝑥−22) (𝑥2 −37𝑥+330)

𝐿1 (16) = 162−37∗16+330
−12 = −12−6 = 1
2
In [4]: 1 x0, x1, x2 = 15, 18, 22
2 x_val = 16
3 L1_x = ((x_val - x0) * (x_val - x2)) / ((x1 - x0) * (x1 - x2))
4 L1_x

Out[4]: 0.5
Find 𝑓(3) by using Lagrange interpolation formula 𝑓(0) = 2,𝑓(1) = 3,𝑓(2) = 12,𝑓(5) = 147
x y

0 2

1 3

2 12

5 147

𝑓(𝑥) = 𝑦0 ⋅ (𝑥(𝑥0 −−𝑥𝑥11)()(𝑥𝑥0 −−𝑥𝑥22)(𝑥 − 𝑥3 ) (𝑥 − 𝑥0 )(𝑥 − 𝑥2 )(𝑥 − 𝑥3 ) (𝑥 − 𝑥0 )(𝑥 − 𝑥1 )(𝑥 − 𝑥3 ) (𝑥 − 𝑥0 )(𝑥 − 𝑥1 )(𝑥 − 𝑥2 )
)(𝑥0 − 𝑥3 ) + 𝑦1 ⋅ (𝑥1 − 𝑥0 )(𝑥1 − 𝑥2 )(𝑥1 − 𝑥3 ) + 𝑦2 ⋅ (𝑥2 − 𝑥0 )(𝑥2 − 𝑥1 )(𝑥2 − 𝑥3 ) + 𝑦3 ⋅ (𝑥3 − 𝑥0 )(𝑥3 − 𝑥1 )(𝑥3 − 𝑥2 )
= 𝑦0 ⋅ 𝐿0 (𝑥) + 𝑦1 ⋅ 𝐿1 (𝑥) + 𝑦2 ⋅ 𝐿2 (𝑥) + 𝑦3 ⋅ 𝐿3 (𝑥)
𝐿0 (𝑥) = (𝑥−1)(𝑥−2)(𝑥−5)
(0−1)(0−2)(0−5) =
(𝑥−1)(𝑥−2)(𝑥−5) => 𝐿 (3) = (3−1)(3−2)(3−5) = (2)(1)(−2) = −0.4
10 0 10 10
𝐿1 (𝑥) = (1−0)(1−2)(1−5) = 4
(𝑥−0)(𝑥−2)(𝑥−5) (𝑥)(𝑥−2)(𝑥−5) => 𝐿1 (3) = 4 = 4 = −1.5
(3)(3−2)(3−5) (3)(1)(−2)

𝐿2 (𝑥) = (𝑥−0)(𝑥−1)(𝑥−5)
(2−0)(2−1)(2−5) =
(𝑥)(𝑥−1)(𝑥−5) => 𝐿 (3) = (3)(3−1)(3−5) = (3)(2)(−2) = 2
−6 2 −6 −6
𝐿3 (𝑥) = (5−0)(5−1)(5−2) = 60
(𝑥−0)(𝑥−1)(𝑥−2) (𝑥)(𝑥−1)(𝑥−2) => 𝐿3 (3) = 60 = 60 = 0.1
(3)(3−1)(3−2) (3)(2)(1)

𝑓(3) = 2 ⋅ (−0.4) + 3 ⋅ (−1.5) + 12 ⋅ (2) + 147 ⋅ (0.1) = −0.8 + (−4.5) + 24 + 14.7 = 33.4 + 1.6 = 35
In [8]: 1 data_points = [(0, 2), (1, 3), (2, 12), (5, 147)]
2 polynomial = lagrange_interpolation(data_points, x)
3 f_3 = polynomial.subs(x, 3)# Evaluate the polynomial at x = 3
4 simplify(f_3)

Out[8]: 35
Find the polynomial for the following data by using lagrange interpolation formula

𝑥 = 0,𝑓(𝑥) = 5
𝑥 = 2,𝑓(𝑥) = 25
𝑥 = 5,𝑓(𝑥) = 100
x y

0 5

2 25

5 100

𝑓(𝑥) = 𝑦0 ⋅ (𝑥(𝑥0 −− 𝑥𝑥11)()(𝑥𝑥0−−𝑥𝑥22)) + 𝑦1 ⋅ (𝑥(𝑥1 −− 𝑥𝑥00)()(𝑥𝑥1−−𝑥𝑥22)) + 𝑦2 ⋅ (𝑥(𝑥2 −− 𝑥𝑥00)()(𝑥𝑥2−−𝑥𝑥11))


= 5 ⋅ (𝑥(0 −− 2)(0
2)(𝑥 − 5) + 25 ⋅ (𝑥 − 0)(𝑥 − 5) + 100 ⋅ (𝑥 − 0)(𝑥 − 2)
− 5) (2 − 0)(2 − 5) (5 − 0)(5 − 2)
( 𝑥 2 − 7𝑥 + 10)
= 5 ⋅ 10 + 25 ⋅ −6 + 100 ⋅ 15 ( 𝑥2 − 5𝑥) ( 𝑥2 − 2𝑥)

= 15 ⋅ (𝑥 − 7𝑥 + 10) − 125 ⋅30(𝑥 − 5𝑥) + 200 ⋅ (𝑥 − 2𝑥)


2 2 2

= 90𝑥 + 120𝑥 + 150


2
30
= 3𝑥2 + 4𝑥 + 5
In [7]: 1 data_points = [(0, 5), (2, 25), (5, 100)]
2 polynomial = lagrange_interpolation(data_points, x)
3 simplify(polynomial)

Out[7]: 3𝑥2 + 4𝑥 + 5

You might also like