0% found this document useful (0 votes)
133 views42 pages

PHN 311 Lectures On Interpolation

The document discusses the Lagrange method of interpolation. It begins by defining interpolation as finding the value of a function y at a value of x that is not given, given a set of (x,y) data points. It states that polynomials are commonly used as interpolants because they are easy to evaluate, differentiate, and integrate. The Lagrangian interpolation polynomial is then defined using a weighting function Li(x). An example is provided to find the root of an equation using Lagrangian interpolation. The document provides additional examples calculating logarithm and exponential values through Lagrangian interpolation.

Uploaded by

Jay Rashamiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views42 pages

PHN 311 Lectures On Interpolation

The document discusses the Lagrange method of interpolation. It begins by defining interpolation as finding the value of a function y at a value of x that is not given, given a set of (x,y) data points. It states that polynomials are commonly used as interpolants because they are easy to evaluate, differentiate, and integrate. The Lagrangian interpolation polynomial is then defined using a weighting function Li(x). An example is provided to find the root of an equation using Lagrangian interpolation. The document provides additional examples calculating logarithm and exponential values through Lagrangian interpolation.

Uploaded by

Jay Rashamiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Lagrange Method of

Interpolation
What is Interpolation ?

Given (x0,y0), (x1,y1), …… (xn,yn), find the


value of ‘y’ at a value of ‘x’ that is not given.

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

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.

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

Since f  0.25  0.22929 , it follows that the root lies between


0.25 and 0.5.
Therefore,
0.75
x2   0.375
2
The successive approximation are given by

x3  0.3125 x4  0.3438 x5  0.3594

x6  0.3672 x7  0.3711 x8  0.3692

x9  0.3702 x10  0.3706 x11  0.3704

x12  0.3705

Hence the required root is 0.371, correct to three decimal


places.
Example-2
Certain corresponding values of x and log10 x are

xi f(xi)
Form formula
300
300, 2.4771 ,
2.4771

304 2.4829

305 2.4843
307 2.4871

Find the value of log10 301

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  304300  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  300305  304305  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
 51 2   7  3 2 

 1.2739  4.9658  4.4717  0.7106

 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

Hence determine the value of ln 2.7 .


We have
l0  x  
 x  2.5 x  3.0 
 2 x 2  11x  15
 0.5 1.0 
Similarly, we find
l1  x     4 x 2  20 x  24 
and
l2  x   2 x 2  9 x  10
Hence
L2  x    2 x 2  11x  15  0.69315   4 x 2  20 x  24   0.91629 
  2 x 2  9 x  10  1.09861

 0.08164 x2  0.81366 x  0.60761

Which is required quadratic polynomial.

Putting x  2.7, in the above polynomial, we obtain

ln 2.7  L2  2.7   0.08164  2.7   0.81366  2.7   0.60761  0.9941164


2

Actual value of ln 2.7  0.9932518, so that


Error  0.0008646
Program for Lagrange’s Method
Dimension p(50),x(50),f(50)
open(unit=5,file='lagr.in',status='unknown')
open(unit=6,file='lagr.res',status='unknown')
read(5,100)n,xx
Write(6,100)n,xx
read(5,101)(x(i),f(i),i=1,n)
write(6,101)(x(i),f(i),i=1,n)
100 format(I5, f10.4)
101 format(2f10.4)
Do 10 j=1,n
P(j)=1.0
Do 11 i=1,n
If(i-j)5,11,5
5 P(j)=p(j)*((xx-x(i))/(x(j)-x(i)))
11 continue
10 continue
Sum=0.0
Do 15 k=1,n
Sum=sum+p(k)*f(k)
15 continue
Write(6,102)xx,sum
102 format(2e13.4)
close(unit=6)
close(unit=5)
Stop
end
Hermite Method of
Interpolation
Hermite’s Interpolation Formula

 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' n1  xi   yi' (1)
i  0,1, 2,......n

We have here  2n  2  conditions and therefore the number of coefficients to


determined is  2n  2  and the degree of the polynomial is  2n  2 
 In analogy with the Lagrange interpolation formula, we seek a representation of
the form
n n
H 2 n 1  x    ui  x  yi  vi  x  yi'
i 0 i 0

Where ui  x  and vi  x  are polynomials in x of degree  2n  2  . Using equation


(1), we obtain
1  i  j
ui  x j    ; vi  x   0,
 0  i  j
ui'  x   0 for all i
1  i  j
vi'  x j   
0  i  j

Where ui  x  and vi  x  are polynomials in x of degree  2n  2  , we write


 x  x 
li  x  
2
ui  Ai 
 x  x 
li  x  
2
vi  Bi 

 x  ai x  bi  
li  x  
2
ui  
 x  ci x  d i  
li  x  
2
vi  

Using above conditions we obtain

 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

ui  x   1  2  x  xi  l  xi  li  x  


' 2
i
(2)

vi  x    x  xi  
li  x  
2

Then Hermite interpolation formula becomes


n n
H 2 n1  x    1  2  x  xi  l  xi   li  x   yi   x  xi  li  x  yi'
' 2 2
i
i 0 i 0

(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

2.0 0.69315 0.5


2.5 0.91629 0.4000
3.0 1.09861 0.33333
The polynomials li  x  have already been computed in Example 3. These are

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

l0'  x   4 x  11, l1'  x   8x  20, l2'  x   4 x  9


Hence
l0'  x0   3, l1'  x1   0, l2'  x2   3
Equations 2 give
v0  x    x  2   2 x 2  11x  15 ,
2
u0  x    6 x  11  2 x  11x  15 ,
2 2

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

Substituting these expressions in equation 3, we obtain the required Hermite polynomial

H 5  x    6 x  11  2 x 2  11x  15   0.069315 


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

ln  2.7   H 5  2.7   0.993252

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 strategy for such cases is to derive an approximating


function that broadly fits the data without necessarily passing
through the given points.

 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

S  e12  e22  ......  em2 ,


Then the method of least squares consists in minimizing S i.e., the
sum of squares of errors.

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

For S to be minimum, we have


S
 0  2  y1   a0  a1 x1    2  y2   a0  a1 x2   ....  2  ym   a0  a1 xm 
a0
And
S
 0  2 x1  y1   a0  a1 x1    2 x2  y2   a0  a1 x2   ....  2 xm  ym   a0  a1 xm 
a1

The above equations can be simplified to


ma0  a1  x1  x2  ...  xm   y1  y2  ....  ym

26
And
a0  x1  x2  ...  xm   a1  x12  x22  ...  xm2   x1 y1  x2 y2  ....  xm ym

The above equations can be written as


m m
ma0  a1  xi  yi
i 1 i 1
And
m m m
a0  xi  a1  x  xi yi 2
i
i 1 i 1 i 1

On solving these we get


m m m
m xi yi   xi . yi
a1  i 1 i 1 i 1
2
 m
m

m x    xi 
2
i
i 1  i 1 

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.

The last equation can be written as


y  a0  a1 x
Which shows that the fitted straight line passes through the centroid
of the data points.
The correlation coefficient (cc) can be defined as:

Sy  S
cc 
Sy

Where
m
S y    yi  y 
2

i 1

If cc is close to 1, then the fit is considered to be good.


28
Example-1
Find the best value of a0 and a1 if the straight line Y  a0  a1 x
is fitted to the data  xi , yi  :

X 1 2 3 4 5
Y 0.6 2.4 3.5 4.8 5.7

and also find the correlation coefficient.

 yi  y   yi  a0  a1 xi 
2 2
xi yi xi2 xi yi

1 0.6 1 0.6 7.84 0.0754


2 2.4 4 4.8 1.00 0.0676
3 3.5 9 10.5 0.01 0.0100
4 4.8 16 19.2 1.96 0.0196
5 5.7 25 28.5 5.29 0.0484
15 17.0 55 63.6 16.10 0.2240
29
From the table, we find x  3 and y  3.4 , then
5  63.5  15 17 
a1   1.26
5  55  225
Therefore,
a0  y  a1 x  0.38

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  a log10 y  log10 b


In this case ,we put

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:

X 1.0 3.0 5.0 7.0 9.0


Y 2.473 6.722 18.274 49.673 135.026

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

....   ym   a0  a1 xm  a2 xm2  .....  an xmn  


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  xin1  .....  an  xi2 n   xin yi

where the summations are performed from i=1 to i=m.


36
The Previous equation constitutes (n+1) equations in (n+1) unknowns, and hence
can be resolved by

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

3a0  3a1  5a2  24


3a0  5a1  9a2  40
5a0  9a1  17 a2  74
On solving the above equations, we obtain

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

should be minimum, for this, we have

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

From which a0 , a1 and a2 can be determined.

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)

We form the following table of values

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

5a0  13a1  14a2  33


13a0  57a1  63a2  122
14a0  63a1  78a2  109

On solving the above equations, we get

a0  2 a1  5 a2  3

42

You might also like