Professional Documents
Culture Documents
SKD 27&28 BSMRMU Lagrangian Interpolation
SKD 27&28 BSMRMU 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
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
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
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
j2
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.725C
The absolute relative approximate error a obtained between the results from the second and
= 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)
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?
22 Sujit K. Debsarma
Fortran95 Program & Results
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