Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Numerical Interpolation

(Linear, Quadratic & Lagrangian Interpolation)

OCN-3205
(Computer Programming for Oceanographers)
Lecture - 27 & 28
Sujit Kumar Debsarma
SAARC Laureate (1995, Meteorology & Environment)
Visiting Professor, BSMRMU & DU
Former Scientist, SMRC
Former Pr. Meteorologist & System Manager, BMD
E-mail: sujit.ocn@bsmrmu.edu.bd, sujit.debsarma@gmail.com
Cell: +88 01911 025955, +88 01818 383404

1
What is Interpolation?
Interpolation is the calculation of the value of a function
between the values already known. Figuring, reckoning,
calculation, or computation - problem solving involve
numbers or quantities.
Given (x0, y0), (x1, y1), …, (xn, yn), find the value of ‘y’ at
a value of ‘x’ that is not given.

2 Sujit K. Debsarma
Interpolants

Polynomials are the most common choice of


interpolants because they are easy to:

Evaluate

Differentiate, and

Integrate.

3 Sujit K. Debsarma
(𝑥𝑛 , 𝑦𝑛 )
Linear Interpolation (𝑥1 , 𝑦1 )
(𝑦−𝑦0 ) (𝑦 −𝑦 ) (𝑦 −𝑦 )
Tangent = = 1 0 = 2 1 (𝑥0 , 𝑦0 ) (𝑥, 𝑦)
(𝑥−𝑥0 ) (𝑥1 −𝑦0 ) (𝑥2 −𝑦1 )

𝑦 − 𝑦0
𝑥 − 𝑥0

To interpolate value of dependent variable y at some point of


independent variable x using Linear Interpolation, we take
two points i.e. if we need to interpolate y corresponding to x
which lies between x0 and x1 then we take two points [x0, y0]
and [x1, y1] and construct Linear Interpolant which is the
straight line between these points i.e.
y - y0 = ((y1 - y0)/(x1 - x0)) * (x - x0)

Similarly, the Linear Interpolant (straight line) through two


points [x1, y1] and [x2, y2] is
y - y1 = ((y2 - y1)/(x2 - x1)) * (x - x1)
4 Sujit K. Debsarma
Algorithm For Linear Interpolation Method

1. Start
2. Read data points (x0, y0) and (x1, y1).
3. Read value of independent variables say, 𝑥𝑝
whose corresponding value of dependent say
𝑦𝑝 is to be determined.
4. Calculate
𝑦𝑝 = y0 + ((y1 - y0)/(x1 - x0)) * (𝑥𝑝 - x0)
1. Display value of 𝑦𝑝 as interpolated value.
2. Stop

5 Sujit K. Debsarma
Fortran95 Program of Linear Interpolation
program LinInterp Try with (4,1) & (8,3)
implicit none at xp = 6
real :: x0, y0, x1, y1, xp, yp ANS: yp= 2.0
!Inputs
print *,"Enter first point (x0,y0):"
read *, x0, y0
print *, "Enter second point: (x1,y1):"
read *, x1, y1
print *, "Enter interpolation point: (xp)"
read *, xp
! Calculation
yp = y0 + ((y1-y0)/(x1-x0)) * (xp - x0)
print *,"Interpolated value at ", xp, "is = ", yp
end program LinInterp
Sujit Kumar Debsarma
6
7 Sujit K. Debsarma
8 Sujit K. Debsarma
9 Sujit K. Debsarma
10 Sujit K. Debsarma
11 Sujit K. Debsarma
12 Sujit K. Debsarma
13 Sujit K. Debsarma
Cubic Interpolation
For the third order polynomial (also called cubic interpolation), we choose the
temperature given by
3
T ( z ) =  Li ( z )T ( z i )
i =0

= L0 ( z )T ( z 0 ) + L1 ( z )T ( z1 ) + L2 ( z )T ( z 2 ) + L3 ( z )T ( z 3 )

20
19.19774

18

16
ys

f ( range)
14
(
f x desired )
12

10

9.44745 8
9 8.5 8 7.5 7 6.5 6
−9 x s  range x desired −6
14 Sujit K. Debsarma
Cubic Interpolation (contd.)
z o = −9, T (z o ) = 9.9 z1 = −8, T (z1 ) = 11.7

z 2 = −7, T (z 2 ) = 17.6 z 3 = −6, T (z 3 ) = 18.2

3 z − zj  z − z1  z − z 2  z − z 3 
L0 ( z ) =  =    
j =0 z 0 − z j  z 0 − z1  z 0 − z 2  0z − z 3  19.19774
20
j 0

z − zj  z − z 0  z − z 2  z − z 3 
18
3
L1 ( z ) =  =    
z
j =0 1 − z j  1
z − z 0  1 z − z 2  1 z − z 3  16
j 1 ys

f ( range)
3 z − zj  z − z 0  z − z1  z − z 3 
14

L2 ( z ) =  =     ( )
f x desired

z
j =0 2 − z j  2
z − z 0  2 z − z1  2 z − z 3  12
j2

3 z − zj  z − z 0  z − z1  z − z 2  10

L3 ( z ) =  =    
z
j =0 3 − z j  3
z − z 0  3 z − z1  3 z − z 2  9.44745 8
j 3 9 8.5 8 7.5 7 6.5 6
−9 x s  range x desired −6

15 Sujit K. Debsarma
Cubic Interpolation (contd.)
 z − z1  z − z 2  z − z3   z − z0  z − z 2  z − z3 
T ( z ) =    T ( z0 ) +    T (z1 )
 z0 − z1  z0 − z 2  z0 − z3   z1 − z0  z1 − z 2  z1 − z3  z 0  z  z3
 z − z0  z − z1  z − z3   z − z0  z − z1  z − z 2 
+    T ( z 2 ) +    T ( z3 )
 z 2 − z0  z0 − z1  z0 − z3   z3 − z0  z3 − z1  z3 − z 2 

T (− 7.5) =
(− 7.5 + 8)(− 7.5 + 7 )(− 7.5 + 6) (9.9) + (− 7.5 + 9)(− 7.5 + 7 )(− 7.5 + 6) (11.7 )
(− 9 + 8)(− 9 + 7 )(− 9 + 6) (− 8 + 9)(− 8 + 7 )(− 8 + 6)
+
(− 7.5 + 9)(− 7.5 + 8)(− 7.5 + 6) (17.6) + (− 7.5 + 9)(− 7.5 + 8)(− 7.5 + 7 ) (18.2)
(− 7 + 9)(− 7 + 8)(− 7 + 6) (− 6 + 9)(− 6 + 8)(− 6 + 7 )
= (− 0.0625)(9.9) + (0.5625)(11.7) + (0.5625)(17.6) + ( − 0.0625)(18.2)

= 14.725C
The absolute relative approximate error a obtained between the results from the second and

third order polynomial is


14.725 − 14.138
a =  100
14.725

= 3.9898%
16 Sujit K. Debsarma
Thermocline
What is the value of depth at which the thermocline exists?
d 2T
The position where the thermocline exists is given where 2
=0
dz
T (z ) =
(z + 8)(z + 7 )(z + 6) (9.9) + (z + 9)(z + 7 )(z + 6) (11.7 )
(− 9 + 8)(− 9 + 7 )(− 9 + 6) (− 8 + 9)(− 8 + 7 )(− 8 + 6)
+
(z + 9)(z + 8)(z + 6) (17.6) + (z + 9)(z + 8)(z + 7 ) (18.2)
(− 7 + 9)(− 7 + 8)(− 7 + 6) (− 6 + 9)(− 6 + 8)(− 6 + 7 )
= −615.9 − 262.58 z − 35.55 z 2 − 1.5667 z 3 , − 9  z  −6
dT
= 262.58 − 71.1z − 4.7 z 2 , − 9  z  −6
dz
d 2T
= −71.1 − 9.4 z , − 9  z  −6
dz 2
Simply setting this expression equal to zero, we get
0 = −71.1 − 9.4 z , − 9  z  −6
z = −7.5638 m
17 Sujit K. Debsarma
Lagrange Method of
Interpolation

Sujit K. Debsarma
Lagrangian Interpolation
Lagrangian interpolating polynomial is given by
n
f n ( x) =  Li ( x) f ( xi )
i =0

where ‘ n ’ in f n (x) stands for the n th order polynomial that approximates the function y = f (x)

given at (n + 1) data points as ( x0 , y 0 ), (x1 , y1 ),......, ( x n −1 , y n −1 ), ( x n , y n ) , and


n x − xj
Li ( x) = 
j =0 xi − x j
j i

Li (x) is a weighting function that includes a product of (n − 1) terms with terms of j = i


omitted.

19 Sujit K. Debsarma
For example, in the given table we’re given 4 set of discrete data
points, for an unknown function f(x) :

How to find?

Here we can apply the Lagrange’s interpolation formula to get our


solution.
The Lagrange’s Interpolation formula:
If y = f(x) takes the values y0, y1, … , yn corresponding to x = x0,
x1 , … , xn then,

This method is preferred over its counterparts like Newton’s method


because it is applicable even for unequally spaced values of x.
20 Sujit K. Debsarma
Interpolation of data 𝑓(𝑥𝑖) where 𝑖=1, 2, …, 𝑁 (𝑁 is the
number of data) means that on each 𝑥𝑖 the properties will be
𝑝(𝑥𝑖)=𝑓(𝑥𝑖) where 𝑝(𝑥𝑖) is the interpolation function based on
data of 𝑓(𝑥𝑖).
21 Sujit K. Debsarma
LAGRANGE POLYNOMIAL
Here, we will apply 𝑝(𝑥) as a polynomial (i.e. Lagrange
polynomial). For N data, we will have polynomial of order
𝑁−1.
𝑝(𝑥)=𝑎 +𝑎 𝑥+𝑎 𝑥 +⋯+𝑎 𝑥 ≃𝑓(𝑥)
0 1 2
2
𝑁−1
𝑁−1

The value of each coefficient 𝑎 (𝑖=0, 1, …, 𝑁−1) can be deducted


𝑖

from 𝑝(𝑥 )=𝑓(𝑥 ) (𝑖=1, 2, …, 𝑁).


𝑖 𝑖

Thus, we can get a set of linear equations:


𝑝(𝑥1) = 𝑎0 + 𝑎1𝑥1 + 𝑎2𝑥1 2 + ⋯ + 𝑎𝑁−1𝑥1𝑁−1 = 𝑓(𝑥1)
𝑝(𝑥2) = 𝑎0 + 𝑎1𝑥2 + 𝑎2𝑥2 2 + ⋯ + 𝑎𝑁−1𝑥2𝑁−1 = 𝑓(𝑥2)
𝑝(𝑥3) = 𝑎0 + 𝑎1𝑥3 + 𝑎2𝑥3 2 + ⋯ + 𝑎𝑁−1𝑥3𝑁−1 = 𝑓(𝑥3)

𝑝(𝑥𝑁) = 𝑎0 + 𝑎1𝑥𝑁 + 𝑎2𝑥𝑁 2 + ⋯ + 𝑎𝑁−1𝑥𝑁𝑁−1 = 𝑓(𝑥𝑁)

22 Sujit K. Debsarma
Fortran95 Program & Results

INPUT FILE: Lagrange.dat


5
1980 440
1985 510
1990 525
2000 571
2005 500

23 Sujit K. Debsarma
A Practical Example
To maximize a catch of fish in a lake, it is suggested to throw the
line to the depth of the thermocline. The characteristic feature of
this area is the sudden change in temperature. We are given the
temperature vs. depth plot for a lake. Determine the value of
the temperature at z = −7.5 using the Lagrangian method for
quadratic interpolation.
Temperature Depth
T (oC) z (m)
19.1 0
19.1 -1
19 -2
18.8 -3
18.7 -4
18.3 -5
18.2 -6
17.6 -7
11.7 -8
9.9 -9
9.1 -10
Temperature vs. depth of a lake
24 Sujit K. Debsarma
PROGRAM LagrangeInterpolation
! ========== Program by Prof. S. K. Debsarma ================
! *** Lagrange Polynomials to interpolate a data ***
! Variables and parameters used in the program:
!N = Number of data
! xdata = Data of x axis
! fdata = The value of data
!x = The x point of data to interpolate
!p = Lagrange polynomial result
! i, j = Integer for counting
! ===============================================
IMPLICIT NONE
DOUBLE PRECISION, ALLOCATABLE :: xdata(:), fdata(:)
DOUBLE PRECISION :: x, p
INTEGER :: i, N, OUTPUT_DATA_NO = 500
OPEN(unit = 10, FILE = ’Lagrange.dat’)
OPEN(unit = 11, FILE = ’outLagrange.dat’)

READ(10,*) N
ALLOCATE (xdata(N))
ALLOCATE (fdata(N))

25 Sujit K. Debsarma
DO i=1, N
READ(10,*) xdata(i), fdata(i)
END DO
CLOSE (10)
DO i=1, OUTPUT_DATA_NO
x=xdata(1) + (i-1)*(xdata(N) – xdata(1)/ real(OUTPUT_DATA_NO - 1)
CALL Lagrange(N, xdata, fdata, x, p)
WRITE(11,*) x, p !Write to File UNIT = 11
WRITE(*,*) x, p !Write to Screen
END DO
WRITE(*,*) 'OUTPUT FILE "OUTLagrange.dat" is created.’
CLOSE(11)
DEALLOCATE(fdata)
DEALLOCATE(xdata)
STOP

CONTAINS
SUBROUTINE Lagrange(N, xdata, fdata, x, output)
IMPLICIT NONE
INTEGER, INTENT (IN) :: N
DOUBLE PRECISION, DIMENSION(N), INTENT(IN) :: xdata, fdata
DOUBLE PRECISION, INTENT (IN) :: x
26 Sujit K. Debsarma
DOUBLE PRECISION, INTENT (OUT) :: output
DOUBLE PRECISION :: L
INTEGER :: i, j
output = 0.0
DO I = 1, N
L = 1.0
DO j = 1, N
IF (I == j) THEN
L = L * 1.0
ELSE
L = L * (x - xdata(j)) / real(xdata(i) - xdata(j))
END IF
END DO ! j loop
output = output + L * fdata(i)
END DO ! i loop
RETURN
END SUBROUTINE Lagrange
END PROGRAM LagrangeInterpolation

27 Sujit K. Debsarma

You might also like