⎟=⎜ ⎟,

. .

. . .

⎜ ⎟⎜ . .

⎟⎜ ⎟

⎜ ⎟⎝ ⎠⎝ ⎠

..

⎝ ⎠ gn’2 bn’2

. dn’2 sn’2

bn’1 ’ gn /hn’1

gn’1

sn’2 dn’1

where we use the general notation

si = h’1 , i = 1, . . . , n ’ 1, (19.13)

i

and

di = 2(si’1 + si ), i = 1, . . . , n ’ 1 (19.14)

bi = 3s2 (fi ’ fi’1 ) + 3s2 (fi+1 ’ fi ), i = 1, . . . , n ’ 1. (19.15)

i’1 i

In case the second derivative is given at an end node, say at x0 : f0 = 2p0 ,

the extra condition reads

3

(f1 ’ f0 ) ’ 2g0 ’ g1 .

h0 p0 = (19.16)

h0

Now g0 cannot be eliminated and an extra row and column must be added

to the matrix:

⎛ ⎞⎛ ⎞ ⎛ ⎞

d0 s0 g0 b0

⎜ s0 d1 s1 ⎟⎜ ⎟⎜ ⎟

⎜ ⎟⎜ g1 b1

⎟⎜ ⎟

⎜ ⎟⎜ ⎟⎜ ⎟

s1 d2 s2

⎜ ⎟⎜ g2 b2

⎟⎜ ⎟

⎜ ⎟⎜ ⎟=⎜ ⎟,

.. .. ..

⎜ ⎟⎜ . .

⎟⎜ ⎟

. .

. . .

⎜ ⎟⎜ . .

⎟⎜ ⎟

⎜ ⎟⎝ ⎠⎝ ⎠

..

⎝ ⎠ gn’2 bn’2

. dn’2 sn’2

bn’1 ’ gn sn’1

gn’1

sn’2 dn’1

(19.17)

with

d0 = 2s0 , (19.18)

1

b0 = 3(f1 ’ f0 )s2 ’ f0 . (19.19)

0

2

Again, a symmetric tridiagonal matrix is obtained. Similarly, a given second

derivative at the end point xn can be handled., yielding an extra row at the

bottom and column at the right with

dn = 2sn’1 , (19.20)

1

bn = 3(fn ’ fn’1 )s2 ’ fn . (19.21)

n’1

2

19.2 Cubic splines through points 529

If the third derivative f0 is speci¬ed (at x0 ), the same matrix is obtained

as in the previous case, but with values

d0 = s0 , (19.22)

1

b0 = 2(f1 ’ f0 )s2 + f0 h0 . (19.23)

0

6

For the third derivative fn speci¬ed at xn , the extra elements are

dn = sn’1 , (19.24)

1

bn = 2(fn ’ fn’1 )s2 + fn hn’1 . (19.25)

n’1

6

For periodic splines, f (xn +ξ) = f (x0 +ξ), and the function and its ¬rst two

derivatives are continuous at xn . Thus there are n unknowns g0 , . . . , gn’1 ,

and the matrix equation now involves a symmetric tridiagonal cyclic matrix:

Ag = b, (19.26)

⎛ ⎞⎛ ⎞ ⎛ ⎞

d0 s0 sn’1 g0 b0

⎜ s0 ⎟⎜ ⎟⎜ ⎟

d1 s1

⎜ ⎟⎜ g1 b1

⎟⎜ ⎟

⎜ ⎟⎜ . ⎟⎜ ⎟

.. .. ..

⎜ ⎟⎜ . .

⎟=⎜ ⎟,

.

. . .

⎜ ⎟⎜ . .

⎟⎜ ⎟

⎜ ⎟⎝ ⎠ ⎝ bn’2 ⎠

..

⎝ ⎠ gn’2

. dn’2 sn’2

gn’1 bn’1

sn’1 sn’2 dn’1

where the matrix elements are given by (19.14), (19.13) and (19.15), with

additional elements

d0 = 2(s0 + 2sn’1 ), (19.27)

b0 = 3(f1 ’ f0 )s2 + 3(f0 ’ fn’1 )s2 , (19.28)

0 n’1

bn’1 = as in (19.15) with fn = f0 . (19.29)

As the matrices are well-behaved (diagonally dominant, positive de¬nite),

the equations can be simply solved; algorithms and Python programs are

given in Section 19.6.

Cubic splines have some interesting properties, related to the curvature,

as de¬ned in (19.1). We refer to Kreyszig (1993) for the proofs.

(i) Of all functions (continuous and with continuous ¬rst and second

derivatives) that pass through n given points, and have given ¬rst

derivatives at the end points, the cubic spline function has the small-

est curvature. The cubic spline solution is unique; all other functions

530 Splines for everything

with these properties have a larger curvature. One may say that the

cubic spline is the smoothest curve through the points.4

(ii) Of all functions (continuous and with continuous ¬rst and second

derivatives) that pass through n given points, the function with small-

est curvature is a natural cubic spline, i.e., with zero second deriva-

tives at both ends.

So, if for some good reason, you look for the function with smallest curvature

through a number of given points, splines are the functions of choice.

Figure 19.2 shows the cubic (periodic) spline solution using as x, y data

just the following points, which sample a sine function:

π 3π

yi = 0, 1, 0, ’1, 0.

xi = 0, , π, , 2π,

2 2