ME 310
Numerical Methods
Interpolation
These presentations are prepared by
Dr. Cuneyt Sert
Mechanical Engineering Department
Middle East Technical University
Ankara, Turkey
csert@metu.edu.tr
They can not be used without the permission of the author
1
Interpolation
• Estimating intermediate values between precise data points.
• We first fit a function that exactly passes through the given data points and than evaluate
intermediate values using this function.
Polynomial Interpolation Spline Interpolation
f(x) f(x)
extrapolation
interpolation
x x
• Polynomial Interpolation: A unique nth order polynomial passes through n points.
• Newton’s Divided Difference Interpolating Polynomials
• Lagrange Interpolating Polynomials
• Spline Interpolation: Pass different curves (mostly 3rd order) through different subsets of the
data points.
2
Polynomial Interpolation
• Given the following n+1 data points
(x1, y1), (x2, y2), (x3, y3), ..., (xn+1, yn+1)
there is a unique nth order polynomial that passes through them
p(x) = a0 + a1 x + a2 x2 + . . . + an xn
• The question is to find the coefficients a0 , a1 , . . ., an
• Linear Interpolation:
• Given: (x0, y0) and (x1, y1)
• A straight line passes from these two points.
y1 = f(x1) • Using similar triangles
a0 + a 1 x
f(x) = ? f (x ) f (x 0 ) f (x 1 ) f (x 0 )
x x0 x1 x 0
y0 = f(x0)
x
x0 x x1 f (x 1 ) f (x 0 )
f (x ) f (x 0 ) (x x 0 )
x1 x 0
or
f1 (x ) b 0 b1 (x x 0 )
Linear interpolation formula 3
Polynomial Interpolation
• Quadratic Interpolation:
• Given: (x0, y0) , (x1, y1) and (x2, y2)
y2 = f(x2) • A parabola passes from these three points.
y1 = f(x1) • Similar to the linear case, the equation of
this parabola can be written as
a0 + a 1 x + a 2 x2
f2 (x ) b 0 b1 (x x 0 ) b2 (x x 0 )( x x 1 )
y0 = f(x0)
x
x0 x1 x2 Quadratic interpolation formula
• How to find b0, b1 and b2 in terms of given quantities?
• at x=x0 f2(x) = f(x0) = b0 b 0 f(x 0 )
f (x 1 ) f(x 0 )
• at x=x1 f2(x) = f(x1) = b0 + b1x1 b1
x1 x 0
• at x=x2 f2(x) = f(x2) = b0 + b1(x2-x0)+ b2 (x2-x0) (x2-x1)
f (x 2 ) f(x 1 ) f (x 1 ) f(x 0 )
x2 x1 x1 x 0
b2
x2 x 0
4
Newton’s Divided Difference Interpolating Polynomials
• We can generalize the linear and quadratic interpolation formulas for an nth order polynomial
passing through n+1 points
fn(x) = b0 + b1 (x - x0) + b2 (x - x0)(x - x1) + . . . + bn (x - x0)(x - x1) ... (x - xn-1)
where the constants are
b0 = f(x0) b1 = f [x1, x0] b2 = f [x2, x1, x0] ... bn = f [xn, xn-1, . . ., x1, x0]
where the bracketed functions are finite divided differences evaluated recursively
f (x i ) f(x j )
f [x i , x j ] 1st finite divided difference
xi x j
f [x i , x j ] f [x j , x k ]
f [x i , x j , x k ] 2nd finite divided difference
xi xk
f [x n , x n 1 , ..., x 1 ] f [x n 1 , ..., x 1 , x 0 ] nth finite divided difference
f [x n , x n 1 , ..., x 1 , x 0 ]
xn x 0
• There nth order Newton’s Divided Difference Interpolating polynomial is
fn(x) = f(x0) + (x - x0) f[x1, x0] + (x - x0)(x - x1) f[x2, x1, x0] + . . .
+ (x - x0)(x - x1) ... (x - xn-1) f[xn, xn-1, . . ., x1, x0] 5
Example 29:
The following logarithmic table is given.
x f(x)=log(x)
4.0 0.60206 (a) Interpolate log(5) using the points x=4 and x=6
4.5 0.6532125 (b) Interpolate log(5) using the points x=4.5 and x=5.5
5.5 0.7403627 Note that the exact value is log(5) = 0.69897
6.0 0.7781513
(a) Linear interpolation. f(x) = f(x0) + (x - x0) f[x1, x0]
x0 = 4, x1 = 6 f[x1, x0] = [f(6) – f(4)] / (6 - 4) = 0.0880046
f(5) f(4) + (5 - 4) 0.0880046 = 0.690106 et = 1.27 %
(b) Again linear interpolation. But this time
x0 = 4.5, x1 = 5.5 f[x1, x0] = [f(5.5) – f(4.5)] / (5.5 - 4.5) = 0.0871502
f(5) f(4.5) + (5 – 4.5) 0.0871502 = 0.696788 et = 0.3 %
6
Example 29 (cont’d):
x f(x)=log(x) (c) Interpolate log(5) using the points x=4.5, x=5.5 and x=6
4.0 0.6020600
4.5 0.6532125
5.5 0.7403627
6.0 0.7781513
(c) Quadratic interpolation.
x0 = 4.5, x1 = 5.5 , x2 = 6 f[x1, x0] = 0.0871502 (already calculated)
f[x2, x1] = [f(6) – f(5.5)] / (6 – 5.5) = 0.0755772
f[x2, x1 , x0] = {f[x2, x1] - f[x1, x0]} / (6 – 4.5) = -0.0077153
f(5) 0.696788 + (5 - 4.5)(5 - 5.5) (-0.0077153) = 0.698717 et = 0.04 %
• Note that 0.696788 was calculate in part (b).
• Errors decrease when the points used are closer to the interpolated point.
• Errors decrease as the degree of the interpolating polynomial increases.
7
Finite Divided Difference (FDD) Table
Finite divided differences used in the Newton’s Interpolating Polynomials can be presented in a table
form. This makes the calculations much simpler.
x f( ) f[, ] f[, ,] f[, , ,]
x0 f(x0) f [x1 , x0] f [x2 , x1 , x0] f [x3 , x2 , x1 , x0]
x1 f(x1) f [x2 , x1] f [x3 , x2 , x1]
x2 f(x2) f [x3 , x2]
x3 f(x3)
Exercise 27: The first two columns of the following table is given. Calculate the missing finite
divided differences.
x f( ) f[, ] f[, ,] f[, , ,]
4 0.6020600 ? ? ?
4.5 0.6532125 ? ?
5.5 0.7403627 ?
6 0.7781513
• The numbers decrease as we go right in the table. This means that the contribution of higher order
terms are less than the lower order terms.
• This is expected. The opposite behavior is an indication of an inappropriate interpolation (see exam
questions of Fall 2006). 8
Example 30: x f( ) f[, ] f[, ,] f[, , ,]
4 0.6020600 0.1023050 -0.0101032 0.001194
4.5 0.6532125 0.0871502 -0.0077153
5.5 0.7403627 0.0755772
6 0.7781513
Use this previously calculated table to interpolate for log(5).
(a) Using points x=4 and x=4.5.
log (5) 0.60206 + (5 - 4) 0.102305 = 0.704365 et = 0.8 % (this is extrapolation)
(b) Using points x=4.5 and x=5.5.
log (5) 0.6532125 + (5 - 4.5) 0.0871502 = 0.696788 et = 0.3 %
(c) Using points x=4 and x=6.
The entries of the above table can not be used for this interpolation.
(d) Using points x=4.5 , x=5.5 and x=6.
log (5) 0.6532125 + (5-4.5) 0.0871502 + (5-4.5)(5-5.5)(-0.0077153)= 0.698717 et = 0.04 %
(e) Using all four points.
log (5) 0.60206 + (5 - 4) 0.102305 + (5 - 4)(5 - 4.5)(-0.0101032)
+ (5 - 4)(5 - 4.5)(5 – 5.5)(0.001194) = 0.6990149 et = 0.006 % 9
Exercise 28:
Create the FDD table for the given data set. Use it to
x f( )
interpolate for f(2).
-2 -0.909297
• For a linear interpolation use the points x=1 and x=3.
-1 -0.841471
• For a quadratic interpolation either use the points x=0, x=1
0 0.000000 and x=3 or the points x=1, x=3 and x=4.
1 0.841471
• For a third cubic interpolation use the points x=0, x=1, x=3
3 0.141120 and x=4.
4 -0.756802
Important: Always try to put the interpolated point at the
6 -0.279415 center of the points used for the interpolation.
Exercise 29: Complete the following table given for the log function. Do you observe anything
strange? Comment.
x f( ) f[, ] f[, ,] f[, , ,] f[, , , ,] f[, , , , ,]
0.5
1
3
5
8
10
10
Errors of Newton’s DD Interpolating Polynomials
fn(x) = f(x0) + (x - x0) f[x1, x0] + (x - x0)(x - x1) f[x2, x1, x0] + . . .
+ (x - x0)(x - x1) ... (x - xn-1) f[xn, xn-1, . . ., x1, x0]
• The structure of Newton’s Interpolating Polynomials is similar to the Taylor series.
f n 1 (x)
• Remainder (truncation error) for the Taylor series was Rn ( x i 1 x i )n 1
(n 1)!
• Similarly the remainder for the nth order interpolating polynomial is
f n 1 (x )
Rn ( x x 0 )( x x 1 ) . . . ( x x n )
(n 1)!
where x is somewhere in the interval containing the interpolated point x and other data points.
• But usually only the set of data points is given and the function f is not known.
• An alternative formulation uses a finite divided difference to approximate the (n+1)th derivative.
R n f[x , x n , x n 1 , . . . , x 0 ] (x x 0 )( x x 1 ) . . . (x x n )
• But this includes f(x) which is not known.
• Error can be predicted if an additional data point (xn+1) is availbale
R n f[x n 1 , x n , x n 1 , . . . , x 0 ] (x x 0 )( x x 1 ) . . . (x x n )
which is nothing but fn+1(x) - fn(x) 11
Newton’s Interpolating Polynomials for Equally Spaced Data
• If the data points are equally spaced and in ascending order, that is,
(x0, y0) , (x0 + h, y1) , (x0 + 2h, y1) , . . . . . , (x0 + nh, yn)
finite divided difference simplify.
f (x 1 ) f(x 0 ) f (x 0 )
f [x 1 , x 0 ]
x1 x 0 h
f (x 2 ) f(x 1 ) f (x 1 ) f(x 0 )
x2 x1 x1 x 0 f ( x 2 ) 2 f ( x 1 ) f ( x 0 ) f 2 ( x 0 )
f [x 2 , x 1 , x 0 ]
x2 x 0 2h2 2h2
f n ( x 0 )
or in general f [x n , x n 1 ,..., x 0 ]
n! hn
where fn(x0) is the nth forward difference.
• With this notation Newton’s DD Interpolating polynomials simplify to
fn(x) = f(x0) + f(x0) a + 2f(x0) a(a - 1) / 2! + . . . + nf(x0) a(a - 1) ... (a - n + 1) / n! + Rn
where a = (x - x0) / h and Rn = f (n+1)(x) hn+1 a(a - 1) ... (a - n) / (n+1)!
• This is called the forward Newton-Gregory formula.
12
Lagrange Interpolating Polynomials
• It is a reformulation of Newton’s Interpolating Polynomials.
n n x xj
fn (x ) L i (x)f (x i ) where L i (x ) xi x j
i 0 j 0
ji
x x1 x x0
• For n=1 (linear): f1 (x ) f (x 0 ) f (x 1 )
x 0 x1 x1 x 0
(x x 1 )( x x 2 ) (x x 0 )( x x 2 ) (x x 0 )( x x 1 )
• For n=2: f2 (x ) f (x 0 ) f (x 1 ) f (x 2 )
(x 0 x 1 )( x 0 x 2 ) (x 1 x 0 )( x 1 x 2 ) (x 2 x 0 )( x 2 x 1 )
• To generalize, nth order polynomial is the summation of (n+1) nth order polynomials.
• Each of these nth order polynomials have a value of 1 at one of the data points and have values of 0
at all other data points.
1 at x xi
• This is due to the following property of Lagrange functions L i ( x )
0 at all other data points
L0(x) f(x0) L1(x) f(x1) L2(x) f(x2) f2(x)
+ + =
x0 x1 x213
Example 31:
x f(x) Calculate f(4) using Lagrange Interpolating Polynomials
1 4.75 (a) of order 1
2 4.00 (b) of order 2
3 5.25 (c) of order 3
5 19.75
6 36.00
(a) Linear interpolation. Select x0 = 3, x1 = 5
f1(x) = L0(x) f(x0) + L1(x) f(x1) = (x-5)/(3-5) 5.25 + (x-3)/(5-3) 19.75
f(4) 12.5
(b) Quadratic interpolation. Select x0 = 2, x1 = 3 , x1 = 5
f2(x) = L0(x) f(x0) + L1(x) f(x1) + L2(x) f(x2)
= (x-3)(x-5)/(2-3)(2-5) 4.00 + (x-2)(x-5)/(3-2)(3-5) 5.25 + (x-2)(x-3)/(5-2)(5-3) 19.75
f(4) 10.5
Exercise 30: Solve part (b) using the last three points. Also solve part (c).
14
Spline Interpolation
• We learned how to interpolate between n+1 data points using nth order polynomials.
• For high number of data points (typically n > 6 or 7), high order polynomials are necessary, but
sometimes they suffer from oscillatory behavior.
actual function
interpolation
function
• Instead of using a single high order polynomial that passes through all data points, we can use
different lower order polynomials between each data pair.
• These lower order polynomials that pass through only two points are called splines.
• Third order (cubic) splines are the most preferred ones.
first order splines :
15
Linear Splines:
• Given a set of ordered data points, each two point can be connected using a straight line.
f(x) f(x) = f(x0) + m0(x - x0) for x0 x x1
f(x) = f(x1) + m1(x - x1) for x1 x x2
f(x) = f(x2) + m2(x - x2) for x2 x x3
x
where the slopes are mi = [f(xi+1) – f(xi)] / (xi+1 - xi)
x0 x1 x2 x3
• Functions are not continuous at the interior points.
Quadratic Splines:
• Every pair of data points are connected using quadratic functions.
a2x2+b2x+c2
f(x) a1x2+b1x+c1 anx2+bnx+cn
• For n+1 data points, there are n splines
and 3n unknown constants.
.... • We need 3n equations to solve for them.
x
x0 x1 x2 .... xn-1 xn 16
Quadratic Splines (cont’d):
• These 3n equations are
• The first and last functions must pass through the end points (2 equations).
a1 x02 + b1 x0 + c1 = f(x0)
an xn2 + bn xn + cn = f(xn)
• The function values must be equal at interior points (2n-2 equations).
ai-1 xi-12 + bi-1 xi-1 + ci-1 = f(xi-1)
for i = 2 to n
ai xi-1 + bi xi-1 + ci = f(xi-1)
2
• First derivatives must be equal at the interior points (n-1 equations).
2 ai-1 xi-1 + bi-1 = 2 ai xi-1 + bi for i = 1 to n
• This makes a total of 3n-1 equations. One more equation is necessary and we need to make an
arbitrary choice. Among many possibilities we will use the following,
• Take the second derivative at the first point to be zero (1 equation).
a1 = 0 i.e. first two points are connected with a straight line.
• Solve this set of 3n linear algebraic equations with any of the methods that we learned.
17
Cubic Splines:
• For n+1 points, there will be n intervals and for each interval there will be a 3rd order polynomial
ai xi3 + bi xi2 + ci x + di for i = 1 to n
• Totally there are 4n unknowns. They can be solved using the following equations
• The first and last functions must pass through the end points (2 equations).
• The function values must be equal at interior points (2n-2 equations).
• First derivatives must be equal at the interior points (n-1 equations).
• Second derivatives must be equal at the interior points (n-1 equations).
• This makes a total of 4n-2 equations. Two extra equations are (other choices are possible)
• Second derivatives at the end points are zero (2 equations).
• Setting up and solving 4n equations is costly. There is another way of constructing cubic splines that
results in only n-1 equations in n-1 unknowns. See pages 502-503 of the book.
18
Example 32:
Develop quadratic splines for these data points and predict f(3.4) and f(2.2)
x f(x)
1 1 ai x2 + bi x + ci
f(x)
2 5
2.5 7
3 8
4 2 x
x0=1 2 2.5 3 4
• There are 5 points and n=4 splines. Totally there are 3n=12 unknowns. Equations are
• End points: a1 12+ b1 1 + c1 = 1 , a4 42 + b4 4 + c4 = 2
• Interior points: a1 22 + b1 2 + c1 = 5 , a2 22 + b2 2 + c2 = 5
a2 2.52 + b2 2.5 + c2 = 7, a3 2.52 + b3 2.5 + c3 = 7
a3 32 + b3 3 + c3 = 8 , a4 32 + b4 3 + c4 = 8
• Derivatives at the interior points: 2a12 + b1 = 2a2 2 + b2
2a2 2.5 + b2 = 2a3 2.5 + b3
2a3 3 + b3 = 2a4 3 + b4
• Arbitrary choice for the missing equation: a1 = 0 19
Example 32 (cont’d):
• a1=0 is already known. Solve for the remaining 11 unknowns.
1 1 0 0 0 0 0 0 0 0 0 b1 1 b1 4
0
0 0 0 0 0 0 0 16 4 1 c
1
2
c
1
-3
2 1 0 0 0 0 0 0 0 0 0 a2 5 a2 0
0 0 4 2 1 0 0 0 0 0 0 b2 5 b2 4
0 0 6.25 2.5 1 0 0 0 0 0 0 c2 7 c2 -3
0 0 0 0 0 6.25 2.5 1 0 0 0 a3 7 a3 - 4
0 0 0 0 0 9 3 1 0 0 0 b 8 b 24
3 3
0 0 0 0 0 0 0 0 9 3 1 c3 8 c3 - 28
1
0 4 1 0 0 0 0 0 0 0 a
0 a
- 6
4 4
0 0 5 1 0 5 1 0 0 0 0 b 4 0 b 4 36
0 0 0 0 0 6 1 0 6 1 0 c 4 0 c 4 46
• Equations for the splines are
1st spline: f(x) = 4x – 3 (Straight line.)
2nd spline: f(x) = 4x – 3 (Same as the 1st. Coincidence)
3rd spline: f(x) = -4x2 + 24x – 28
4th spline: f(x) = -6x2 + 36x – 46
• To predict f(3.4) use the 4th spline. f(3.4) = -6 (3.4)2 + 36 (3.4) – 46 = 7.04
To predict f(2.2) use the 2nd spline. f(2.2) = 4 (2.2) – 3 = 5.8 20