Lecture Notes 4-Interpolation
Lecture Notes 4-Interpolation
MATHEMATICS
in MECHANICAL
ENGINEERING
Lecture notes
Ha Le Nhu Ngoc Thanh, Ph.D.
Chapter 4:
INTERPOLATION AND POLYNOMIAL APPROXIMATION
1. Introduction
2. Lagrange interpolating
3. Newton interpolating
4. Spline Interpolation
5. Least Square Method (Reference)
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 2 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
INTRODUCTION
Polynomial problem
A census of the population of the United States is taken every 10 years.
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 3 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
INTRODUCTION
Algebraic polynomial
A well-known classes of functions mapping the set of real
numbers into itself is the algebraic polynomials, set of functions
of the form
Pn (x ) an x n an 1x n 1
a1x a0
Where n is a nonnegative integer and a 0 , , an are real constants.
Any given function, there exists a polynomial that is as “close”
to the given function as desired.
yn f (x n )
yk f (x k )
y1 f (x1 )
y0 f (x 0 )
P (x n )
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 4 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
LAGRANGE INTERPOLATION
Lagrange Interpolating polynomial
General Theorem :
If x 0 , x1, , x n are n 1 distinct numbers and is a function whose
yn f (x n )
values are given at these numbers, then a unique polynomial P(x)
of degree n exists with y f (x k )
k
n
P (x ) f (x k )Ln,k (x ) f (x 0 )Ln,0 (x ) f (x n )Ln,n (x )
k 0 Missing: x xk
where, for each k 0, 1, ,n
n x xi (x x 0 )(x x1 ) (x xk 1 )(x xk 1 )(x xn )
Ln,k x
i 0 xk xi (x k x 0 )(x k x1 ) (x k xk 1 )(x k xk 1 )(x k xn )
i k
to simple Ln,k x Lk x
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 5 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
LAGRANGE INTERPOLATION
Lagrange Interpolating polynomial
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 6 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
LAGRANGE INTERPOLATION
Example 1:
Solution :
xk 2 2.75 4
, k 0,1, 2 3 points poynomial 2nd order
yk f (x k ) 1 2 1 2.75 1 4
x x1 x x2 x 2.75 x 4 2
a) k 0 : L0 (x ) x 2.75 x 4
x0 x1 x 0 x2 2 2.75 2 4 3
x x0 x x2 x 2 x 4 16
k 1 : L1(x ) x 2 x 4
x1 x 0 x1 x2 2.75 2 2.75 4 15
x x0 x x1 x 2 x 2.75 2
k 2 : L2 (x ) x 2 x 2.75
x2 x 0 x2 x1 4 2 4 2.75 5
2
P (x ) f (x k )Lk (x ) f (x 0 )L0 (x ) f (x 1 )L1(x ) f (x 2 )L2 (x )
k 0
1 2 1 16 1 2
x 2.75 x 4 x 2 x 4 x 2 x 2.75
2 3 2.7515 4 5
1 2 35 49 1 2 35 49
P (x ) x x b) P 3 3 3 0.32955 f (3)
22 88 44 22 88 44
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 7 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
LAGRANGE INTERPOLATION
Example 2:
Find the interpolating polynomial that passes through (2, 5), (3, 7), (5, 8)
and find a value at x 4
Solution :
xk 2 3 5
, k 0,1, 2 3 points poynomial 2nd order
yk f (x k ) 5 7 8
x 3 x 5 1 1 2 8
a) k 0 : L0 (x ) x 3 x 5 x x 5
2 3 2 5 3 3 3
x 2 x 5 1 1 2 7
k 1 : L1(x ) x 2 x 5 x x 5
3 2 3 5 2 2 2
x 2 x 3 1 1 2 5
k 2 : L2 (x ) x 2 x 3 x x 1
5 2 5 3 6 6 6
2
P (x ) f (x k )Lk (x ) f (x 0 )L0 (x ) f (x1 )L1(x ) f (x 2 )L2 (x )
k 0
1 2 8 1 2 7 1 5
5 x x 5 7 x x 5 8 x2 x 1
3 3 2 2 6 6
1 2 9 1 2 9
P (x ) x x 2 b) P 4 4 4 2 8
2 2 2 2
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 8 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
LAGRANGE INTERPOLATION
(MATLAB simulation) n x xi (x x 0 )(x x1 ) (x xk 1 )(x xk 1 )(x xn )
Ln,k x
i 0 xk xi (x k x 0 )(x k x1 ) (x k xk 1 )(x k xk 1 )(x k xn )
i k
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 9 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
LAGRANGE INTERPOLATION
Example 3.
Using Lagrange’s interpolation formula to find y(1.5) from the
Calculator :
following table:
xk 0 1 2 5 xg A xg B xg C
Y Y :F F Y
X A X B X C
yk f xk 2 3 12 147
input : A x 1, B x 2 ,C x 3, X x 0 ,Y Y0 , F 0,
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 10 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
LAGRANGE INTERPOLATION
Example 4.
Using Lagrange’s interpolation formula to find y(3.5) from the
following table:
xk 1 2 3 4 5
yk f xk 1 4 9 16 25
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 11 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
LAGRANGE INTERPOLATION
Error evaluation of Lagrange interpolating polynomial.
Suppose x 0 , x1 , x n are distinct nunbers in the interval [a, b ] and f (x ) continuous
n 1
and f in a, b . Then for each x in [a, b ], a number (generally unknown)
between x 0 , x1 , x n , and hence in (a, b), exists with
f (n 1) n f (n 1)
f (x ) Pn (x ) x xi Pn (x ) x x0 x x1 x xn
n 1! i 0 n 1!
Error
where P (x ) is the interpolating polynomial of f (x )
f (n 1)
n
Boudary error f (x ) f (x ) Pn (x ) x xi
n 1! i 0
n
M
x xi
n 1! i 0
Absolute error at x k f (x k ) Pn (x k )
where f (n 1)
M, x x 0, xn
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 12 Mechanical Engineering
Introduction
Lagrange Interpolation f (n 1) n
Newton interpolation f (x ) P (x ) x xi
Spline interpolation
n 1! i 0
LAGRANGE INTERPOLATION Error
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 13 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation .
LAGRANGE INTERPOLATION
Example 6:
xk 1 2 3 4 5
Given function: y f (x ) (x 2) cos x with
3 yk f xk 1.5 2 5 3 3.5
a)Find interpolating polynomial for function f (x ),
b) Find f (2.5) and evaluating the absoulute error and boundary error
Solution:
a ) P (x ) L0y 0 L1y1 L2y2 L3y 3 L4y 4
x 2 x 3 x 4 x 5 x 1 x 3 x 4 x 5
1.5 ( 2)
24 6
x 1 x 2 x 4 x 5 x 1 x 2 x 3 x 5
( 5) ( 3)
4 6
x 1 x 2 x 3 x 4
(3.5)
24
4
P (x ) 0.208x 2.833x 3 11.542x 2 14.417x 4
f (2.5) P (2.5) 3.9609
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 14 Mechanical Engineering
Introduction
(n ) n
a n cos ax
Lagrange Interpolation
Newton interpolation cos(ax b) b
Spline interpolation 2
LAGRANGE INTERPOLATION
Example 6:
b)evaluating error
(n ) n
Leibniz : uv C nk u (k )v (n k)
k 0
C n0uv (n ) C n1 u v (n 1)
C n2u v (n 2)
C nn u (n )v
n!
C nk
k! n k !
We have: f (x ) (x 2)cos x (5) (4)
3 f (5)(x ) (x 2) cos x 5 cos x
(5) (5) (4) 3 3
f (5)(x ) (x 2)cos x C 50 (x 2) cos x C 51(x 2) cos x (5)
3 3 3 5 5
cos x 3 cos x 3 5 2 3 cos x 3 2
(3) 3
C 52 (x 2) cos x (4)
3 4 4
0 cos x 3 cos x 3 4 2 3 cos x 3
3
5! 5!
C 50 1, C 51 5,
0!(5!) 1!(4 !)
5 4
f (5)(x ) (x 2) 3 cos x 3 2 5 3 cos x 3
5 4
f (5)(x ) (x 2) 3 cos x 3 2 5 3 cos x 3 , x [1, 5]
5 4 1
7 3 .1 5 3 11.829
2
Absolute error : f (2.5) P (2.5)
f (5)(x ) 11.829
Boundary Error : f (2.5) P (2.5) x x0 x x5 2.5 1 2.5 5 0.1338 The boundary error is 0.1338
(n 1)! 5!
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 15 Mechanical Engineering
Introduction
(n ) n
a n cos ax
Lagrange Interpolation
Newton interpolation cos(ax b) b
Spline interpolation 2
LAGRANGE INTERPOLATION
Example 7:
Solution :
To compute an boundary error for the approximation of the given function in the interval [0,0.8],
we use the following error formula for Lagrange polynomial degree four
f (5) 4 f (5)
f (x ) P4 (x ) . x xi . x x0 x x1 x x2 x x3 x x4
5! i 0 5!
f (5) x cos(x ) 1
1
*boundary error at x=0.28: f (0.28) P4 (0.28) . 0.28 0 0.28 0.2 0.28 0.4 0.28 0.6 0.28 0.8
5!
3.7 10 6
6
*Absolute error at x=0.28: f (0.28) P4 (0.28) sin(0.28) P4 (0.28) 0.2763556 0.2763591 3.5 10
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 16 Mechanical Engineering
Introduction
(n ) n
a n cos ax
Lagrange Interpolation
Newton interpolation cos(ax b) b
Spline interpolation 2
LAGRANGE INTERPOLATION
Example 8:
Consider the following table having the data for f (x ) e 3x cos 2x
x 0.2 0.4 0.5
f (x ) 2.7432 4.0168 5.2022
Find the approximaton of f (0.45) using the best quadratic Lagrange interpolation formula and also estimate an
boundary error and absolute error for the approximation
Solution :
we can find a Lagrange polynomial: P2 (x ) 11.28x 2 x 2.452
Absolute error : f (0.45) P2 (0.45) 0.0097
Boundary error :
f (3)(x )
f (x ) P2 (x ) x x0 x x1 x x2
3!
where: f (x ) 27e 3x 8 sin 2x
x 0.2, 0.5 f (x ) 27e 3(0.5) 8 sin 2(0.5) 127.7374
127.7374
Boundary error can be computed by: f (0.45) P2 (0.45) 0.45 0.2 0.45 0.4 0.45 0.5 0.0133
3!
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 17 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
P x1 P x0 P x2 P x1 P x3 P x2
a0 P x 0 , a1 P x 1, x 0 , P x 2 , x1 , P x 3, x2
x1 x 0 x 2 x1 x3 x2
P x 2 , x1 P x 1, x 0 P x 3, x2 P x 2 , x1 P x 3 , x 2 , x1 P x 2 , x 1, x 0
a2 P x 2 , x 1, x 0 , P x 3 , x 2 , x1 a3 P x 3 , x 2 , x 1, x 0
x2 x0 x3 x1 x3 x0
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 19 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 20 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
Example 2:
Fit a third-order polynomal to the four points used as follows:
x 1 4 6 5
f (x ) P (x ) 0 1.386924 1.791759 1.6094381
estimate error at x 2
Solution :
From newton's interpolating polynomial:
P3 (x ) 0 0.4620981 x 1 0.05187311 x 1 x 4 0.00786629 x 1 x 4 x 6
Estimate error (n 2) : R2 (x ) 0.00786629 x 1 x 4 x 6
R2 (2) 0.00786629 2 1 2 4 2 6 0.0629 true error 0.0644
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 22 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 23 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 24 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
SPLINE INTERPOLATION
Spline Interpolation definition
nth-order polynomials were used to interpolate
between n + l data points. For example, for eight
points, we can derive a perfect seventh-order
polynomial. This curve would capture all the
meanderings (at least up to and including
seventh derivatives) suggested by the points.
Given : x 0 , x1, , x n , n 1 points Pn (x )
ex: 8 points P7 (x )
x 0 , x1, , x n P7 (x )
However, there are cases where these functions
can lead to erroneous results because of round-off
error and overshoot. An alternative approach is
to apply lower-order polynomials to subsets of
data points. Such connecting polynomials are
called spline functions.
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 25 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
SPLINE INTERPOLATION
Spline Interpolation definition
For n data points (i=1,2,…,n), there are (n - 1) intervals. Each interval i has two points, and has its own
spline function 𝑠𝑖 (𝑥) .
Spline si (x ) can be:
Linear(First-Order) splines.
Quadratic(Second order) splines.
Cubic (Third order) splines.
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 26 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
SPLINE INTERPOLATION
1. Linear spline
For linear splines, each function is merely the straight line connecting the two points at each end of the
interval, the straight line is given by:
fi 1 fi
si (x ) fi (x x i ), where fi is shorthand for fi (x )
xi 1 xi s1(x )
s 3 (x )
s2 (x )
Example 1:
Fit the data in Table with first-order spline. Evaluate the function at x = 5.
i xi fi Solution :
f2 f1 1 2.5
1 3.0 2.5 In the first interval from 3.0 to 4.5: s1(x ) f1 x x1 2.5 x 3
x2 x1 4.5 3.0
2 4.5 1.0
f3 f2 2.5 1.0
3 7.0 2.5 In the second interval from 4.5 to 7.0: s2 (x ) f2 x x2 1.0 x 4.5
x3 x2 7.0 4.5
4 9.0 0.5 f4 f3 0.5 2.5
In the third interval from 7.0 to 9.0: s 3 (x ) f3 x x3 2.5 x 7.0
x 4 x2 9.0 7.0
2.5 1.0
x 5 4.5, 7 x 5 s2 (x ) s2 (5) 1.0 5 4.5 1.3
7.0 4.5
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 27 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
SPLINE INTERPOLATION
2. Quadratic spline (Second order spline)
For second-order spline, each function is merely the second-order connecting the two points at each end of
2
the interval, the second-order spline is given by: si (x ) ai bi (x x i ) ci (x x i ) , i 1, 2, , n, Interval:(n 1)
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 28 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
SPLINE INTERPOLATION
2. Quadratic spline (Second order spline)
si 1 (x )
* Smooth condition
2
si (x ) fi bi x xi ci x xi
2
si 1(x ) fi 1 bi 1 x xi 1 ci 1 x xi 1
si (x ) si 1(x )
x xi 1
x xi 1
bi 2ci x i 1 xi bi 1 2ci 1 xi 1 xi 1
bi 2ci x i 1 xi bi 1
bi 2ci hi bi 1, where i 1, 2, ... n 2 Notice that there are n - 1 intervals and n data points
3.Assume that the second derivative
is zero at the first point: s1(x ) 2c1 0 c1 0
i x f
Example 2: i i
1 3.0 2.5
Fit the data in Table with second-order spline.
2 4.5 1.0
Evaluate the function at x = 5.
3 7.0 2.5
4 9.0 0.5
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 29 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
SPLINE INTERPOLATION
Solution:
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 30 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
SPLINE INTERPOLATION
Summarization
bi hi ci hi2 fi 1 fi bi bi 1 2ci hi 0
c1 0
where i 1, 2... n 1 where i 1, 2,... n 2
b1h1 f2 f1 b1 b2 0
n points b2h2 c2h22 f3 f2 b2 b3 2c2h2 0
n 1 spline
hi xi 1 xi b h
i i ci hi2 fi1 fi bi bi 1 2ci hi 0
bn 1hn cn 1hn2 1 fn fn bn 2 bn 1 2cn 2hn 2 0
1 1
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 31 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
SPLINE INTERPOLATION
3. Cubic spline
si (x ) ai bi (x x i ) ci (x x i )2 di (x x i )3 , i 1, 2, , n, data points , Interval:(n 1)
ai , bi , ci and di are computed by:
1 0 0 0 c1 0
h1 2(h1 h2 ) h2 0 c2 3( f [x 3 , x 2 ] f [x 2 , x1 ])
0 0
0 0 hn 2 2(hn 2 hn 1 ) hn 1 cn 1 3( f [x n , x n 1] f [x n 1, x n 2 ])
0 0 0 0 1 cn 0
n n
fi fj
where f [x i , x j ] , hi xi 1 xi
xi xj
i xi fi
fi 1 fi hi ci 1 ci
and ai fi ; bi 2ci ci ; di 1 3.0 2.5
1
hi 3 3hi 2 4.5 1.0
Example 3:
Fit the data in Table with third-order spline. Evaluate the function at x = 5. 3 7.0 2.5
4 9.0 0.5
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 32 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
SPLINE INTERPOLATION
Solution:
1 0 0 0 c1 0
h1 2 h1 h2 h2 0 c2 3 f [x 3 , x 2 ] f [x 2 , x1 ]
0 h2 2 h2 h3 h3 c 3 3 f [x 4 , x 3 ] f [x 3 , x 2 ]
0 0 0 1 c4 0
where
f1 2.5 h1 4.5 3.0 1.5 f [x 2 , x1 ] f2 f1 x2 x1 1
f2 1.0 h2 7.0 4.5 2.5 f [x 3 , x 2 ] f3 f2 x3 x2 0.6
,
f3 2.5 h3 9.0 7.0 2.0 f [x 4 , x 3 ] f4 f3 x4 x3 1
f4 0.5 c1 0
1 0 0 0 c1 0 c1 0
1.5 8 2.5 0 c2 4.8 c2 0.83954
i xi fi
0 2.5 9 2 c3 4.8 c3 0.76654
1 3.0 2.5
0 0 0 1 c4 0 c4 0 2 4.5 1.0
3 7.0 2.5
4 9.0 0.5
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 33 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
SPLINE INTERPOLATION
Example 4: x 0 1 2 3
Given data points
f (x ) ex 1 e e2 e 3
Fit the data by cubic spline
fi fi hi
Solution: ai fi a1 1, a2 e, a 3 e 2 ; bi 1
2ci ci 1
hi 3
1 0 0 0 c1 0
f2 f1 h1 e 1 1
h1 2 h1 h2 h2 0 c2 3 f [x 3 , x 2 ] f [x 2 , x1 ] b1 2c1 c2 0.757 1.4659
h1 3 1 3
0 h2 2 h2 h3 h3 c 3 3 f [x 4 , x 3 ] f [x 3 , x 2 ] f3 f2 h2 e2 e 1
b2 2c2 c3 2 0.757 5.830 2.2228
0 0 0 1 c4 0 h2 3 1 3
where f4 f3 h3 e3 e2 1
b3 2c3 c4 2 5.830 8.8098
f1 1 h1 1 f [x 2 , x1 ] f2 f1 x2 x1 e 1 h3 3 1 3
ci ci
f2 e h2 1 f [x 3 , x 2 ] f3 f2 x3 x2 e2 e And : di 1
, 3hi
f3 e2 h3 1 f [x , x ] f4 f3 x4 x3 e3 e2 c2 c1 c3 c2
4 3 0.757 5.83 0.757
d1 0.2523 ; d2 1.691
f4 e 3 c1 0 3h1 3 3h2 3
1 0 0 0 c1 0 c1 0 c4 c3 5.83
d3 1.9433
2 3h3 3
1 4 1 0 c2 3(e 2e 1) c2 0.757
3 2 s1(x ) 1 1.4659x 0.2523x 2 , x [0,1]
0 1 4 1 c3 3(e 2e e) c3 5.830
2 3
0 0 0 1 c4 c4 0 s2 (x ) 2.718 2.2228 x 1 0.757 x 1 1.691 x 1 , x [1, 2]
0
2 3
s 3 (x ) 7.389 8.809 x 2 5.83 x 2 1.9433 x 2 ,x [2, 3]
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 34 Mechanical Engineering
Introduction
Lagrange Interpolation
Newton interpolation
Spline interpolation
Exercises
Exercise 1:
Given data points
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 35 Mechanical Engineering