Data interpolation in Python
Dr. Santosh Prasad Gupta
Assistant Professor
Department of Physics
Patna University, Patna
6/30/2021 Department of Physics, PU: SP Gupta 1
In this document we learn about:
Basic introduction to Interpolation
Lagrange Interpolation formula
Lagrange Interpolation formula in python using for loop
Lagrange Interpolation formula in python using numpy and scipy
6/30/2021 Department of Physics, PU: SP Gupta 2
Interpolation
Interpolation is the process of deriving a simple function from a set of
discrete data points so that the function passes through all the given
data points (i.e. reproduces the data points exactly) and can be used to
estimate data points in-between the given ones.
Interpolation is also used to simplify complicated functions by sampling
data points and interpolating them using a simpler function.
Polynomials are commonly used for interpolation because they are
easier to evaluate, differentiate, and integrate - known as polynomial
interpolation
x sin(x)
Interpolation was used for long time to 0 0.0000
provide an estimate of a tabulated 0.1 0.0998
function at values that are not available
0.2 0.1987
in the table.
0.3 0.2955
0.4 0.3894
Using Lagrange interpolation sin (0.15) ≈ 0.1494468
True value (4 decimal digits) sin (0.15) = 0.1494
6/30/2021 Department of Physics, PU: SP Gupta 3
Interpolation problem
Given a set of n+1 points: x0 , f ( x0 ) , x1, f ( x1 ) , ...., xn , f ( xn )
Find an nth order polynomial 𝒇𝒏 𝒙 that passes through all points, such
that:
f n ( xi ) f ( xi ) for i 0,1, 2,..., n
Example: Linear Interpolation Quadratic Interpolation
Given any two points, there is one Given any three points there is one
polynomial of order ≤ 1 that polynomial of order ≤ 2 that
passes through the two points. passes through the three points.
6/30/2021 Department of Physics, PU: SP Gupta 4
Linear Interpolation
Given any two points, x0 , f ( x0 ) , x1, f ( x1 )
The line that interpolates the two points is:
f ( x1 ) f ( x0 )
f1 ( x) f ( x0 ) x x0
x1 x0
Example :
Find a polynomial that interpolates (1,2) and (2,4).
42
f1 ( x) 2 x 1 2 x
2 1
6/30/2021 Department of Physics, PU: SP Gupta 5
Quadratic Interpolation
Given any three points: x0 , f ( x0 ), x1, f ( x1 ), and x2 , f ( x2 )
The polynomial that interpolates the three points is:
f 2 ( x ) b0 b1 x x0 b2 x x0 x x1
where :
b0 f ( x0 )
f ( x1 ) f ( x0 )
b1 f [ x0 , x1 ]
x1 x0
f ( x2 ) f ( x1 ) f ( x1 ) f ( x0 )
x2 x1 x1 x0
b2 f [ x0 , x1 , x2 ]
x2 x0
6/30/2021 Department of Physics, PU: SP Gupta 6
Lagrange Interpolation
Problem: xi x0 x1 …. xn
Given
yi y0 y1 …. yn
Find the polynomial of least order such that:
f n (x)
f n ( xi ) f ( xi ) for i 0,1,..., n
Lagrange Interpolation Formula: n
f n ( x) f xi i ( x)
i 0
x x
n
i ( x) x x
j 0, j i
j
i j
Department of Physics,
6/30/2021 7
PU: SP Gupta
i ( x) are called the cardinals.
The cardinals are n th order polynomials :
0 i j
i (x j )
Lagrange Interpolation: 1 i j
Example
P2 ( x) f ( x0 ) 0 ( x) f ( x1 ) 1 ( x) f ( x2 ) 2 ( x)
0 ( x)
x x1 x x2
x 1 / 4 x 1
x0 x1 x0 x2 1 / 3 1 / 4 1 / 3 1
1 ( x)
x x0 x x2 x 1 / 3 x 1
x1 x0 x1 x2 1 / 4 1 / 3 1 / 4 1 x 1/3 1/4 1
2 ( x)
x x0 x x1 x 1 / 3 x 1 / 4
y 2 -1 7
x2 x0 x2 x1 1 1 / 3 1 1 / 4
P2 ( x) 2 18( x 1 / 4)( x 1) 116( x 1 / 3)( x 1)
72( x 1 / 3)( x 1 / 4)
6/30/2021 Department of Physics, PU: SP Gupta 8
Lagrange Interpolation using python
Find a polynomial to interpolate using Lagrange
interpolation method
The required polynomial can be written as
x y
4
f4 ( x) f ( xi ) i 0 31 2 2 5 3 4 4 0 1
1 3
i 0
( x 1) ( x 2) ( x 3) ( x 4) ( x 1)( x 2)( x 3)( x 4) 2 2
0
(0 1) ( 0 2) (0 3) ( 0 4) 24 3 5
( x 0) ( x 2) ( x 3) ( x 4) x ( x 2)( x 3)( x 4) 4 4
1
(1 0) (1 2) (1 3) (1 4) 6
( x 0) ( x 1) ( x 3) ( x 4) x ( x 1)( x 3)( x 4)
2
( 2 0) ( 2 1) ( 2 3) ( 2 4) 4
( x 0) ( x 1) ( x 2) ( x 4) x ( x 1)( x 2)( x 4)
3
( 3 0) (3 1) ( 3 2) ( 3 4) 6
( x 0) ( x 1) ( x 2) ( x 3) x ( x 1)( x 2)( x 3)
4
( 4 0) ( 4 1) ( 4 2) ( 4 3) 24
6/30/2021 Department of Physics, PU: SP Gupta 9
Lagrange Interpolation: python script using for loop
#Lagrange Interpolation : python script
#Defining the Lagrange Interpolation as LagIntp
def LagIntp(x, y, xp): #xp the x value where yp has to calculated
m = len(x)
n = len(y)
assert m==n
L=0
l = [1]*n
for i in range(n):
for j in range(n):
if j != i:
l[i]*=(xp-x[j])/(x[i]-x[j])
L = L + y[i]*l[i]
return L
#Enter the x values
x=eval(input('Enter the x values:'))
#Enter the corresponding y values
y=eval(input('Enter the y values:'))
#Enter the xp value where corresponding yp required to calculate
xp=eval(input('Enter the xp value:'))
print('Value of yp at xp from interpolation:', LagIntp(x, y, xp))
6/30/2021 Department of Physics, PU: SP Gupta 10
Lagrange Interpolation: After running the previous script
Enter the x values:[0,1,2,3,4]
Enter the y values:[1,3,2,5,4] x y
Enter the xp value:1.5 0 1
Value of yp at xp from interpolation: 2.08593
1 3
2 2
Enter the x values:[0,1,2,3,4]
Enter the y values:[1,3,2,5,4] 3 5
Enter the xp value:1.1 4 4
Value of yp at xp from interpolation: 2.80193
x sin(x)
0 0.0000
Enter the x values:[0,0.1,0.2,0.3,0.4]
Enter the y values:[0.0000,0.0998,0.1987,0.2955,0.3894]
0.1 0.0998
Enter the xp value:0.15 0.2 0.1987
Value of yp at xp from interpolation: 0.149446874999999 0.3 0.2955
0.4 0.3894
6/30/2021 Department of Physics, PU: SP Gupta 11
Lagrange Interpolation: python script using numpy and scipy
#Lagrange Interpolation: python script using numpy and scipy
import numpy as np
from [Link] import lagrange
#Enter the x values
x=eval(input('Enter the x values:'))
#Enter the corresponding y values
y=eval(input('Enter the y values:'))
#Enter the xp value where corresponding yp required to calculate
xp=eval(input('Enter the xp values:'))
#Interpolating as f using the function lagrange
f = lagrange(x, y)
print('The interpolated values of yp:\n', f(xp))
Entering few values of xp random manner
Enter the x values:[0,1,2,3,4]
Enter the y values:[1,3,2,5,4]
Enter the xp values:[0.5,1.5,2.5,3.5]
The interpolated values of yp:
[3.3984375 2.0859375 3.1484375 6.0859375]
6/30/2021 Department of Physics, PU: SP Gupta 12
Entering the value of xp using [Link]() or [Link]()
Enter the x values:[0,1,2,3,4]
Enter the y values:[1,3,2,5,4]
Enter the xp values:[Link](-0.5,5,0.5)
The interpolated values of yp:
[-7.4140625 1. 3.3984375 3. 2.0859375 2.
3.1484375 5. 6.0859375 4. -4.6015625]]
Enter the x values:[0,1,2,3,4]
Enter the y values:[1,3,2,5,4]
Enter the xp values:[Link](-0.5,5,20)
The interpolated values of yp:
[ -7.4140625 -1.59096385 1.68496078 3.15321399 3.44797432
3.09809624 2.52711016 2.05322243 1.88931531 2.14294703
2.81635173 3.80643948 4.90479632 5.79768418 6.06604096
5.18548047 2.52629247 -2.64655735 -11.17342736 -24. ]
6/30/2021 Department of Physics, PU: SP Gupta 13
Lagrange Interpolation python script using numpy and scipy and
visualizing the data and interpolated data
#Lagrange Interpolation: python script using numpy and scipy
import numpy as np
from [Link] import lagrange
#Enter the x values
x=eval(input('Enter the x values:'))
#Enter the corresponding y values
y=eval(input('Enter the y values:'))
#Enter the xp value where corresponding yp required to calculate
xp=eval(input('Enter the xp values:'))
#Interpolating as f using the function lagrange
f = lagrange(x, y)
print('The interpolated values of yp:\n', f(xp))
#visualizing the data points and interpolated polynomial
fig = [Link](figsize = (10,8))
#Plotting the interpolated data as continuous blue line and
# x, y data as red filled circle
[Link](xp, f(xp), 'b', x, y, 'ro')
[Link]('Lagrange Polynomial')
[Link]()
[Link]('x')
[Link]('y')
[Link]()
6/30/2021 Department of Physics, PU: SP Gupta 14
After running the previous script, we have the following output
Enter the x values:[0,1,2,3,4]
Enter the y values:[1,3,2,5,4]
Enter the xp values:[Link](-1,5,30)
The interpolated values of yp:
[-26. -16.77018045 -9.76275766 -4.62484783 -1.03105272
1.31654038 2.68835866 3.32734373 3.44895169 3.24115309
2.86443295 2.45179073 2.10874037 1.91331027 1.91604327
2.13999669 2.58074231 3.20636635 3.95746952 4.74716697
5.46108831 5.95737762 6.06669344 5.59220875 4.30961103
1.96710218 -1.71460141 -7.04226891 -14.35015503 -24. ]
6/30/2021 Department of Physics, PU: SP Gupta 15
Assignments
Question 1: A set of x values and corresponding y values are as follows.
x=0.1, 0.2, 0.5, 0.9, 1.3, 1.7 and y = 10, 15, 19, 17, 12, 11. Find the value of y
at x=0.7
Question 2: A set of x values and corresponding y values are as follows.
x=1, 2, 7, 11, 13, 17 and y = 11, 13, 19, 31, 51, 91. Find the values of y at x=0,
1.5, 5, 9, 12,13.2, 15, 16, 17.2, 19 and also plot these interpolated values along
with the original data. The original data should be in blue color and in diamond
shape whereas the interpolated values should be red color continuous line
Question 3: A set of x values and corresponding y values are as follows.
x=1, 2, 3, 4, 5, 6 and y = 1, 4, 9, 16, 25, 36. For the x form 0 to 7 with step size
0.3, find the corresponding y values and also plot these interpolated values
along with the original data. The original data should be in green color and in
triangle shape whereas the interpolated values should be orange color
continuous line
6/30/2021 Department of Physics, PU: SP Gupta 16