You are on page 1of 2

Algorithms for Cubic Spline Interpolation

Algorithm for finding zi , i = 0 . . . n


% Compute the hi and bi
for i = 0 : n − 1
hi = xi+1 − xi
bi = (yi+1 − yi )/hi
end
% Gaussian Elimination
u1 = 2(h0 + h1 )
v1 = 6(b1 − b0 )
for i = 2 : n − 1
ui = 2(hi−1 + hi ) − h2i−1 /ui−1
vi = 6(bi − bi−1 ) − hi−1 vi−1 /ui−1
end
% Back-substitution
zn = 0
for i = n − 1 : −1 : 1
zi = (vi − hi zi+1 )/ui
end
z0 = 0

How many flops are required to compute the zi ?

Evaluating S(x)

Remember that once you have the zi , you can evaluate S(x) as follows:

zi zi+1
Si (x) = (xi+1 − x)3 + (x − xi )3 + Ci (x − xi ) + Di (xi+1 − x)
6hi 6hi

yi+1 hi yi hi
with Ci = hi − 6 zi+1 and Di = hi − 6 zi .

This, however, is not the most efficient computational form. We would like to use the idea of nested
multiplication, so write:

Si (x) = ai + bi (x − xi ) + ci (x − xi )2 + di (x − xi )3

P

1 (n) (n)
Notice that this is just the infinite Taylor expansion Si (x) = n! (x − xi )n Si (xi ) (with Si = 0 for
n=1
n ≥ 4 since Si is a cubic polynomial).
Therefore,

ai = Si (xi ) = yi
hi hi yi+1 − yi
bi = Si0 (xi ) = − zi+1 − zi +
6 3 hi
1 00 zi
ci = S (xi ) =
2 i 2
1 000 zi+1 − zi
di = S (xi ) =
6 i 6hi
Algorithm for Evaluating S(x)
for i = 0 : n − 1
if x ≤ xi+1
break;
end
end
h = xi+1 − xi
Compute a, b, c and d as above.
S = a + (x − xi ) (b + (x − xi ) (c + (x − xi )d))

How many flops are required to for each spline function evaluation?

You might also like