You are on page 1of 2

Algorithms for Cubic Spline Interpolation

Algorithm for nding 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(hi1 + hi ) h2 /ui1 i1 vi = 6(bi bi1 ) hi1 vi1 /ui1 end % Back-substitution zn = 0 for i = n 1 : 1 : 1 zi = (vi hi zi+1 )/ui end z0 = 0 How many ops are required to compute the zi ? Evaluating S(x) Remember that once you have the zi , you can evaluate S(x) as follows: Si (x) = with Ci =
yi+1 hi hi 6 zi+1

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


yi hi

and Di =

hi 6 zi .

This, however, is not the most ecient 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

Notice that this is just the innite Taylor expansion Si (x) =


n=1

1 n! (x

xi )n Si (xi ) (with Si

(n)

(n)

= 0 for

n 4 since Si is a cubic polynomial). Therefore, ai bi ci di = Si (xi ) = yi = Si (xi ) = = = hi yi+1 yi hi zi+1 zi + 6 3 hi 1 zi S (xi ) = 2 i 2 zi+1 zi 1 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 ops are required to for each spline function evaluation?

You might also like