You are on page 1of 46

Chapter 14

Polynomial Interpolation
Interpolation & Extrapolation

•Interpolation: data to be found are within the range of


observed data.

•Extrapolation: data to be found are beyond the range of


observation data. (not reliable*)
Interpolation
 Used to estimate values between data points
 Difference from regression -

 exactly goes through data points


 therefore, no error at data points

The most common method is the polynomial interpolation


Interpolation vs. Regression

Same data points, different curve fitting


Polynomial Interpolation
Given n data points, fit a unique (n-1)th- order polynomial
through them

f  x   a1  a2 x  a3 x  ...  an x
2 n 1

Use polynomial interpolation to determine a’s


For consistency with MATLAB, use

n 1 n 2
f ( x )  p1 x  p2 x    pn  1 x  pn
Interpolating Polynomials

First-order second-order third-order


Coefficients of an Interpolating Polynomial

Newton and Lagrange polynomials are well-suited for


determining values between points (interpolation)
However, they do not provide a convenient polynomial
of conventional form
f ( x )  p1 x n1  p2 x n 2    pn1 x  pn
Use n data points to determine n coefficients
 f ( x1 )  p1 x1n1  p2 x1n 2    pn1 x1  pn
 n 1 n 2
 f ( x 2 )  p1 x 2  p 2 x 2    p n  1 x 2  p n

 
 f ( x )  p x n 1  p x n 2    p x  p
 n 1 n 2 n n 1 n n
Coefficients of an Interpolating Polynomial

Can be solved with any matrix method, but not efficient


 x1n1 x1n 2  x1 1  p1   f ( x1 ) 
 n 1    
 x2 x 2n 2  x 2 1  p2   f ( x 2 ) 
   
 x 3n1 x 3n 2 
 x 3 1  p3    f ( x 3 ) 
   
        
   
 x nn1 x nn 2  x n 1  pn   f ( x n ) 

There are more efficient methods to find p’s


The above equations are notoriously ill-conditioned,
especially for large n
Limit yourself to lower-order polynomials
Polynomial Coefficients
Example – use last four points of Table 14.1
f ( x )  p1 x 3  p2 x 2  p3 x  p4
x1  250 ; f ( x1 )  0.675  p1 ( 250 ) 3  p2 ( 250 ) 2  p3 ( 250 )  p4
x1  300 ; f ( x1 )  0.616  p1 ( 300 ) 3  p2 ( 300 ) 2  p3 ( 300 )  p4
x1  400 ; f ( x1 )  0.525  p1 ( 400 ) 3  p2 ( 400 ) 2  p3 ( 400 )  p4
x1  500 ; f ( x1 )  0.457  p1 (500 ) 3  p2 (500 ) 2  p3 (500 )  p4

( 250 ) 3 ( 250 ) 2 250 1  p1  0.675 


     Vandermonde
 ( 300 ) 3
( 300 ) 2 300 1  2  
p 0. 616 
    
( 400 ) 3 ( 400 ) 2  p
400 1  3   0 .525 
Matrices
   
 ( 500 ) 3
(500 ) 2 500 1  p4  0.457 
Vandermonde Matrices
>> A=[250^3 250^2 250 1; 300^3 300^2 300 1; 400^3 400^2 400 1; 500^3 500^2 500 1]
A =
15625000 62500 250 1
27000000 90000 300 1
64000000 160000 400 1
125000000 250000 500 1
>> b=[0.675; 0.616; 0.525; 0.457]
b =
0.6750
0.6160
0.5250
0.4570
>> format long
>> p = A\b
p =
-0.00000000260000
f ( x )  0.0000000026 x 3  0.0000042700 x 2
0.00000427000000
-0.00293700000000  0.0029370000 x  1.1830000000
1.18300000000000
>> cond(A)
ans =
9.306535523991324e+009
ill-conditioned matrix
Newton Interpolation
Use Newton’s “divided differences” of the
functional values
f n1 ( x )  b1  b2 ( x  x1 )  b3 ( x  x1 )( x  x 2 )  
 bn ( x  x1 )( x  x 2 )  ( x  x n1 )
The lower order coefficients bi do not change
when the order of interpolation is increased
It is easy to add more data points and try a
higher order polynomial
Newton Linear Interpolation
Start with linear interpolation: f1(x) = b1 + b2 (x  x1 )

Similar triangles
f1 ( x )  f ( x1 ) f ( x 2 )  f ( x1 )

x  x1 x 2  x1
f ( x 2 )  f ( x1 )
f1 ( x )  f ( x1 )  ( x  x1 )
x 2  x1
Newton Linear Interpolation
Newton linear interpolation formula
f  x 2   f  x1 
f1  x   f  x1   x  x1 
x 2  x1
Example 1: interpolate e2 using e1 and e5
e5  e1
f1 2   e  1
2  1  2.7183  148.41  2.7183 1  36.423
51 4

Example 2: Interpolate e2 using e1.5 and e2.5


e 2.5  e 1.5
f1 2   e 1.5
 2  1.5   4.4817  12.1825  4.4817 0.5   8.3321
2.5  1.5 1

Exact e  7.3891 2
Newton Linear Interpolation
Accuracy of Interpolation
Logarithmic function

Linear estimates
of ln(2)

Small interval x provides a better estimate


Quadratic Interpolation
Quadratic interpolation – need three points
Use the parabola

f 2 x   b1  b2 x  x1   b3 x  x1 x  x2 

This is the same as f 2 ( x )  a1  a 2 x  a 3 x 2

a1  b1  b2 x1  b3 x1 x 2

where a2  b2  b3 x1  b3 x 2
a  b
 3 3
Quadratic Interpolation
f 2 x   b1  b2 x  x1   b3 x  x1 x  x2 
To get the coefficients b’s
1) Set x = x1, get b1 = f(x1)
f 2 x1   b1  b2 x1  x1   b3 x1  x1 x1  x2   b1  f x1 

2) Use b1 and set x = x2 to get b2


f 2  x 2   f  x1   b2  x 2  x1   b3  x 2  x1  x 2  x 2   f  x 2 
f  x 2   f  x1 
b2 
x 2  x1
Quadratic Interpolation
3) Use b1 and b2, and set x = x3 to get b3
f  x 2   f  x1 
f 2  x 3   f  x1   x3  x1   b3 x3  x1 x3  x2   f x3 
x 2  x1
f  x 3   f  x 2  f  x 2   f  x1 

x3  x2 x 2  x1
b3 
x 3  x1

b1 is a constant (0th order)


b2 gives slope (finite difference)
b3 gives curvature (difference of finite differences)
Hand Calculation Example
Example: interpolate e2 using e1, e3, and e5
 x1  1 f  x1   2.7183

 x2  3 f  x 2   20.086
x  5 f  x 3   148.41
 3
e5  e3 e3  e1

e3  e1
b1  2.7183 ; b2   8.6836 ; b3  5  3 3  1  13.870
31 51

f2 2  2.7183  8.6836 * 2  1  13.870 * 2  12  3  2.4681

Exact e  7.3891
2
Example – use different xi
Example: interpolate e2 using e1, e1.5, and e2.5
 x1  1 f  x1   2.7183

 x 2  1.5 f  x 2   4.4817
 x  2.5 f  x 3   12.1825
 3
e 2.5  e 1.5 e 1.5  e 1

e 1.5  e 1
b1  2.7183 ; b2   3.5268 ; b3  2.5  1.5 1.5  1  2.7827
1.5  1 2.5  1

f2 2  2.7183  3.5268 * 2  1  2.7827 * 2  12  1.5   7.6365

Exact e  7.3891
2
Quadratic Interpolation
Quadratic Interpolation
Order of Interpolation
Linear, quadratic and cubic estimates of ln(2)
Logarithmic function

Higher-order interpolation improves the estimate


Newton’s Interpolating Polynomials

General form for Newton’s interpolating polynomials


f n1  x   b1  b2  x  x1   b3  x  x1  x  x 2   
   bn  x  x1  x  x 2  x  x 3   x  xn1 

b1  f  x1 
b2  f x 2 , x1 
b3  f x 3 , x 2 , x1 

bn  f x n , x n1 ,  , x 2 , x1 

Bracketed functions are finite differences


Newton’s Divided Differences

First finite difference


f  x i   f x j 
f x i , x j  
xi  x j
Second finite difference

f x i , x j   f x j , x k 
f x i , x j , x k  
xi  xk
The difference of two finite differences
Newton’s Divided Differences

The nth finite difference


f  x n , x n  1 ,  , x 3 , x 2   f x n  1 , x n  2 ,  , x 2 , x 1 
f x n , x n  1 ,  , x 2 , x 1  
x n  x1

An iterative procedure
1. Evaluate all first-order finite differences; save f (x1) for b1
2. Evaluate second-order from firsts; save f [x2, x1] for b2
3. Continue to nth-order, saving needed ones
Newton Interpolation
f n 1 ( x )  b1  b2 ( x  x 1 )  b3 ( x  x 1 )( x  x 2 )    bn ( x  x 1 )  ( x  x n 1 )
b1  f ( x 1 )  f x 1 
f x 2   f x 1 
b2  f x 2 , x 1  
x 2  x1
f x 3 , x 2   f x 2 , x 1 
b3  f x 3 , x 2 , x 1  
x 3  x1
f x 4 , x 3 , x 2   f x 3 , x 2 , x 1 
b4  f x 4 , x 3 , x 2 , x 1  
x4  x1
f x 5 , x 4 , x 3 , x 2   f x 4 , x 3 , x 2 , x 1 
b5  f x5 , x 4 , x 3 , x 2 , x 1  
x5  x 1

 No need to solve a system of simultaneous equations


 Spacing may be non-uniform and xi may be in arbitrary order
Newton Interpolation
f n1 ( x )  b1  b2 ( x  x1 )  b3 ( x  x1 )( x  x 2 )    bn ( x  x1 )  ( x  x n1 )
f x , x n1 ,  , x 3 , x1   f x n1 , x n 2 ,  , x 2 , x1 
bn  f x , x n1, , x 2 
,x  n

x n  x1
n 1

i xi yi  f ( x i ) f x i  1 , x i  f x i  2 , x i  1 , x i  f x i  3 ,  , x i  f x i  4 ,  , x i 
First Second Third Fourth
1 x1 f ( x1 ) f x 2 , x 1  f x 3 , x 2 , x 1  f x 4 , x 3 , x 2 , x 1  f x 5 , x 4 , x 3 , x 2 , x 1 
2 x2 f ( x2 ) f x 3 , x 2  f x 4 , x 3 , x 2  f x 5 , x 4 , x 3 , x 2  f x6 , x 5 , x 4 , x 3 , x 2 
3 x3 f ( x3 ) f x 4 , x 3  f x 5 , x 4 , x 3  f x6 , x 5 , x 4 , x 3 
4 x4 f ( x4 ) f x 5 , x 4  f x6 , x 5 , x 4 
5 x5 f ( x5 ) f x6 , x 5 
6 x6 f ( x6 ) Use the top element of each column
(= bn) to evaluate the interpolated
functional value f(x)
Percentage Relative Error

Relative error decreases


with increasing order of the
interpolating polynomial

The error is also sensitive to


the position and sequence of
the original data
(x1 , x2 , x3 , x4 , …, xn)
Hand Calculations
Newton Interpolation
Estimate f(x)  e x at x  2 using (x 1 , x 2 , x 3 , x 4 )  (0 ,1, 4 , 3 )
i xi f ( xi ) f x i  1 , x i  f x i  2 , x i  1 , x i  f x i  3 , x i  2 , x i  1 , x i 

1 0 1.000000 1.718282 3.893752 1.571970


 x  10  x  4 0  x  30
2 1 2.718282 17.29329 8.609662
 x 41  x  31
3 4 54.59815 34.51261
 x  34
4 3 20.08554

f 2 ( 2 )  1  1.718282 x  3.893752 x ( x  1)  12.22407


f 3 ( 2 )  1  1.718282 x  3.893752 x ( x  1)  1.571970 x ( x  1)( x  4 )  5.936187
Error Estimate

Error for Newton’s interpolating polynomial


Estimate from

Rn  f xn1 , xn , xn1 , , x1 x  x1 x  x2 x  xn 

The leading term in the truncated polynomial


Similar to the truncation error in a Taylor series
Newton Interpolation Polynomial

f(x) = ex,
Interpolation at [0 1 4 3 1.5 2.5]
Lagrange Interpolating Polynomials

Give the same result as the Newton’s polynomials, but


different approach
n
f n1 ( x )  L1 ( x ) f ( x1 )  L2 ( x ) f ( x 2 )    Ln ( x ) f ( x n )   Li ( x ) f ( x i )
i 1
n x  xj Pi ( x )
Li ( x )   
j 1 x i  x j Pi ( x i )
j i

( x  x1 )( x  x 2 )  ( x  x i 1 )( x  x i  1 )  ( x  x n )

( x i  x1 )( x i  x 2 )  ( x i  x i 1 )( x i  x i  1 )  ( x k  x n )

 Pi ( x i ) 
 j  i ; Li ( x i )  P ( x )  1 
Note :  i i   Li ( x j )   ij
 j  i ; L (x )  0 
 i j 
Lagrange Interpolation
1st-order Lagrange polynomial
x  x2 x  x1
f1 ( x )  L1 f ( x1 )  L2 ( x ) f ( x 2 )  f ( x1 )  f ( x2 )
x1  x 2 x 2  x1
Second-order Lagrange polynomial
( x  x2 )( x  x3 ) ( x  x1 )( x  x3 ) ( x  x1 )( x  x2 )
f2 ( x )  f ( x1 )  f ( x2 )  f ( x3 )
( x1  x2 )( x1  x3 ) ( x2  x1 )( x2  x3 ) ( x3  x1 )( x3  x2 )

Third-order Lagrange polynomial


( x  x 2 )( x  x 3 )( x  x 4 ) ( x  x1 )( x  x 3 )( x  x 4 )
f4 ( x )  f ( x1 )  f ( x2 )
( x 1  x 2 )( x1  x 3 )( x1  x 4 ) ( x 2  x 1 )( x 2  x 3 )( x 2  x 4 )
( x  x 1 )( x  x 2 )( x  x 4 ) ( x  x1 )( x  x 2 )( x  x 3 )
 f ( x3 )  f ( x4 )
( x 3  x1 )( x 3  x 2 )( x 3  x 4 ) ( x 4  x 1 )( x 4  x 2 )( x 4  x 3 )
Linear Lagrange Interpolation

 Both L1(x) and L2(x) are straight lines


Quadratic Lagrange Interpolation
L1(x)f(x1) L3(x)f(x3)

x1 x2 x3

L2(x)f(x2)
First-order Lagrange interpolation

f(x) = ex
Interpolation at [0 4]
Second-order Lagrange interpolation

f(x) = ex,
Interpolation at [0 1 4]
Third-order Lagrange interpolation

f(x) = ex,
Interpolation at [0 1 4 3]
Example: Lagrange Interpolation
 Estimate f(x) = exp(x) at x = 2 using (x0, x1, x2, x3 ) = (0,1,4,3)
second-order: (x1, x2, x3 ) = (0,1,4)
( x  1)( x  4 ) ( x  0 )( x  4 ) ( x  0 )( x  1)
f2 ( x )  f (0 )  f ( 1)  f (4 )
(0  1)( 0  4 ) ( 1  0 )( 1  4 ) ( 4  0 )( 4  1)
( 2  1)( 2  4 ) ( 2  0 )( 2  4 ) ( 2  0 )( 2  1)
f2 ( 2)  ( 1.0 )  ( 1.71828 )  (54.5982 )  12.224067
(0  1)( 0  4 ) ( 1  0 )( 1  4 ) ( 4  0 )( 4  1)

third order: (x1, x2, x3, x4 ) = (0,1,4,3)


( x  1)( x  4 )( x  3 ) ( x  0 )( x  4 )( x  3 )
f3 ( x )  f (0 )  f ( 1)
(0  1)( 0  4 )( 0  3 ) ( 1  0 )( 1  4 )( 1  3 )
( x  0 )( x  1)( x  3 ) ( x  0 )( x  1)( x  4 )
 f (4)  f ( 3)
( 4  0 )( 4  1)( 4  3 ) ( 3  0 )( 3  1)( 3  4 )
2 4 2 4
f3 ( 2)  ( 1.0 )  ( 2.71828 )  ( 54.5982 )  ( 20.08554 )  5.936187
 12 6 12 6
Lagrange Interpolation
Very convenient for the same abscissas but
different yi (e.g., measurements always taken
using the same independent variables xi)
Lk(x) need to be computed only once
However, it is less convenient when additional
data may be added
original : x 1 , y 1  , x 2 , y 2  , , x n , y n 
new 1 : x 1 , z 1  ,x 2 , z 2  , ,x n , z n 
new 2 : x 1 , y1  ,x 2 , y 2  , ,x n , y n  ,x n1 , y n1 
Difficulties with Polynomial Interpolation
Humped and Flat Data

Runge’s
function

1
f(x)
1  25 x 2
Oscillations
1
f(x)
1  25 x 2
MATLAB Functions:
polyfit and polyval
>> x = linspace(-1,1,5); y = 1./(1+25*x.^2)
>> xx = linspace(-1,1); p = polyfit(x,y,4)
p =
3.3156 0.0000
>> y4 = polyval(p,xx);
-4.2772 0.0000 1.0000 4th-order polynomial
>> yr = 1./(1+25*xx.^2);
>> H=plot(x,y,'o',xx,y4,xx,yr,'--')
>> x=linspace(-1,1,11); y = 1./(1+25*x.^2)
y =
Columns 1 through 8
0.0385 0.0588 0.1000 0.2000 0.5000 1.0000 0.5000 0.2000
Columns 9 through 11
0.1000 0.0588 0.0385
>> p = polyfit(x,y,10)
p = 10th-order polynomial
Columns 1 through 8
-220.9417 0.0000 494.9095 -0.0000 -381.4338 0.0000 123.3597 -0.0000
Columns 9 through 11
-16.8552 0.0000 1.0000
>> y10 = polyval(p,xx);
>> H = plot(x,y,'o',xx,y10,xx,yr,'--')
>> set(H,'LineWidth',3,'MarkerSize',12)
>> print -djpeg Fig14_13.jpg
Runge’s Function with polynomial fit

4th-order 10th-order
Noisy straight line

You might also like