You are on page 1of 56

> 4.

Interpolation and Approximation


Most functions cannot be evaluated exactly:

x, e
x
, lnx, trigonometric functions
since by using a computer we are limited to the use of elementary
arithmetic operations
+, , ,
With these operations we can only evaluate polynomials and rational
functions (polynomial divided by polynomials).
4. Interpolation Math 1070
> 4. Interpolation and Approximation
Interpolation
Given points
x
0
, x
1
, . . . , x
n
and corresponding values
y
0
, y
1
, . . . , y
n
nd a function f(x) such that
f(x
i
) = y
i
, i = 0, . . . , n.
The interpolation function f is usually taken from a restricted class of
functions: polynomials.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1 Polynomial Interpolation Theory
Interpolation of functions
f(x)
x
0
, x
1
, . . . , x
n
f(x
0
), f(x
1
), . . . , f(x
n
)
Find a polynomial (or other special function) such that
p(x
i
) = f(x
i
), i = 0, . . . , n.
What is the error f(x) = p(x)?
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.1 Linear interpolation
Linear interpolation
Given two sets of points (x
0
, y
0
) and (x
1
, y
1
) with x
0
= x
1
, draw a line
through them, i.e., the graph of the linear polynomial
x
0
x
1
y
0
y
1
(x) =
x x
1
x
0
x
1
y
0
+
x x
0
x
1
x
0
y
1
(x) =
(x
1
x)y
0
+ (x x
0
)y
1
x
1
x
0
(5.1)
We say that (x) interpolates the value y
i
at the point x
i
, i = 0, 1, or
(x
i
) = y
i
, i = 0, 1
Figure: Linear interpolation
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.1 Linear interpolation
Example
Let the data points be (1, 1) and (4,2). The polynomial P
1
(x) is given by
P
1
(x) =
(4 x) 1 + (x 1) 2
3
(5.2)
The graph y = P
1
(x) and y =

x, from which the data points were taken.


Figure: y =

x and its linear interpolating polynomial (5.2)


4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.1 Linear interpolation
Example
Obtain an estimate of e
0.826
using the function values
e
0.82

= 2.270500, e
0.83

= 2.293319
Denote x
0
= 0.82, x
1
= 0.83. The interpolating polynomial P
1
(x)
interpolating e
x
at x
0
and x
1
is
P
1
(x) =
(0.83 x) 2.270500 + (x 0.82) 2.293319
0.01
(5.3)
and
P
1
(0.826) = 2.2841914
while the true value s
e
0.826

= 2.2841638
to eight signicant digits.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.2 Quadratic Interpolation
Assume three data points (x
0
, y
0
), (x
1
, y
1
), (x
2
, y
2
), with x
0
, x
1
, x
2
distinct.
We construct the quadratic polynomial passing through these points using
Lagranges folmula
P
2
(x) = y
0
L
0
(x) + y
1
L
1
(x) + y
2
L
2
(x) (5.4)
with Lagrange interpolation basis functions for quadratic interpolating
polynomial
L
0
(x) =
(xx
1
)(xx
2
)
(x
0
x
1
)(x
0
x
2
)
L
1
(x) =
(xx
0
)(xx
2
)
(x
1
x
0
)(x
1
x
2
)
L
2
(x) =
(xx
0
)(xx
1
)
(x
2
x
0
)(x
2
x
1
)
(5.5)
Each L
i
(x) has degree 2 P
2
(x) has degree 2. Moreover
L
i
(x
j
) = 0, j = i
L
i
(x
i
) = 1
for 0 i, j 2 i.e., L
i
(x
j
) =
i,j
=
_
1, i = j
0, i = j
the Kronecker delta function.
P
2
(x) interpolates the data
P
2
(x) = y
i
, i=0,1,2
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.2 Quadratic Interpolation
Example
Construct P
2
(x) for the data points (0, 1), (1, 1), (2, 7). Then
P
2
(x)=
(x1)(x2)
2
(1)+
x(x2)
1
(1)+
x(x1)
2
7 (5.6)
Figure: The quadratic interpolating polynomial (5.6)
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.2 Quadratic Interpolation
With linear interpolation: obvious that there is only one straight line
passing through two given data points.
With three data points: only one quadratic interpolating polynomial
whose graph passes through the points.
Indeed: assume Q
2
(x), deg(Q
2
) 2 passing through
(x
i
, y
i
), i = 0, 1, 2, then it is equal to P
2
(x). The polynomial
R(x) = P
2
(x) Q
2
(x)
has deg(R) 2 and
R(x
i
) = P
2
(x
i
) Q
2
(x
i
) = y
i
y
i
= 0, for i = 0, 1, 2
So R(x) is a polynomial of degree 2 with three roots R(x) 0
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.2 Quadratic Interpolation
Example
Calculate a quadratic interpolate to e
0.826
from the function values
e
0.82

= 2.27050 e
0.83

= 2.293319 e
0.84

= 2.231637
With x
0
= e
0.82
, x
1
= e
0.83
, x
2
= e
0.84
, we have
P
2
(0.826)

= 2.2841639
to eight digits, while the true answer e
0.826

= 2.2841638 and
P
1
(0.826)

= 2.2841914.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.3 Higher-degree interpolation
Lagranges Formula
Given n + 1 data points (x
0
, y
0
), (x
1
, y
1
), . . . , (x
n
, y
n
) with all x
i
s
distinct, unique P
n
, deg(P
n
) n such that
P
n
(x
i
) = y
i
, i = 0, . . . , n
given by Lagranges Formula
P
n
(x) =
n

i=0
y
i
L
i
(x) = y
0
L
0
(x) + y
1
L
1
(x) + y
n
L
n
(x) (5.7)
where L
i
(x)=
n

j=0,j=i
xx
j
x
i
x
j
=
(xx
0
) (xx
i1
)(xx
i+1
) (xx
n
)
(x
i
x
0
) (x
i
x
i1
)(x
i
x
i
) (x
i
x
n
)
,
where L
i
(x
j
) =
ij
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.4 Divided dierences
Remark; The Lagranges formula (5.7) is suited for theoretical uses,
but is impractical for computing the value of an interpolating polynomial:
knowing P
2
(x) does not lead to a less expensive way to compute P
3
(x).
But for this we need some preliminaries, and we start with a discrete version
of the derivative of a function f(x).
Denition (First-order divided dierence)
Let x
0
= x
1
, we dene the rst-order divided dierence of f(x) by
f[x
0
, x
1
] =
f(x
1
) f(x
2
)
x
1
x
2
(5.8)
If f(x) is dierentiable on an interval containing x
0
and x
1
, then the mean
value theorem gives
f[x
0
, x
1
] = f

(c), for c between x


0
and x
1
.
Also if x
0
, x
1
are close together, then
f[x
0
, x
1
] f

_
x
0
+ x
1
2
_
usually a very good approximation.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.4 Divided dierences
Example
Let f(x) = cos(x), x
0
= 0.2, x
1
= 0.3.
Then
f[x
0
, x
1
] =
cos(0.3) cos(0.2)
0.3 0.2

= 0.2473009 (5.9)
while
f

_
x
0
+ x
1
2
_
= sin(0.25)

= 0.2474040
so f[x
0
, x
1
] is a very good approximation of this derivative.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.4 Divided dierences
Higher-order divided dierences are dened recursively
Let x
0
, x
1
, x
2
R distinct. The second-order divided
dierence
f[x
0
, x
1
, x
2
] =
f[x
1
, x
2
] f[x
0
, x
1
]
x
2
x
0
(5.10)
Let x
0
, x
1
, x
2
, x
3
R distinct. The third-order divided
dierence
f[x
0
, x
1
, x
2
, x
3
] =
f[x
1
, x
2
, x
3
] f[x
0
, x
1
, x
2
]
x
3
x
0
(5.11)
In general, let x
0
, x
1
, . . . , x
n
R, n + 1 distinct numbers. The
divided dierence of order n
f[x
0
, . . . , x
n
] =
f[x
1
, . . . , x
n
] f[x
0
, . . . , x
n1
]
x
n
x
0
(5.12)
or the Newton divided dierence.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.4 Divided dierences
Theorem
Let n 1, f C
n
[, ] and x
0
, x
1
, . . . , x
n
n + 1 distinct numbers in [, ].
Then
f[x
0
, x
1
, . . . , x
n
] =
1
n!
f
(n)
(c) (5.13)
for some unknown point c between the maximum and the minimum of
x
0
, . . . , x
n
.
Example
Let f(x) = cos(x), x
0
= 0.2, x
1
= 0.3, x
2
= 0.4.
The f[x
0
, x
1
] is given by (5.9), and
f[x
1
, x
2
] =
cos(0.4) cos(0.3)
0.4 0.3

= 0.3427550
hence from (5.11)
f[x
0
, x
1
, x
2
]

=
0.3427550 (0.2473009)
0.4 0.2
= 0.4772705 (5.14)
For n = 2, (5.13) becomes
f[x
0
, x
1
, x
2
] =
1
2
f

(c) =
1
2
cos(c)
1
2
cos(0.3)

= 0.4776682
which is nearly equal to the result in (5.14).
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.5 Properties of divided dierences
The divided dierences (5.12) have special properties that help simplify work
with them.
(1) Let (i
0
, i
1
, . . . , i
n
) be a permutation (rearrangement) of the integers
(0, 1, . . . , n). It can be shown that
f[x
i
0
, x
i
1
, . . . , x
i
n
] = f[x
0
, x
1
, . . . , x
n
] (5.15)
The original denition (5.12) seems to imply that the order of x
0
, x
1
, . . . , x
n
is important, but (5.15) asserts that it is not true.
For n = 1
f[x
0
, x
1
] =
f(x
0
) f(x
1
)
x
0
x
1
=
f(x
1
) f(x
0
)
x
1
x
0
= f[x
1
, x
0
]
For n = 2 we can expand (5.11) to get
f[x
0
, x
1
, x
2
] =
f(x
0
)
(x
0
x
1
)(x
0
x
2
)
+
f(x
1
)
(x
1
x
0
)(x
1
x
2
)
+
f(x
2
)
(x
2
x
0
)(x
2
x
1
)
(2) The denitions (5.8), (5.11) and (5.12) extend to the case where some or
all of the x
i
coincide, provided that f(x) is suciently dierentiable.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.5 Properties of divided dierences
For example, dene
f[x
0
, x
0
] = lim
x
1
x
0
f[x
0
, x
1
] = lim
x
1
x
0
f(x
0
) f(x
1
)
x
1
x
0
f[x
0
, x
0
] = f

(x
0
)
For an arbitrary n 1, let all x
i
x
0
; this leads to the denition
f[x
0
, . . . , x
0
] =
1
n!
f
(n)
(x
0
) (5.16)
For the cases where only some of nodes coincide: using (5.15), (5.16)
we can extend the denition of the divided dierence.
For example
f[x
0
, x
1
, x
0
] = f[x
0
, x
0
, x
1
] =
f[x
0
, x
1
] f[x
0
, x
0
]
x
1
x
0
=
f[x
0
, x
1
] f

(x
0
)
x
1
x
0
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.5 Properties of divided dierences
MATLAB program: evaluating divided dierences
Given a set of values f(x
0
), . . . , f(x
n
) we need to calculate the set of
divided dierences
f[x
0
, x
1
], f[x
0
, x
1
, x
2
], . . . , f[x
0
, x
1
, . . . , x
n
]
We can use the MATLAB function divdif using the function call
divdif y = divdif(x nodes, y values)
Note that MATLAB does not allow zero subscripts, hence x nodes and
y values have to be redened as vectors containing n +1 components:
x nodes = [x
0
, x
1
, . . . , x
n
]
x nodes(i) = x
i1
, i = 1, . . . , n + 1
y values = [f(x
0
), f(x
1
), . . . , f(x
n
)]
y values(i) = f(x
i1
), i = 1, . . . , n + 1
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.5 Properties of divided dierences
MATLAB program: evaluating divided dierences
function divdif y = divdif(x nodes,y values) %
% This is a function
% divdif y = divdif(x nodes,y values)
% It calculates the divided differences of the function
% values given in the vector y values, which are the values of
% some function f(x) at the nodes given in x nodes. On exit,
% divdif y(i) = f[x 1,...,x i], i=1,...,m
% with m the length of x nodes. The input values x nodes and
% y values are not changed by this program.
%
divdif y = y values;
m = length(x nodes);
for i=2:m
for j=m:-1:i
divdif y(j) = (divdif y(j)-divdif y(j-1)) ...
/(x nodes(j)-x nodes(j-i+1));
end
end
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.5 Properties of divided dierences
This program is illustrated in this table.
i x
i
cos(x
i
) D
i
0 0.0 1.000000 0.1000000E+1
1 0.1 0.980067 -0.9966711E-1
2 0.4 0.921061 -0.4884020E+0
3 0.6 0.825336 0.4900763E-1
4 0.8 0.696707 0.3812246E-1
5 1.0 0.540302 -0.3962047E-2
6 1.2 0.36358 -0.1134890E-2
Table: Values and divided dierences for cos(x)
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.6 Newtons Divided Dierences
Newton interpolation formula
Interpolation of f at x
0
, x
1
, . . . , x
n
Idea: use P
n1
(x) in the denition of P
n
(x):
P
n
(x) = P
n1
(x) + C(x)
C(x) P
n
correction term
C(x
i
) = 0 i = 0, . . . , n 1
_
=
=C(x) = a(x x
0
)(x x
1
) . . . (x x
n1
)
P
n
(x) = ax
n
+ . . .
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.6 Newtons Divided Dierences
Denition: The divided dierence of f(x) at points x
0
, . . . , x
n
is denoted by f[x
0
, x
1
, . . . , x
n
] and is dened to be the coecient of
x
n
in P
n
(x; f).
C(x) = f[x
0
, x
1
, . . . , x
n
](x x
0
) . . . (x x
n1
)
p
n
(x; f) = p
n1
(x; f)+f[x
0
, x
1
, . . . , x
n
](xx
0
) . . . (xx
n1
) (5.17)
p
1
(x; f) = f(x
0
) + f[x
0
, x
1
](x x
0
) (5.18)
p
2
(x; f)=f(x
0
)+f[x
0
, x
1
](xx
0
)+f[x
0
, x
1
, x
2
](xx
0
)(xx
1
) (5.19)
.
.
.
p
n
(x; f) = f(x
0
) + f[x
0
, x
1
](x x
0
) + . . . (5.20)
+f[x
0
, . . . , x
n
](x x
0
) . . . (x x
n1
)
=Newton interpolation formula
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.6 Newtons Divided Dierences
For (5.18), consider p
1
(x
0
), p
1
(x
1
). Easily, p
1
(x
0
) = f(x
0
), and
p
1
(x
1
)=f(x
0
)+(x
1
x
0
)
_
f(x
1
)f(x
0
)
x
1
x
0
_
=f(x
0
)+[f(x
1
)f(x
0
)] =f(x
1
)
So: deg(p
1
) 1 and satises the interpolation conditions. Then the
uniqueness of polynomial interpolation (5.18) is the linear
interpolation polynomial to f(x) at x
0
, x
1
.
For (5.19), note that
p
2
(x) = p
1
(x) + (x x
0
)(x x
1
)f[x
0
, x
1
, x
2
]
It satises: deg(P
2
) 2 and
p
2
(x
i
) = p
1
(x
i
) + 0 = f(x
i
), i = 0, 1
p
2
(x
2
) = f(x
0
) + (x
2
x
0
)f[x
0
.x
1
] + (x
2
x
0
)(x
2
x
1
)f[x
0
, x
1
, x
2
]
= f(x
0
) + (x
2
x
0
)f[x
0
.x
1
] + (x
2
x
1
){f[x
1
, x
2
] f[x
0
, x
1
]}
= f(x
0
) + (x
1
x
0
)f[x
0
.x
1
] + (x
2
x
1
)f[x
1
, x
2
]
= f(x
0
) +{f(x
1
) f(x
0
)} +{f(x
2
) f(x
1
)} = f(x
2
)
By the uniqueness of polynomial interpolation, this is the quadratic
interpolating polynomial to f(x) at {x
0
, x
1
, x
2
}.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.6 Newtons Divided Dierences
Example
Find p(x) P
2
such that p(1) = 0, p(0) = 1, p(1) = 4.
p(x) = p(x
0
) + p[x
0
, x
1
](x x
0
) + p[x
0
, x
1
, x
2
](x x
0
)(x x
1
)
x
i
p(x
i
) p[x
i
, x
i+1
] p[x
i
, x
i+1
, x
i+1
]
1 0 1 1
0 1 3
1 4
p(x) = 0 + 1(x + 1) + 1(x + 1)(x 0) = x
2
+ 2x + 1
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.6 Newtons Divided Dierences
Example
Let f(x) = cos(x). The previous table contains a set of nodes x
i
, the values
f(x
i
) and the divided dierences computed with divdif
D
i
= f[x
0
, . . . , x
i
] i 0
p
n
(0.1) p
n
(0.3) p
n
(0.5)
1 0.9900333 0.9700999 0.9501664
2 0.9949173 0.9554478 0.8769061
3 0.9950643 0.9553008 0.8776413
4 0.9950071 0.9553351 0.8775841
5 0.9950030 0.9553369 0.8775823
6 0.9950041 0.9553365 0.8775825
True 0.9950042 0.9553365 0.8775826
Table: Interpolation to cos(x) using (5.20)
This table contains the values of p
n
(x) for various values of n, computed
with interp, and the true values of f(x).
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.6 Newtons Divided Dierences
In general, the interpolation node points x
i
need not to be evenly spaced,
nor be arranged in any particular order
to use the divided dierence interpolation formula (5.20)
To evaluate (5.20) eciently we can use a nested multiplication
algorithm
P
n
(x) = D
0
+ (x x
0
)D
1
+ (x x
0
)(x x
1
)D
2
+ . . . + (x x
0
) (x x
n1
)D
n
with D
0
= f(x
0
), D
i
= f[x
0
, . . . , x
i
] for i 1
P
n
(x) = D
0
+ (x x
0
)[D
1
+ (x x
1
)[D
2
+ (5.21)
+ (x x
n2
)[D
n1
+ (x x
n1
)D
n
] ]]
For example
P
3
(x) = D
0
+ (x x
0
)D
1
+ (x x
1
)[D
2
+ (x x
2
)D
3
]
(5.21) uses only n multiplications to evaluate P
n
(x) and is more convenient
for a xed degree n. To compute a sequence of interpolation polynomials of
increasing degree is more ecient to se the original form (5.20).
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.1.6 Newtons Divided Dierences
MATLAB: evaluating Newton divided dierence for polynomial interpolation
function p eval = interp(x nodes,divdif y,x eval) %
% This is a function
% p eval = interp(x nodes,divdif y,x eval)
% It calculates the Newton divided difference form of
% the interpolation polynomial of degree m-1, where the
% nodes are given in x nodes, m is the length of x nodes,
% and the divided differences are given in divdif y. The
% points at which the interpolation is to be carried out
% are given in x eval; and on exit, p eval contains the
% corresponding values of the interpolation polynomial.
%
m = length(x nodes);
p eval = divdif y(m)*ones(size(x eval));
for i=m-1:-1:1
p eval = divdif y(i) + (x eval - x nodes(i)).*p eval;
end
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2 Error in polynomial interpolation
Formula for error E(t) = f(t) p
n
(t; f)
P
n
(x) =
n

j=0
f(x
j
)L
j
(x)
Theorem
Let n 0, f C
n+1
[a, b], x
0
, x
1
, . . . , x
n
distinct points in [a, b]. Then
f(x) P
n
(x) = (x x
0
) . . . (x x
n
)f[x
0
, x
1
, . . . , x
n
, x] (5.22)
=
(x x
0
)(x x
1
) (x x
n
)
(n + 1)!
f
(n+1)
(c
x
) (5.23)
for x [a, b], c
x
unknown between the minimum and maximum of
x
0
, x
1
, . . . , x
n
.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2 Error in polynomial interpolation
Proof of Theorem
Fix t R. Consider
p
n+1
(x; f) interpolates f at x
0
, . . . , x
n
, t
p
n
(x; f) interpolates f at x
0
, . . . , x
n
From Newton
p
n+1
(x; f) = p
n
(x; f) + f[x
0
, . . . , x
n
, t](x x
0
) . . . (x x
n
)
Let x = t.
f(t) := p
n+1
(t, f) = p
n
(t; f) + f[x
0
, . . . , x
n
, t](t x
0
) . . . (t x)
E(t) := f[x
0
, x
1
, . . . , x
n
, t](t x
0
) . . . (t x
n
)
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2 Error in polynomial interpolation
Examples
f(x) = x
n
, f[x
0
, . . . , x
n
] =
n!
n!
= 1 or
p
n
(x; f) = 1 x
n
f[x
0
, . . . , x
n
] = 1
f(x) P
n1
, f[x
0
, x
1
, . . . , x
n
] = 0
f(x) = x
n+1
, f[x
0
, . . . , x
n
] = x
0
+ x
1
+ . . . + x
n
R(x) = x
n+1
p
n
(x; f) P
n+1
R(x
i
) = 0, i = 0, . . . , n
R(x
i
) = (x x
0
) . . . (x x
n
) = x
n+1
(x
0
+ . . . + x
n
)x
n
+ . . .
. .
P
n
=f[x
0
, . . . , x
n
] = x
0
+ x
1
+ . . . + x
n
.
If f P
m
,
f[x
0
, . . . , x
m
, x] =
_
polynomial degree mn 1 if n m1
0 if n > m1
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2 Error in polynomial interpolation
Example
Take f(x) = e
x
, x [0, 1] and consider the error in linear interpolation
to f(x) using x
0
, x
1
satisfying 0 x
0
x
1
1.
From (5.23)
e
x
P
1
(x) =
(x x
0
)(x x
1
)
2
e
c
x
for some c
x
between the max and min of x
0
, x
1
and x. Assuming
x
0
< x < x
1
, the error is negative and approximately a quadratic
polynomial
e
x
P
1
(x) =
(x
1
x)(x x
0
)
2
e
c
x
Since x
0
c
x
x
1
(x
1
x)(x x
0
)
2
e
x
0
|e
x
P
1
(x)| =
(x
1
x)(x x
0
)
2
e
x
1
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2 Error in polynomial interpolation
For a bound independent of x
max
x
0
xx
1
(x
1
x)(x x
0
)
2
=
h
2
8
, h = x
1
x
0
and e
x
1
e on [0, 1]
|e
x
P
1
(x)|
h
2
8
e, 0 x
0
x x
1
1
independent of x, x
0
, x
1
.
Recall that we estimated e
0.826

= 2.2841914 by e
0.82
and e
0.83
, i.e.,
h = 0.01.
|e
x
P
1
(x)|
h
2
8
e = |e
x
P
1
(x)|
0.01
2
8
2.72 = 0.0000340,
The actual error being 0.0000276, it satises this bound.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2 Error in polynomial interpolation
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2 Error in polynomial interpolation
Example
Again let f(x) = e
x
on [0, 1], but consider the quadratic interpolation.
e
x
P
2
(x) =
(x x
0
)(x x
1
)(x x
2
)
6
e
c
x
for some c
x
between the min and max of x
0
, x
1
, x
2
and x.
Assuming evenly spaced points, h = x
1
x
0
= x
2
x
1
, and
0 x
0
< x < x
2
1, we have as before
|e
x
P
2
(x)|

(x x
0
)(x x
1
)(x x
2
)
6

e
1
while
max
x
0
xx
2
(x x
0
)(x x
1
)(x x
2
)
6
=
h
3
9

3
(5.24)
hence
|e
x
P
2
(x)|
h
3
e
9

3
0.174h
3
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2 Error in polynomial interpolation
For h = 0.01, 0 x 1
|e
x
P
2
(x)| 1.74 10
7
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2 Error in polynomial interpolation
Let
w
2
(x) =
(x + h)x(x h)
6
=
x
3
xh
6
a translation along x-axis of polynomial in (5.24). Then x =
h

3
satises
0 = w

2
(x) =
3x
2
h
2
6
and gives

w
2
_

3
_

=
h
3
9

3
Figure: y = w
2
(x)
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2.2 Behaviour of the error
When we consider the error formula (5.23) or (5.22), the polynomial

n
(x) = (x x
0
) (x x
n
)
is crucial in determining the behaviour of the error. Let assume that
x
0
, . . . , x
n
are evenly spaced and x
0
x x
n
.
Figure: y =
6
(x)
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2.2 Behaviour of the error
Remark that the interpolation error
is relatively larger in [x
0
, x
1
] and [x
5
, x
6
], and
is likely to be smaller near the middle of the node points
In practical interpolation problems, high-degree polynomial interpolation
with evenly spaced nodes is seldom used.
But when the set of nodes is suitable chosed, high-degree polynomials can be
very useful in obtaining polynomial approximations to functions.
Example
Let f(x) = cos(x), h = 0.2, n = 8 and then interpolate at x = 0.9.
Case (i) x
0
= 0.8, x
8
= 2.4 x = 0.9 [x
0
, x
1
]. By direct calculation of
P
8
(0.9),
cos(0.9) P
8
(0.9)

= 5.51 10
9
Case (ii) x
0
= 0.2, x
8
= 1.8 x = 0.9 [x
3
, x
4
], where x
4
is the midpoint.
cos(0.9) P
8
(0.9)

= 2.26 10
10
,
a factor of 24 smaller than the rst case.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.2.2 Behaviour of the error
Example
Le f(x) =
1
1+x
2
, x [5, 5], n > 0 an even integer, h =
10
n
x
j
= 5 + jh, j = 0, 1, 2, . . . , n
It can be shown that for many points x in [5, 5], the sequence of {P
n
(x)}
does not converge to f(x) as n .
Figure: The interpolation to
1
1+x
2
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3 Interpolation using spline functions
x 0 1 2 2.5 3 3.5 4
y 2.5 0.5 0.5 1.5 1.5 1.125 0
The simplest method of interpolating data in a table: connecting the node
points by straight lines: the curve y = (x) is not very smooth.
Figure: y = (x): piecewise linear interpolation
We want to construct a smooth curve that interpolates the given data point
that follows the shape of y = (x).
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3 Interpolation using spline functions
Next choice: use polynomial interpolation. With seven data point, we
consider the interpolating polynomial P
6
(x) of degree 6.
Figure: y = P
6
(x): polynomial interpolation
Smooth, but quite dierent from y = (x)!!!
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3 Interpolation using spline functions
A third choice: connect the data in the table using a succession of
quadratic interpolating polynomials: on each [0, 2], [2, 3], [3, 4].
Figure: y = q(x): piecewise quadratic interpolation
Is smoother than y = (x), follows it more closely than y = P
6
(x), but
at x = 2 and x = 3 the graph has corners, i.e., q

(x) is discontinuous.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.1 Spline interpolation
Cubic spline interpolation
Suppose n data points (x
i
, y
i
), i = 1, . . . , n are given and
a = x
1
< . . . < x
n
= b
We seek a function S(x) dened on [a, b] that interpolates the data
S(x
i
) = y
i
, i = 1, . . . , n
Find S(x) C
2
[a, b], a natural cubic spline such that
S(x)is a polynomial of degree 3on each subinterval [x
j1
, x
j
], j = 2, 3, . . . ,n;
S(x
i
) = y
i
, i = 1, . . . , n,
S

(a) = S

(b).
On [x
i1
, x
i
]: 4 degrees of freedom (cubic spline) 4(n 1) DOF.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.1 Spline interpolation
S(x
i
) = y
i
: n constraints
S

(x
i
0) = S

(x
i
+ 0) continuity 2 = 1, . . . , n 1
S

(x
i
0) = S

(x
i
+ 0) i = 2, . . . , n 1
S(x
i
0) = S(x
i
+ 0) i = 2, . . . , n 1
_
_
_
3n 6
n + (3n 6) = 4n 6 constraints
Two more constraints needed to be added, Boundary Conditions:
S

= 0 at a = x
1
, x
n
= b,
for a natural cubic spline.
(Linear system, with symmetric, positive denite, diagonally dominant
matrix.)
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.2 Construction of the cubic spline
Introduce the variables M
1
, . . . , M
n
with
M
i
S

(x
i
), i = 1, 2, . . . , n
Since S(x) is cubic on each [x
j1
, x
j
] S

(x) is linear, hence determined


by its values at two points:
S

(x
j1
) = M
j1
, S

(x
j
) = M
j
S

(x) =
(x
j
x)M
j1
+ (x x
j1
)M
j
x
j
x
j1
, x
j1
x x
j
(5.25)
Form the second antiderivative of S

(x) on [x
j1
, x
j
] and apply the
interpolating conditions
S(x
j1
) = y
j1
, S(x
j
) = y
j
we get
S(x) =
(x
j
x)
3
M
j1
+ (x x
j1
)
3
M
j
6(x
j
x
j1
)
+
(x
j
x)y
j1
+ (x x
j1
)y
j
6(x
j
x
j1
)

1
6
(x
j
x
j1
) [(x
j
x)M
j1
+ (x x
j1
)M
j
] (5.26)
for x [x
j1
, x
j
], j = 1, . . . , n.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.2 Construction of the cubic spline
Formula (5.26) implies that S(x) is continuous over all [a, b] and
satises the interpolating conditions S(x
i
) = y
i
.
Similarly, formula (5.25) for S

(x) implies that is continuous on [a, b].


To ensure the continuity of S

(x) over [a, b]: we require S

(x) on
[x
j1
, x
j
] and [x
j
, x
j+1
] have to give the same value at their common
pointx
j
, j = 2, 3, . . . , n 1, leading to the tridiagonal linear system
x
j
x
j1
6
M
j1
+
x
j+1
x
j1
3
M
j
+
x
j+1
x
j
6
M
j+1
(5.27)
=
y
j+1
y
j
x
j+1
x
j

y
j
y
j1
x
j
x
j1
, j = 2, 3, . . . , n 1
These n 2 equations together with the assumption
S

(a) = S

(b) = 0:
M
1
= M
n
= 0 (5.28)
leads to the values M
1
, . . . , M
n
, hence the function S(x).
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.2 Construction of the cubic spline
Example
Calculate the natural cubic spline interpolating the data
_
(1, 1), (2,
1
2
), (3,
1
3
), (4,
1
4
)
_
n = 4, and all x
j1
x
j
= 1. The system (5.27) becomes
1
6
M
1
+
2
3
M
2
+
1
6
M
3
=
1
3
1
6
M
2
+
2
3
M
3
+
1
6
M
4
=
1
12
and with (5.28) this yields
M
2
=
1
2
, M
3
= 0
which by (5.26) gives
S(x) =
_

_
1
12
x
3

1
4
x
2

1
3
x +
3
2
, 1 x 2

1
12
x
3
+
3
4
x
2

7
3
x +
17
6
, 2 x 3

1
12
x +
7
12
, 3 x 4
Are S

(x) and S

(x) continuous?
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.2 Construction of the cubic spline
Example
Calculate the natural cubic spline interpolating the data
x 0 1 2 2.5 3 3.5 4
y 2.5 0.5 0.5 1.5 1.5 1.125 0
n = 7, system (5.27) has 5 equations
Figure: Natural cubic spline interpolation y = S(x)
Compared to the graph of linear and quadratic interpolation y = (x) and
y = q(x), the cubic spline S(x) no longer contains corners.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.3 Other interpolation spline functions
So far we only interpolated data points, wanting a smooth curve.
When we seek a spline to interpolate a known function, we are interested also
in the accuracy.
Let f(x) be given on [a, b], that we want to interpolate on evenly
spaced values of x. For n > 1, let
h =
b a
n 1
, x
j
= a + (j 1)h, j = 1, 2, . . . , n
and S
n
(x) be the natural cubic spline interpolating f(x) at x
1
, . . . , x
n
.
It can be shown that
max
axb
|f(x) S
n
(x)| ch
2
(5.29)
where c depends on f

(a), f

(b) and max


axb
|f
(4)
(x)|. The reason
why S
n
(x) doesnt converge more rapidly (have an error bound with a
higher power of h) is that f

(a) = 0 = f

(b), while by denition


S

n
(a) = S

n
(b) = 0. For functions f(x) with f

(a) = f

(b) = 0, the
RHS of (5.29) can be replaced with ch
4
.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.3 Other interpolation spline functions
To improve on S
n
(x), we look for other interpolating functions S(x) that
interpolate f(x) on
a = x
1
< x
2
< . . . < x
n
= b
Recall the denition of natural cubic spline:
1
S(x) cubic on each subinterval [x
j1
, x
j
];
2
S(x), S

(x) and S

(x) are continuous on [a, b];


3
S

(x
1
) = S

(x
n
) = 0.
We say that S(x) is a cubic spline on [a, b] if
1
S(x) cubic on each subinterval [x
j1
, x
j
];
2
S(x), S

(x) and S

(x) are continuous on [a, b];


With the interpolating conditions
S(x
i
) = y
i
, i = 1, . . . , n
the representation formula (5.26) and the tridiagonal system (5.27) are
still valid.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.3 Other interpolation spline functions
This system has n 2 equations and n unknowns: M
1
, . . . , M
n
. By
replacing the end conditions (5.28) S

(x
1
) = S

(x
n
) = 0, we can
obtain other interpolating cubic splines.
If the data (x
i
, y
i
) is obtained by evaluating a function f(x)
y
i
= f(x
i
), i = 1, . . . , n
then we choose endpoints (boundary conditions) for S(x) that would
yield a better approximation to f(x). We require
S

(x
1
) = f

(x
1
), S

(x
n
) = f

(x
n
)
or
S

(x
1
) = f

(x
1
), S

(x
n
) = f

(x
n
)
When combined with(5.26-5.27), either of these conditions leads to a
unique interpolating spline S(x), dependent on which of these
conditions is used. In both cases, the RHS of (5.29) can be replaced
by ch
4
, where c depends on max
x[a,b]

f
(4)
(x)

.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.3 Other interpolation spline functions
If the derivatives of f(x) are not known, then extra interpolating
conditions can be used to ensure that the error bond of (5.29) is
proportional to h
4
. In particular, suppose that
x
1
< z
1
< x
2
, x
n1
< z
2
< x
n
and f(z
1
), f(z
2
) are known. Then use the formula for S(x) in (5.26)
and
s(z
1
) = f(z
1
), s(z
2
) = f(z
2
) (5.30)
This adds two new equations to the system (5.27), one for M
1
and
M
2
, and another equation for M
n1
, M
n
. This form is preferable to
the interpolating natural cubic spline, and is almost equally easy to
produce. This is the default form of spline interpolation that is
implemented in MATLAB. The form of spline formed in this way is
said to satisfy the not-a-knot interpolation boundary conditions.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.3 Other interpolation spline functions
Interpolating cubic spline functions are a popular way to represent data
analytically because
1
are relatively smooth: C
2
,
2
do not have the rapid oscillation that sometime occurs with the
high-degree polynomial interpolation,
3
are relatively easy to work with on a computer.
They do not replace polynomials, but are a very useful extension of
them.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.4 The MATLAB program spline
The standard package MATLAB contains the function
rm spline.
The standard calling sequence is
y = spline(x nodes, y nodes, x)
which produces the cubic spline function S(x) whose graph passes
through the points {(
i
,
i
) : i = 1, . . . , n} with
(
i
,
i
) = (x nodes(i), y nodes (i))
and n the length of x nodes (and y nodes). The not-a-knot
interpolation conditions of (5.30) are used. The point (
2
,
2
) is the
point (z
1
, f(z
1
)) of (5.30) and (
n1
,
n1
) is the point (z
2
, f(z
2
)).
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.4 The MATLAB program spline
Example
Approximate the function f(x) = e
x
on the interval [a, b] = [0, 1]. For
n > 0, dene h = 1/n and interpolating nodes
x
1
= 0, x
2
= h, x
3
= 2h, . . . , x
n+1
= nh = 1
Using spline, we produce the cubic interpolating spline interpolant
S
n,1
to f(x). With the not-a-knot interpolation conditions, the nodes
x
2
and x
n
are the points z
1
and z
2
in (5.30). For a general smooth
function f(x), it turns out that te magnitude of the error
f(x) S
n,1
(x) is largest around the endpoints of the interval of
approximation.
4. Interpolation Math 1070
> 4. Interpolation and Approximation > 4.3.4 The MATLAB program spline
Example
Two interpolating nodes are inserted, the midpoints of the subintervals [0, h]
and [1 h, 1]:
x
1
=0, x
2
=
1
2
h, x
3
=h, x
4
=2h, . . . , x
n+1
=(n1)h, x
n+2
=1
1
2
h, x
n+3
=1
Using spline results in a cubic spline function S
n,2
(x); with the not-a-knot
interpolation conditions conditions, the nodes x
2
and x
n+2
are the points z
1
and z
2
of (5.30). Generally, S
n,2
is a more accurate approximation than is
S
n,1
(x).
The cubic polynomials produced for S
n,2
(x) by spline for the intervals
[x
1
, x
2
] and [x
2
, x
3
] are the same, thus we can use the polynomial for [0,
1
2
h]
for the entire interval [0, h]. Similarly for [1 h, h].
n E
(1)
n
Ratio E
(2)
n
Ratio
5 1.01E-4 1.11E-5
10 6.92E-6 14.6 7.88E-7 14.1
20 4.56E-7 15.2 5.26E-8 15.0
40 2.92E-8 15.6 3.39E-9 15.5
Table: Cubic spline approximation to f(x) = e
x
4. Interpolation Math 1070