PHN 311 Lectures On Interpolation
PHN 311 Lectures On Interpolation
Interpolation
What is Interpolation ?
2 http://numericalmethods.eng.usf.edu
Interpolants
Polynomials are the most common
choice of interpolants because they
are easy to:
Evaluate
Differentiate, and
Integrate.
3 http://numericalmethods.eng.usf.edu
Lagrangian Interpolation
where ‘ n ’ in f n (x) stands for the n th order polynomial that approximates the function y f (x)
4 http://numericalmethods.eng.usf.edu
Example-1
Find the root, correct to three decimal places and lying
between 0 and 0.5, of the equation
4e x sin x 1 0
Solution: Let
f x 4e x sin x 1 0
We have f 0 1 and f 0.5 0.163145
Therefore,
x1 0.25
x12 0.3705
xi f(xi)
Form formula
300
300, 2.4771 ,
2.4771
304 2.4829
305 2.4843
307 2.4871
Ln x
x x1 x x2 ....... x xn
y0
x x0 x x2 ....... x xn
y1 ........
x0 x1 x0 x2 ........ x0 xn x1 x0 x1 x2 ........ x1 xn
301 304 301 305 301 307 301 300 301 305 301 307
log10 301 2.4771 2.4829
300 304300 305 300 307 304 300 304 305 304 307
301 300 301 304 301 307 301 300 301 304 301 305
2.4843 2.4871
305 300305 304305 307 307 300 307 304 307 305
3 4 6 1 4 6
log10 301 2.4771 2.4829
4 5 7 4 1 3
1 3 6 1 3 4
2.4843 2.4871
51 2 7 3 2
2.4786
8
Find the Lagrange interpolating polynomial of degree 2
approximating the function y ln x defined by the following
table of values.
2 0.69315
2.5 0.91629
3.0 1.09861
In Hermite interpolation formula both the function and its first derivative values
are to be assigned at each point of interpolation. This is referred to as Hermite’s
interpolation formula.
Given the set of data points xi , yi , yi' , i 0,1,.....n it is required to
determine a polynomial of the least degree, say H 2 n 1 x
Such that
H 2 n 1 xi yi
H 2' n1 xi yi' (1)
i 0,1, 2,......n
x ai x bi
li x
2
ui
x ci x d i
li x
2
vi
ai xi bi 1
ci xi d i 0
ai 2li' xi 0
ci 1
From the last equation we get
ai 2li' xi
bi 1 2 xi li' xi
ci 1 and di xi
On substituting we get
vi x x xi
li x
2
(3)
Example-1
Example 1. Determine the Hermite polynomial of degree 5, which fits the following data
and hence find an approximate value of y ln 2.7 .
x y ln x y ' 1/ x
l0 x 2 x2 11x 15, l1 x 4 x 2 20 x 24 , l2 x 2 x 2 9 x 10
We therefore obtain
v1 x x 2.5 4 x 2 20 x 24 ,
2
u1 x 4 x 20 x 24 ,
2 2
v2 x x 3 2 x 2 9 x 10 ,
2
u2 x 19 6 x 2 x 9 x 10 ,
2 2
4 x 2 20 x 24 0.91629
19 6 x 2 x 2 9 x 10 1.09861
2
x 2 2 x 2 11x 15 0.5
2
x 2.5 4 x 2 20 x 24 0.4
2
x 3 2 x 2 9 x 10 0.33333
Putting x 2.7 and simplifying, we obtain
Which is correct to six decimal places. This is therefore a more accurate result than that
obtained by using the Lagrange interpolation formula.
Program for Hermite Method
Dimension x(50),f(50),fd(50),al(50),alp(50)
open(unit=5,file='lagr.in',status='unknown')
open(unit=6,file='lagr.res',status='unknown')
read(5,*)n,xx
Write(6,*)n,xx
read(5,*)(x(i),f(i),fd(i),i=1,n)
write(6,*)(x(i),f(i),fd(i),i=1,n)
call poly(x,f,n,xx,al,alp)
term=0.0
do 1 i=1,n
term1=(1-2*(xx-x(i))*alp(i))*al(i)*al(i)*f(i)
term2=(xx-x(i))*al(i)*al(i)*fd(i)
1 term=term+term1+term2
write(6,*) xx,term
close (unit=6)
close (unit=5)
Stop
end
20
Program continued
Subroutine poly(x,f,n,xx,p,pd)
Dimension p(50),x(50),f(50),pd(50),pp(50)
Do 10 j=1,n
P(j)=1.0
Do 11 i=1,n
If(i.eq.j)go to 11
p(j)=p(j)*(xx-x(i))/(x(j)-x(i))
11 continue
10 continue
Do 20 j=1,n
xxx= x(j)
Pp(j)=1.0
pd(j)=1.0
Do 112 i=1,n
If(i.eq.j)go to 15
pp(j)=pp(j)*(xxx-x(i))/(x(j)-x(i))
15 continue
ss=0.0
Do 13 i=1,n
If(i.eq.j)go to 13
ss=ss+1.0/(xxx-x(i))
13 continue
pd(j)=pp(j)*ss
20 continue
return
end
21
Program continued
Subroutine poly(x,f,n,xx,p,pd)
Dimension p(50),x(50),f(50),pd(50),pp(50)
Do 10 j=1,n
P(j)=1.0
Do 11 i=1,n
If(i.eq.j)go to 11
p(j)=p(j)*(xx-x(i))/(x(j)-x(i))
11 continue
10 continue
Do 20 j=1,n
xxx= x(j)
Pp(j)=1.0
pd(j)=1.0
Do 15 i=1,n
If(i.eq.j)go to 15
pp(j)=pp(j)*(xxx-x(i))/(x(j)-x(i))
15 continue
ss=0.0
Do 13 i=1,n
If(i.eq.j)go to 13
ss=ss+1.0/(xxx-x(i))
13 continue
pd(j)=pp(j)*ss
20 continue
return
end
22
Least Square Method
Introduction
In experimental work, we often encounter the problem of fitting
a curve to data which are subject to errors.
The curve drawn is such that the discrepancy between the data
points and the curve is least.
In this least squares method, the sum of the squares of the errors
is minimized.
24
The procedure for least square curve fitting
Let the set of data points be xi , yi , i 1, 2,3..., m .
Let the curve given by Y f x be fitted to this data.
At x xi , the given ordinate is yi and the corresponding
value of the fitting curve is f xi .
If ei is the error of approximation at x xi , then we have
ei yi f xi
If we write
S y1 f x1 y2 f x2 .... ym f xm
2 2 2
25
Fitting a straight line
Let Y a0 a1 x be the straight line to be fitted to the given data,
viz. xi , yi , i 1, 2,3..., m. Then, the corresponding equation, we
have,
S y1 a0 a1 x1 y2 a0 a1 x2 .... ym a0 a1 xm
2 2 2
26
And
a0 x1 x2 ... xm a1 x12 x22 ... xm2 x1 y1 x2 y2 .... xm ym
And then
a0 y a1 x
27
2S 2S
Since
a02
and a12
are both positive at the points a0 and a1 , it
follows that these values provide a minimum of S.
Sy S
cc
Sy
Where
m
S y yi y
2
i 1
X 1 2 3 4 5
Y 0.6 2.4 3.5 4.8 5.7
yi y yi a0 a1 xi
2 2
xi yi xi2 xi yi
16.10 0.2240
The correlation coefficient= 0.9930
16.10
30
Linearization of Nonlinear Laws
The data given may not always lie in a linear relationship form.
This can be ascertained from a plot of the given data.
If a nonlinear model is to be fitted, it can be conveniently
transformed to a linear relationship. This can be done by:
Case-I: xy a b
Taking log of both sides, we get
log10 x X log10 y Y
And
1 1
log10 b A0 and A1
a a
31
Then we get
Y A0 A1 X
Case-II:
y aebx
The above equation can be written as
ln y ln a bx
Y A0 A1 X
where
Y ln y A1 b
and
A0 ln a X x
32
Example-2
Using the method of least squares, find constants a and b such that
the function y aebx fits the following data:
We have,
y aebx
Therefore
ln y ln a bx
Y A0 A1 X
Where
Y ln y A1 b
and
A0 ln a X x
33
The table of values is given below
X Y=ln y X2 XY
1 0.905 1 0.905
3 1.905 9 5.715
5 2.905 25 14.525
7 3.905 49 27.335
9 4.905 81 44.145
25 14.525 165 92.625
We obtain
X 5 and Y 2.905
5 92.625 25 14.525
A1 0.5 b
5 165 625
34
Then
A0 Y A1 X 2.905 0.5 5 0.405
Hence,
a e A0 e0.405 1.499
It follows that the required curve I of the form
y 1.499e0.5 x
35
Curve Fitting by Polynomials
Let the polynomial of the nth degree,
Y a0 a1 x a2 x2 ...... an xn
Be fitted to the data points . We then have
S y1 a0 a1 x1 a2 x12 ..... an x1n y2 a0 a1 x2 a2 x22 ..... an x2n
2 2
Equating to zero the first partial derivatives and simplifying, we obtain the normal
derivatives:
ma0 a1 xi a2 xi2 ..... an xin yi
a0 xi a1 xi2 ..... an xin 1 xi yi
. . . .
. . . .
a0 xin a1 xin1 ..... an xi2 n xin yi
Example-3
Fit a polynomial of the second degree to the data points (x,y) given by.
X 0 1 2
Y 1 6 17
For n=2, the table is as follows:
X Y X2 X3 X4 XY X2Y
0 1 0. 0 0 0 0
1 6 1 1 1 6 6
2 17 4 8 16 34 68
3 24 5 9 17 40 74
37
The normal equations are
a0 1, a1 2 and a2 3
The required polynomial is given by
Y 1 2 x 3x 2
and it can be seen that this fitting is exact.
38
Multiple linear least squares
Suppose that z is a linear function of two variable x and y. If the
function z a0 a1 x a2 y is fitted to the data
z1 , x1 , y1 , z2 , x2 , y2 ....... zm , xm , ym
Then the sum
m
S zi a0 a1 xi a2 yi
2
i 1
S
2 zi a0 a1 xi a2 yi 0
a0
S
2 xi zi a0 a1 xi a2 yi 0
a1
S
2 yi zi a0 a1 xi a2 yi 0
a2
39
On simplifying, we get
ma0 a1 xi a2 yi zi
a0 xi a1 xi2 a2 xi yi zi xi
a0 yi a1 yi xi a2 yi2 zi yi
40
Example-4
Find the value of a0 , a1 and a2 , so that the function
z a0 a1 x a2 y
is fitted to the data x, y, z given below.
(0,0,2), (1,1,4), (2,3,3), (4,2,16) and (6,8,8)
X Y Z X2 XY ZX Y2 YZ
0 0 2 0 0 0 0 0
1 1 4 1 1 4 1 4
2 3 3 4 6 6 9 9
4 2 16 16 8 64 4 32
6 8 8 36 48 48 64 64
13 14 33 57 63 122 78 109
41
The normal equations are
a0 2 a1 5 a2 3
42