3.2 - Interpolation and Lagrange Polynomials 1. Polynomial Interpolation: Problem: Given N
3.2 - Interpolation and Lagrange Polynomials 1. Polynomial Interpolation: Problem: Given N
1. Polynomial Interpolation:
Problem: Given n 1 pairs of data points x i , y i , where y i fx i , i 0, 1, . . . , n for some function
fx, we want to find a polynomial P k x of lowest possible degree for which
P k x i y i , i 0, 1, . . . , n (P k x agrees with fx at x 0 , . . . , x n ).
The polynomial P k x is said to interpolate the data x i , y i , i 0, 1, . . . , n and is called an
interpolating polynomial. Graphically, P k x is an approximation to fx and satisfies the conditions:
P k x i fx i , i 0, 1, . . . , n.
For example,
y
10
-3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.5 1.0 1.5 2.0 2.5 3.0
x
-5
-10
— y fx, - - - y P k x
Obviously, for this example P k x is not a good approximation to fx though P k x satisfies the
conditions:
P k x i y i for i 0, 1, 2.
How to form P k x?
There are several ways to form P k x. Let us start with a way we know already. Let
P k x a 0 a 1 x a 2 x 2 . . . a k x k .
We want to find coefficients a 0 , a 1 , . . . , a k such that the following n 1 equations hold:
P k x i a 0 a 1 x a 2 x 2 . . . a k x k y i for i 0, 1, . . . , n.
Rewrite these n 1 equations in a matrix-vector form:
a0
1 x 0 x 20 x k0 y0
a1
1 x1 x 21 x k1 y1
(*) a2 .
1 x n x 2n x kn yn
ak
a0
a1
The vector a2 is a solution of the system (*) of n 1 linear equations in k 1 unknowns.
ak
1
Consider the case where k n. Recall in Linear Algebra, we learned that this system has a unique
1 x 0 x 20 x k0
1 x 1 x 21 x k1
solution if the coefficient matrix is nonsingular and the solution vector is of the
1 x n x 2n x kn
form:
a0 −1
1 x 0 x 20 x k0 y0
a1
1 x 1 x 21 x k1 y1
a2
1 x n x 2n x kn yn
ak
and can be solved by Gaussian-Elimination and Backward-Substution.
Example Let x 0 0, x 1 0. 6, x 2 0. 9, fx cosx 2 . Find P 2 x a 0 a 1 x a 2 x 2 . Sketch both
y fx and y P 2 x for x in 0, 1.
a0
Solve a1 from the following system:
a2
1 0 0 a0 1
1 0. 6 0. 36 a1 cos0. 36
1 0. 9 0. 81 a2 cos0. 81
−1
a0 1 0 0 1 1. 0
a1 1 0. 6 0. 36 cos0. 36 0. 369 487 601 .
a2 1 0. 9 0. 81 cos0. 81 −0. 793 877 047
Hence, P 2 x 1 0. 369 487 601x − 0. 793 877 047 x 2 .
y 1.0
0.9
0.8
0.7
0.6
— y fx, - - - y P 2 x
In MatLab, we can complete this example as follows.
xv[0;0.6;0.9];
A[ones(3,1) xv xv.^2];
2
yvcos(xv.^2);
coeffvinv(A)*yv
coeffv
1.000000000000000
0.369487600719125
-0.793877046537612
Now we sketch the graphs of y cosx 2 and y P 2 x 1 0. 369 487 601x − 0. 793 877 047 x 2 for x
in 0, 1.
xvnew0:0.01:1;
y1cos(xvnew.^2);
y2coeffv(1)coeffv(2)*xvnewcoeffv(3)*xvnew.^2;
plot(xvnew,y1,’r-’,xvnew,y2,’b-.’);
2
red - y=cos(x ), blue -. y=P (x)
2
1.3
1.2
1.1
0.9
0.8
0.7
0.6
0.5
0 0.2 0.4 0.6 0.8 1
If we want to add a pair of data 0. 3, cos0. 09, then we can go over the following steps.
xv[0;0.3;0.6;0.9];
A[ones(4,1) xv xv.^2 xv.^3];
yvcos(xv.^2);
coeffvinv(A)*yv
coeffv
1.000000000000000
-0.064958529434456
0.412917759444563
-0.804529870654783
Hence, P 3 x 1 − 0. 064958529434456x 0. 412917759444563x 2 − 0. 804529870654783x 3 . Now we
sketch the graphs of y cosx 2 and y P 3 x as follows. Remember that xvnew and y1 are still there.
We need to recompute y2.
y2coeffv(1)coeffv(2)*xvnewcoeffv(3)*xvnew.^2coeffv(4)*xvnew.^3;
plot(xvnew,y1,’r-’,xvnew,y2,’b-.’);
title(’red - ycos(x^2), blue -. yP_3(x) ’)
3
2
red - y=cos(x ), blue -. y=P (x)
3
1
0.95
0.9
0.85
0.8
0.75
0.7
0.65
0.6
0.55
0.5
0 0.2 0.4 0.6 0.8 1
Note that the system (*) of linear equations are getting more difficult to solve when n is large. Can
P k x be formed with less computation?
2. Lagrange Interpolating Polynomials:
a. Lagrange Polynomials:
For k 0, 1, . . . , n, define
n
x − x i x − x 0 . . . x − x k−1 x − x k1 . . . x − x n
L n,k x x k − x i
x k − x 0 . . . x k − x k−1 x k − x k1 . . . x k − x n
.
i0, i≠k
L n,k x are called Lagrange polynomials. For example, let x i i, i 0, 1, 2, 3. Then
x 0 0, x 1 1, x 2 2 and x 3 3, and
x − 0x − 2x − 3
L 3,1 x 1 xx − 2x − 3
1 − 01 − 21 − 3 2
xx − 1x − 3
L 3,2 x − 1 xx − 1x − 3
2 − 02 − 12 − 3 2
Observe that Lagrange Polynomials have the following properties:
i. L n,k x k 1
x k − x 0 . . . x k − x k−1 x k − x k1 . . . x k − x n
L n,k x k 1
x k − x 0 . . . x k − x k−1 x k − x k1 . . . x k − x n
ii. L n,k x i 0 for i ≠ k.
x i − x 0 . . . x i − x i . . . x i − x k−1 x i − x k1 . . . x i − x n
L n,k x i 0
x k − x 0 . . . x k − x i . . . x k − x k−1 x k − x k1 . . . x k − x n
For example,
22 − 12 − 3 11 − 11 − 3
L 3,2 2 1 and L 3,2 1 0.
2 − 02 − 12 − 3 2 − 02 − 12 − 3
Theorem Suppose that x 0 , . . . , x n are distinct numbers in the interval a, b and f n1 is continuous
in a, b . For each x in a, b , there exists a number cx in a, b such that
f n1 cx
fx P n x x − x 0 x − x 1 . . . x − x n .
n 1!
t − x
Proof For x ≠ x i and x in a, b , define ht ft − P n t − fx − P n x k0 x − xkk .
n
y 1.0
0.9
0.8
0.7
0.6
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
— y cosx 2 , - - - y P 2 x
′ ′′
f x −2x sinx 2 , f x −2sinx 2 2x 2 cosx 2 ,
′′′
f x −22x cosx 2 4x cosx 2 − 4x 3 sinx 2 −43x cosx 2 − 2x 3 sinx 2
− 43c cosc 2 − 2c 3 sinc 2
R 2 x xx − 0. 6x − 0. 9
6
where c is in 0, 1 .
(2)
f0. 45 ≈ P 2 0. 45
1 −0. 15−0. 45 − cos0. 36 0. 45−0. 45 cos0. 81 0. 45−0. 15
0. 54 0. 18 0. 27
1. 005 509
True error cos 0. 45 2 − P 2 0. 45 0. 979 566 8 − 1. 005 509 0. 025 942 2
Find the approximation error:
′′′
f x ≤ max − 43x cosx 2 − 2x 3 sinx 2 ≤ 43 2 20
x in 0,1
6
′′′ ′′′
or check out the graph of f x to have a better estimate of an upper bound of f x :
y6
′′′
5 f x ≤ 6. 5, for x in 0, 1
4
R 2 0. 45 ≤ 6. 5 0. 45−0. 15−0. 45
3 6
2 0. 03290
1
0
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
Proof We know that both polynomials P 0,1,...,j−1,j1,...,k x and P 0,1,...,i−1,i1,...,k x are degree k − 1. So,
the degree of P 0,1,....,k x is k. Check if P 0,1,....,k x l fx l for l 0, 1, . . . , k : since
P 0,1,...,j−1,j1,...,k x l fx l if l ≠ j and P 0,1,...,i−1,i1,...,k x l fx l if l ≠ i
For l 0, 1, . . . , k, but l ≠ i and l ≠ j,
7
x l − x j P 0,1,...,j−1,j1,...,k x l − x l − x i P 0,1,...,i−1,i1,...,k x l
P 0,1,....,k x l xi − xj
x l − x j fx l − x l − x i fx l x i − x j
fx l fx l
x i − x j x i − x j
For l i :
x i − x j P 0,1,...,j−1,j1,...,k x i − x i − x i P 0,1,...,i−1,i1,...,k x i
P 0,1,....,k x i xi − xj
P 0,1,...,j−1,j1,...,k x i fx i
For l j :
x j − x j P 0,1,...,j−1,j1,...,k x j − x j − x i P 0,1,...,i−1,i1,...,k x j
P 0,1,....,k x i xi − xj
P 0,1,...,i−1,i1,...,k x j fx j
So, P 0,1,....,k x the kth degree interpolating polynomial.
Algorithm Neville’s Iterated Interpolation: Steps of Computing P 0,1,2,...,k x̄ for a given x̄ : (neville.m)
Given x i , fx i for i 0, 1, . . . , k and , let P i fx i
8
xi P i x̄ P i,i1 x̄ P i,i1,i2 x̄ P i,i1,i2,i3 x̄ P i,i1,i2,i3,i4 x̄
x0 P0
x1 P1 P 0,1 x̄
x2 P2 P 1,2 x̄ P 0,1,2 x̄
x3 P3 P 2,3 x̄ P 1,2,3 x̄ P 0,1,2,3 x̄
x4 P4 P 3,4 x̄ P 2,3,4 x̄ P 1,2,3,4 x̄ P 0,1,2,3,4 x̄
An alternative way:
xi P i x̄ P i,i1 x̄ P i,i1,i2 x̄ P i,i1,i2,i3 x̄ P i,i1,i2,i3,i4 x̄
x0 P0
x1 P1 P 0,1 x̄
x2 P2 P 0,2 x̄ P 0,1,2 x̄
x3 P3 P 0,3 x̄ P 0,2,3 x̄ P 0,1,2,3 x̄
x4 P4 P 0,4 x̄ P 0,3,4 x̄ P 0,2,3,4 x̄ P 0,1,2,3,4 x̄
9
1. 5 − 3P 0,1,2 1. 5 − 1. 5 − 0P 1,2,3 1. 5 −1. 53. 25 − 1. 54
P 0,1,2,3 1. 5 5. 437 5
0 − 3 −2
Example Neville’s method is used to approximate f0. 4 as follows. Complete the table.
xi P i x̄ P i,i1 x̄ P i,i1,i2 x̄ P i,i1,i2,i3 x̄
0 1
0. 25 2 P 0,1 0. 4 2. 6
0. 5 P2 P 1,2 0. 4 P 0,1,2 0. 4
0. 75 8 P 2,3 0. 4 2. 4 P 1,2,3 0. 4 2. 96 P 0,1,2,3 0. 4 3. 016
10
y 0.32 y
0.015
0.30
0.28
0.010
0.26
0.24
0.005
0.22
0.20
0.18 0.000
2.0 2.1 2.2 2.3 2.4 2.5 2.6
2.0 2.1 2.2 2.3 2.4 2.5 2.6 x
′′′
f x y |x − 2x − 2. 4x − 2. 6|
′′′ ′′′ 3 sinln 2 cosln 2
f x ≤ f 2 0. 336
23
′′′
f c
R 2 x x − 2x − 2. 4x − 2. 6 ≤ 0. 336 0. 02 0. 001 12
3! 6
(2) xv[2;2.4;2.6];
yvsin(log(xv));
[yout,yall]neville(xv,yv,2.1,2)
yout 0.67510095866951
yall
0.63896127631363 0.67118192650420 0.67510095866951
0.76784387707588 0.69469611949605 0
0.81660904879577 0 0
[yout,yall]neville(xv,yv,2.5,2)
yout 0.79353280699093
yall
0.63896127631363 0.80006452726644 0.79353280699093
0.76784387707588 0.79222646293583 0
0.81660904879577 0 0
true error: |sinlog2. 1 − 0. 67510095866951| 6. 163 548 3 10 −4
true error: |sinlog2. 5 − 0. 79353280699093| 1. 838 044 02 10 −4
′′′ ′′′
f c f c
R 2 2. 1 2. 1 − 22. 1 − 2. 42. 1 − 2. 6 |2. 1 − 22. 1 − 2. 42. 1 − 2. 6|
3! 6
11
Exercises:
1. Given x i , y i 0, −1, 12 , 1, 1, 2 where y i fx i for some fx, construct the polynomial
P 2 x that agrees with fx at x 0 , x 1 and x 2 in the following two ways.
(1) P 2 x a 0 a 1 x a 2 x 2 a standard form.
(2) P 2 x is a Lagrange interpolating polynomial.
4. Use Neville’s method to approximate f0. 5, giving the following table. Determine P 2 f0. 7.
i xi Pi P i,i1 P i,i1,i2
0 0 0
1 0. 4 2. 8 3. 5
27
2 0. 7 7
5. Suppose we know that f0 1, f0. 25 1. 64872, f0. 5 2. 71828, f0. 75 4. 48169. Use given
data to approximate f0. 43 by a Lagrange interpolating polynomial using Neville’s method (MatLab
program neville.m).
6. A census of the population of the US is taken every 10 years. The population, in thousands, from 1940
to 2000 is given below.
year 1940 1950 1960 1970 1980 1990 2000
t 0 10 20 30 40 50 60
population 132165 151326 179323 203302 226542 249633 251432
Use Neville’s method (MatLab program neville.m) to estimate the populations in 1975 and 2005 and
predict the population in 2010.
12
P 0 0. 5 P 0,1 0. 5 P 0,1,2 0. 5 P 0,1,2,3 0. 5 P 0,1,2,3,4 0. 5 P 0,1,2,3,4,5 0. 5 P 0,1,2,3,4,5,6 0. 5
1. 00000000 1. 35886731 1. 41038161 1. 41407897 1. 41421166 1. 41421356 1. 41421357
Let the stopping criterion be set as | P 01k 0. 5 − P 01k−1 0. 5 | .
a. Determine the degree (k) of the polynomial used to approximate 2 with 0. 0001.
b. If P 0,1,2,3,4,5,6 0. 5 is chosen from above sequence to approximate 2 , what is ?
13