1. 1
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
2. 2
• 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.
Interpolation
• 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.
x
f(x)
Spline Interpolation
x
f(x)
Polynomial Interpolation
extrapolation
interpolation
3. 3
• 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:
Polynomial Interpolation
• Given: (x0, y0) and (x1, y1)
• A straight line passes from these two points.
• Using similar triangles
x
a0 + a1 x
x1
f(x) = ?
y0 = f(x0)
x0 x
y1 = f(x1)
0
1
0
1
0
0
x
x
)
x
(
f
)
x
(
f
x
x
)
x
(
f
)
x
(
f
)
x
x
(
x
x
)
x
(
f
)
x
(
f
)
x
(
f
)
x
(
f 0
0
1
0
1
0
Linear interpolation formula
)
x
x
(
b
b
)
x
(
f 0
1
0
1
or
4. 4
• Quadratic Interpolation:
Polynomial Interpolation
• Given: (x0, y0) , (x1, y1) and (x2, y2)
• A parabola passes from these three points.
• Similar to the linear case, the equation of
this parabola can be written as
)
x
x
)(
x
x
(
b
)
x
x
(
b
b
)
x
(
f 1
0
2
0
1
0
2
Quadratic interpolation formula
• How to find b0, b1 and b2 in terms of given quantities?
• at x=x0 f2(x) = f(x0) = b0
• at x=x1 f2(x) = f(x1) = b0 + b1x1
• at x=x2 f2(x) = f(x2) = b0 + b1(x2-x0)+ b2 (x2-x0) (x2-x1)
x
x1
y0 = f(x0)
x0 x2
a0 + a1 x + a2 x2
y1 = f(x1)
y2 = f(x2)
)
f(x
b 0
0
0
1
0
1
1
x
x
)
f(x
)
x
(
f
b
0
2
0
1
0
1
1
2
1
2
2
x
x
x
x
)
f(x
)
x
(
f
x
x
)
f(x
)
x
(
f
b
5. 5
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
j
i
j
i
j
i
x
x
)
f(x
)
x
(
f
]
x
,
x
[
f
k
i
k
j
j
i
k
j
i
x
x
]
x
,
x
[
f
]
x
,
x
[
f
]
x
,
x
,
x
[
f
1st finite divided difference
2nd finite divided difference
0
n
0
1
1
n
1
1
n
n
0
1
1
n
n
x
x
]
x
,
x
...,
,
x
[
f
]
x
...,
,
x
,
x
[
f
]
x
,
x
...,
,
x
,
x
[
f
nth finite divided difference
• 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]
6. 6
Example 29:
The following logarithmic table is given.
(a) Interpolate log(5) using the points x=4 and x=6
(b) Interpolate log(5) using the points x=4.5 and x=5.5
Note that the exact value is log(5) = 0.69897
x f(x)=log(x)
4.0 0.60206
4.5 0.6532125
5.5 0.7403627
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 %
7. 7
Example 29 (cont’d):
(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.
(c) Interpolate log(5) using the points x=4.5, x=5.5 and x=6
x f(x)=log(x)
4.0 0.6020600
4.5 0.6532125
5.5 0.7403627
6.0 0.7781513
8. 8
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).
9. 9
Example 30:
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 %
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
10. 10
Exercise 28:
Create the FDD table for the given data set. Use it to
interpolate for f(2).
• For a linear interpolation use the points x=1 and x=3.
• For a quadratic interpolation either use the points x=0, x=1
and x=3 or the points x=1, x=3 and x=4.
• For a third cubic interpolation use the points x=0, x=1, x=3
and x=4.
Important: Always try to put the interpolated point at the
center of the points used for the interpolation.
x f( )
-2 -0.909297
-1 -0.841471
0 0.000000
1 0.841471
3 0.141120
4 -0.756802
6 -0.279415
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
11. 11
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.
• Remainder (truncation error) for the Taylor series was
• Similarly the remainder for the nth order interpolating polynomial is
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.
• But this includes f(x) which is not known.
• Error can be predicted if an additional data point (xn+1) is availbale
which is nothing but fn+1(x) - fn(x)
1
n
i
1
i
1
n
n )
x
x
(
)!
1
n
(
)
(
f
R
x
)
x
x
(
.
.
.
)
x
x
)(
x
x
(
)!
1
n
(
)
(
f
R n
1
0
1
n
n
x
)
x
x
(
.
.
.
)
x
x
)(
x
x
(
]
x
,
.
.
.
,
x
,
x
,
x
[
f
R n
1
0
0
1
n
n
n
)
x
x
(
.
.
.
)
x
x
)(
x
x
(
]
x
,
.
.
.
,
x
,
x
,
x
[
f
R n
1
0
0
1
n
n
1
n
n
12. 12
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.
n
0
n
0
1
n
n
2
0
2
2
0
1
2
0
2
0
1
0
1
1
2
1
2
0
1
2
0
0
1
0
1
0
1
h
!
n
)
x
(
f
]
x
,...,
x
,
x
[
f
general
in
or
h
2
)
x
(
f
h
2
)
x
(
f
)
x
(
f
2
)
x
(
f
x
x
x
x
)
f(x
)
x
(
f
x
x
)
f(x
)
x
(
f
]
x
,
x
,
x
[
f
h
)
x
(
f
x
x
)
f(x
)
x
(
f
]
x
,
x
[
f
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.
13. 13
Lagrange Interpolating Polynomials
• It is a reformulation of Newton’s Interpolating Polynomials.
• For n=1 (linear):
n
0
i
n
i
j
0
j j
i
j
i
i
i
n
x
x
x
x
)
x
(
L
e
wher
)
x
(
f
)
x
(
L
)
x
(
f
)
x
(
f
x
x
x
x
)
x
(
f
x
x
x
x
)
x
(
f 1
0
1
0
0
1
0
1
1
• For n=2: )
x
(
f
)
x
x
)(
x
x
(
)
x
x
)(
x
x
(
)
x
(
f
)
x
x
)(
x
x
(
)
x
x
)(
x
x
(
)
x
(
f
)
x
x
)(
x
x
(
)
x
x
)(
x
x
(
)
x
(
f 2
1
2
0
2
1
0
1
2
1
0
1
2
0
0
2
0
1
0
2
1
2
• 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.
• This is due to the following property of Lagrange functions
+ + =
points
data
other
all
at
0
x
x
at
1
)
x
(
L i
i
x1
x0 x2
L0(x) f(x0) L1(x) f(x1) L2(x) f(x2) f2(x)
14. 14
Example 31:
Calculate f(4) using Lagrange Interpolating Polynomials
(a) of order 1
(b) of order 2
(c) of order 3
x f(x)
1 4.75
2 4.00
3 5.25
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).
15. 15
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.
• 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 :
actual function
interpolation
function
16. 16
Linear Splines:
• Given a set of ordered data points, each two point can be connected using a straight line.
x
f(x)
x0 x1 x2 x3
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
where the slopes are mi = [f(xi+1) – f(xi)] / (xi+1 - xi)
• Functions are not continuous at the interior points.
Quadratic Splines:
• Every pair of data points are connected using quadratic functions.
• For n+1 data points, there are n splines
and 3n unknown constants.
• We need 3n equations to solve for them.
x
f(x)
x0 x1 x2 . . . . xn-1
a1x2+b1x+c1
a2x2+b2x+c2
anx2+bnx+cn
xn
. . . .
17. 17
Quadratic Splines (cont’d):
• These 3n equations are
• The first and last functions must pass through the end points (2 equations).
a1 x0
2 + b1 x0 + c1 = f(x0)
an xn
2 + bn xn + cn = f(xn)
• The function values must be equal at interior points (2n-2 equations).
ai-1 xi-1
2 + bi-1 xi-1 + ci-1 = f(xi-1)
ai xi-1
2 + bi xi-1 + ci = f(xi-1)
• First derivatives must be equal at the interior points (n-1 equations).
2 ai-1 xi-1 + bi-1 = 2 ai xi-1 + bi
• 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.
for i = 2 to n
for i = 1 to n
18. 18
Cubic Splines:
• For n+1 points, there will be n intervals and for each interval there will be a 3rd order polynomial
ai xi
3 + bi xi
2 + 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.
19. 19
Example 32:
Develop quadratic splines for these data points and predict f(3.4) and f(2.2)
x f(x)
1 1
2 5
2.5 7
3 8
4 2
• 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
x
f(x)
x0=1 2 4
ai x2 + bi x + ci
2.5 3