Sie sind auf Seite 1von 8

.

Spline interpolation
Given a tabulated function f
k
= f (x
k
), k = 0, . . . N, a spline is a polynomial between each pair of tabulated
points, but one whose coemcients are determined slightly non-locally. Te non-locality is designed to
guarantee global smoothness in the interpolated function up to some order of derivative.
Cubic splines are the most popular. Tey produce an interpolated function that is continuous through to
the second derivative. Splines tend to be stabler than ftting a polynomial through the N +I points, with less
possibility of wild oscillations between the tabulated points.
We shall explain how spline interpolation works by frst going through the theory and then applying it to
interpolate the function below:
f
0
f
1
f
2
f
3
f
4
f
5
f
6
f
7
f
8
-4 -3 -2 -1 1 2 3 4
-0.5

0.5
Figure 3.I: Te function f (x) =
I
I+x

sin(x) between [4, 4]. Te values of f (x


k
) are given for i = 0, . . . , 8,
x
k
= 4, 3, 2, I, 0, I, 2, 3, 4.
Figure 3.I shows the function f (x) =
I
I+x

sin x in the region x [4, 4]. We are given the value of the
function at the points f
i
= f (x
k
), k = 0, . . . , 8, where x
k
= 4, 3, . . . , 0, . . . , 3, 4. Tese are what is called
the interpolating nodes.
.. Linear Spline
Lets focus attention on one particular interval (x
k
, x
k+I
). Linear interpolation in that interval gives the
interpolation formula
f = Af
k
+ Bf
k+I
(3.I)
where
A
x
k+I
x
x
k+I
x
k
, B I A =
x x
k
x
k+I
x
k
(3.2)
I
Tis is just like the piecewise Lagrange polynomial interpolation we looked at earlier. Figure 3.2 shows the
piecewise interpolated function over the full range [4, 4].
f
0
f
1
f
2
f
3
f
4
f
5
f
6
f
7
f
8
-4 -3 -2 -1 1 2 3 4
-0.5

0.5
Figure 3.2: Piecewise linear interpolation with 9 nodes (solid) for the function
I
I+x

sin x between [4, 4]


(dotted).
We can see that linear interpolation works quite well for larger values of x but does particularly badly in
(I, I) as it fails to capture the curvature of the function. Te accuracy can be improved by using more
interpolating nodes, but an important issue is that the frst derivatives of the interpolating function are dis-
continuous at the nodes.
.. Cubic spline interpolation
Te goal of cubic spline interpolation is to get an interpolation formula that is continuous in both the frst
and second derivatives, both within the intervals and at the interpolating nodes. Tis will give us a smoother
interpolating function. In general, if the function you want to approximate is smooth, then cubic splines will
do better than piecewise linear interpolation.
Before you read on, Id like you to clear your mind a little. Te following is a derivation of the
cubic interpolation formula from Chapter 3 of Numerical Recipes by Press et al., which is a slight
variation on the B
0
splines we discussed in lectures, but in my opinion illustrates the fundamental
idea of spline interpolation more clearly. If you understand this, then you will fnd working with
the B
0
splines much easier.
Lets restate the problem. We have a function f (x) that is tabulated at the N + I points f
k
= f (x
k
), k =
0, . . . , N. In each interval (x
k
, x
k+I
), we can ft a straight line through the points (x
k
, f
k
) and (x
k+I
, f
k+I
)
using the formula given by (3.I). Te problem is that with a linear function, the frst derivative is not contin-
2
uous at the boundary between two adjacent intervals, while we want the second derivative to be continuous,
even at the boundary!
Now suppose, contrary to fact, that in addition to the tabulated values of f
i
, we also have tabulated values
for the functions second derivatives, that is, a set of numbers f

i
. Ten within each interval (x
k
, x
k+I
), we
can add to the right-hand side of equation (3.I) a cubic polynomial whose second derivative varies linearly
from a value f

k
on the lef to a value f

k+I
on the right. Doing so, we will have the desired continuous second
derivative. If we also construct the cubic polynomial to have zero values at x
k
and x
k+I
, then adding it in will
not spoil the agreement with the tabulated functional values f
k
and f
k+I
at the endpoints x
k
and x
k+I
.
A little side calculation shows that there is only one way to arrange this construction, namely replacing equa-
tion (3.I) by
f = Af
k
+ Bf
k+I
+ Cf

k
+ Df

k+I
(3.3)
where Aand B are defned as before and
C
I
6
(A
3
A)(x
k+I
x
k
)
2
, D
I
6
(B
3
B)(x
k+I
x
k
)
2
(3.4)
Note that since Aand B are linearly dependent on x, C and D (through Aand B) have cubic x-dependence.
We can readily check that f

is in fact the second derivative of the new interpolating polynomial. We take


derivatives of equation (3.3) with respect to x, using the defnitions of A, B, C and D to compute dAdx,
dBdx, dCdx and dDdx.
Te result is
d f
dx
=
f
k+I
f
k
x
k+I
x
k

3A
2
I
6
(x
k+I
x
k
)f

k
+
3B
2
I
6
(x
k+I
x
k
)f

k+I
(3.3)
for the frst derivative, and
d
2
f
dx
2
= Af

k
+ Bf

k+I
(3.6)
for the second derivative. Since A = I at x
k
and A = 0 at x
k+I
, and B = 0 at x
k
and B = I at x
k+I
, (3.6) shows
that f

is just the tabulated second derivative, and also that the second derivative will be continuous across
the boundary between two intervals, say (x
kI
, x
k
) and (x
k
, x
k+I
).
Te only problem now is that we supposed the f

k
s to be known, when actually, they are not. However,
we have not yet required that the frst derivative, computed from (3.3), be continuous across the boundary
between two intervals. Te key idea of a cubic spline is to require this continuity and to use it to get equations
for the second derivatives f

k
.
Te required equations are obtained by setting equation (3.3) evaluated for x = x
k
in the interval (x
kI
, x
k
)
equal to the same equation evaluated for x = x
k
but in the interval (x
k
, x
k+I
). With some rearrangement,
this give (for k = I, . . . , N I)
x
k
x
kI
6
f

kI
+
x
k+I
x
kI
3
f

k
+
x
k+I
x
k
6
f

k+I
=
f
k+I
f
k
x
k+I
x
k

f
k
f
kI
x
k
x
kI
(3.7)
Tese are N I linear equations in the N +I unknowns f

i
, i = 0, . . . , N. Terefore there is a two-parameter
family of possible solutions.
For a unique solution, we need to specify further conditions, typically taken as boundary conditions at x
0
and x
N
. Te most common ways of doing this is to set both f

0
and f

N
equal to zero, giving the so-called
3
natural cubic spline, which has zero second derivatives on both of its boundaries.
Now we have the solution for f

k
, k = 0, . . . , N, we can substitute back into equation (3.3) to give the cubic
interpolation formula in each interval (x
k
, x
k+I
). Tis is probably the easiest approach to use if you want to
write a numerical code to calculate cubic spline interpolation.
So what are B-splines?
In the above approach, we started with a functional form for the interpolation formula ( f = Af
k
+ Bf
k+I
+
Cf

k
+ Df

k+I
), and had to use constraints ( f

continuous at interval boundaries) to solve for f

k
.
As mathematicians, we like to build things up from fundamental units. In this case, we can use a set of
piecewise cubic polynomials defned on some sub-interval of [x
0
, x
N
], which are by construction continuous
through to second derivative at the boundaries of intervals. Tey would form a set of basis functions, since
linear combinations of these functions would also satisfy the continuity properties at the boundaries between
adjacent intervals. To construct the cubic spline over the whole range [x
0
, x
N
], we would then simply need
match the sum of the basis functions with tabulated values of f
i
at the interpolating nodes x
i
, i = 0, . . . , N.
Te B-splines are the basis functions that satisfy our continuity conditions. If the interpolation is over the
region [x
0
, x
N
], then B
0
is defned by
B
0
(x) =

0 x x
0
2h
I
6
(2h + (x x
0
))
3
x
0
2h x x
0
h
2h

3

I
2
(x x
0
)
2
(2h + (x x
0
)) x
0
h x x
0
2h

3

I
2
(x x
0
)
2
(2h (x x
0
)) x
0
x x
0
+ h
I
6
(2h (x x
0
))
3
x
0
+ h x x
0
+ 2h
0 x x
0
+ 2h
(3.8)
where h = x
k+I
x
k
=
x
N
x

N
is the width between interpolating nodes (assumed to be equal). Note that B
0
has non-zero values over four intervals. We can easily check that B
0
satisfy the continuity conditions at the
boundaries of the intervals, namely, B
0
, B

0
and B

0
are continuous at 2h, h, 0, h and 2h.
B
k
(x) B
k+1
(x) B
k+2
(x) B
k-1
(x)
x
k-2
x
k-1
x
k
x
k+1
x
k+2
Figure 3.3: Cubic B-spline
4
We defne B
k
(x) = B
0
(x kh + x
0
) (i.e. the B
0
functions shifed to the right by k nodes). Tis is illustrated
in Figure 3.3, which shows B
kI
, B
k
, B
k+I
and B
k+2
. Note that all four of these basis functions are non-zero
in the interval (x
k
, x
k+I
).
Te cubic spline function, S
3
(x) in [x
0
, x
N
] is then written as the linear combination of the B
k
s:
S
3
(x) =
N+I

k=I
a
k
B
k
(x) (3.9)
Te sum is fromI to N +I, since B
I
is nonzero in the interval (x
0
, x
I
), and B
N+I
is nonzero in the interval
(x
NI
, x
N
), so we need to take into account these functions.
Is the problem now completely specifed: We know that we need 4 conditions (coemcients) to uniquely
specify a cubic, and in [x
0
, x
N
] there are N intervals, so altogether a total of 4N conditions are needed.
Te continuity conditions are automatically satisfed in the N I interior points, since the B
k
s satisfy the
continuity conditions (thats 3(NI) conditions). Te other requirement is that S
3
must match the tabulated
points, i.e. S
3
(x
k
) = f (x
k
), for k = 0, . . . , N (N +I conditions). So there are two unspecifed conditions, and
like before we can take S

3
(x
0
) = S

3
(x
N
) = 0 (i.e., curvature equals zero at the boundaries, natural spline).
Now evaluating S
3
(x) at x
k
:
S
3
(x
k
) = a
kI
B
kI
(x
k
) + a
k
B
k
(x
k
) + a
k+I
B
k+I
(x
k
) + a
k+2
B
k+2
(x
k
) = f (x
k
) (3.I0)
(all other B
k
s are zero)
From the defnitions of B
k
(x) and B
0
(x), we fnd
B
k
(x
k
) = B
0
(x
0
) =
2h
3
3
B
kI
(x
k
) = B
0
(x
0
+ h) =
h
3
6
B
k+I
(x
k
) = B
0
(x
0
h) =
h
3
6
B
k+2
(x
k
) = B
0
(x
0
2h) = 0
Substituting into (3.I0), we get the recurrence relation for the coemcients a
k
a
kI
+ 4a
k
+ a
k+I
=
6
h
3
f (x
k
) (3.II)
for k = 0, . . . , N.
What happens at the boundaries: Remember we set S

3
(x
0
) = S

3
(x
N
) = 0. Diferentiating S
3
(x) gives
S

3
(x) =
N+I

k=I
a
k
B

k
(x) (3.I2)
So we need to fnd the second derivatives of B
k
.
3
Diferentiating B
0
twice, we get
B

0
(x) =

0 x x
0
2h
2h + (x x
0
) x
0
2h x x
0
h
2h 3(x x
0
) x
0
h x x
0
2h + 3(x x
0
) x
0
x x
0
+ h
2h (x x
0
) x
0
+ h x x
0
+ 2h
0
(3.I3)
So as before, we have
0 = S

3
(x
0
) = a
I
B

I
(x
0
) + a
0
B

0
(x
0
) + a
I
B

I
(x
0
) + a
2
B

2
(x
0
)
= a
I
B

0
(x
0
+ h) + a
0
B

0
(x
0
) + a
I
B

(x
0
h)
= a
I
h 2ha
0
+ a
I
h
= a
I
2a
0
+ a
I
(3.I4)
From (3.I0), we have
a
I
+ 4a
0
+ a
I
=
6
h
3
f (x
0
) (3.I3)
Subtract (3.I4) from (3.I3), we obtain
a
0
=
I
h
3
f (x
0
) (3.I6)
Similarly, we fnd
a
N
=
I
h
3
f (x
N
) (3.I7)
We now can write a matrix equation to solve for the coemcients a
0
, . . . , a
N
:

I 0 0
I 4 I 0 0 0
0 I 4 I 0 0

0 0 I 4 I
0 0 0 I

a
0
a
I
a
2

a
NI
a
N

=
I
h
3

f (x
0
)
6f (x
I
)
6f (x
2
)

6f (x
NI
)
f (x
N
)

(3.I8)
Tis set of equations is tridiagonal and can be solved in O(N) operations by the tridiagonal algorithm. Te
fnal two coemcients to completely determine S
3
are
a
I
= 2a
0
a
I
a
N+I
= 2a
N
a
NI
(3.I9)
6
Worked Example
Now we can try to apply cubic spline interpolation to the function f (x) =
I
I+x

sin x, 4 x 4, with the 9


nodes x
k
= 4, 3, 2, I, 0, I, 2, 3, 4.
Here x
0
= 4, x
N
= 4 and h = I. So from equation (3.8), the basis function B
0
(x) is given by
B
0
(x) =

0 x 6
I
6
(x + 6)
3
6 x 3
2
3

I
2
(x + 4)
2
(x + 6) 3 x 4
2
3
+
I
2
(x + 4)
2
(x + 2) 4 x 3

I
6
(x + 2)
3
3 x 2
0 x 2
(3.20)
Te cubic spline S
3
(x) =
9
k=I
a
k
B
k
(x), and we can fnd the coemcients a
k
, k = 0, I, . . . , 8 by solving the
linear system

I 0 0 0 0 0 0 0 0
I 4 I 0 0 0 0 0 0
0 I 4 I 0 0 0 0 0
0 0 I 4 I 0 0 0 0
0 0 0 I 4 I 0 0 0
0 0 0 0 I 4 I 0 0
0 0 0 0 0 I 4 I 0
0 0 0 0 0 0 I 4 I
0 0 0 0 0 0 0 0 I

a
0
a
I
a
2
a
3
a
4
a
3
a
6
a
7
a
8

f (4)
6f (3)
6f (2)
6f (I)
6f (0)
6f (I)
6f (2)
6f (3)
f (4)

(3.2I)
Tis can be readily solved to give a
0
, . . . , a
8
. Additionally, a
I
= 2a
0
a
I
, a
9
= 2a
8
a
7
. Te numerical values
are
a
0
= 0.034333682I a
I
= 0.0I0I80972 a
2
= 0.I2339389
a
3
= 0.39973424 a
4
= 4.9493829 I0
I7
a
3
= 0.39973424
a
6
= 0.I2339389 a
7
= 0.0I0I80972 a
8
= 0.03433682I
(3.22)
a
I
= 0.038492670, a
9
= 0.038492670.
We shall just show the construction of S
3
(x) in the interval [4, 3]:
S
3
(x) = a
I
B
I
(x) + a
0
B
0
(x) + a
I
B
I
(x) + a
2
B
2
(x)
= a
I
B
0
(x + I) + a
0
B
0
(x) + a
I
B
0
(x I) + a
2
B
0
(x 2)
= a
I

I
6
((x + I) + 2)
3
+ a
0
(
2
3
+
I
2
(x + 4)
2
(x + 2))
+a
I
(
2
3

I
2
((x I) + 4)
2
((x I) + 6)) + a
2
(
I
6
((x 2) + 6)
3
)
So need to work out the functional form of S
3
(x) for each interval. Te whole process is somewhat tedious,
but as shown in the next fgure, it does give the right result!
7
-4 -3 -2 -1 1 2 3 4
-0.5

0.5
Figure 3.4: Heres the function f (x) =
I
I+x

sin x again (dotted), and now with the cubic spline interpolation
for [4, 3] superimposed on top.
8