Two routines are provided for the calculation of the complete cubic interpolatory spline S. One, SPCOEF in FORTRAN, Spline_coeff in C, sets up the tridiagonal system (3.42)–(3.44) for {ci}, 1 solves it, and computes {di} and {bi} from (3.41) and (3.40). This routine should be called only once for a given set of data. The coefficients output from this routine are then used in the evaluation routine, SVALUE in FORTRAN, Spline-value in C. It is called once for each point t where S(t) is to be evaluated. A routine to compute the coefficients defining the shape-preserving interpolant is quite useful. It can be written easily by modifying SPCOEF or Spline_coeff so as to use the formulas of Section 3.5.2. Proceeding in this way, SVALUE or Spline_value can be used for the evaluation of both kinds of spline.
Instead of using the slopes f´(x1) and f´(xN) needed for the end conditions of the
complete cubic spline, the routines provided interpolate the four data points nearest each end with cubits, and the slopes of these approximations are used in (3.43) and (3.44). As h 0, the resulting spline converges to the complete cubic spline. In prac- tice this approximation works well enough if N is not too small. The approximation is not plausible for Example 3.8 because there are only eight data points, all of which are used to approximate the derivatives at the end as well as the function through- out the interval. When the data are this sparse, the shape-preserving spline is more appropriate.
A typical call in FORTRAN is
CALL SPCOEF (N, X, F, B, C, D, FLAG) and
flag = Spline_coeff(n, x, f, b, c, d);
in the C and C++ versions. The input vectors X and F hold the data points (xi,fi)
D contain the coefficients of the cubits. In normal circumstances the output variable FLAG is set to zero. However, if the input N < 2, then no calculations are performed and FLAG := - 1. If the entries of X are not correctly ordered (so that some hj < 0), then FLAG := -2.
To evaluate the spline the FORTRAN version SVALUE first finds an index i such that xi < t < xi+1 and then the ith cubic is evaluated to get S(t). A typical call in FOR- TRAN is
CALL SVALUE (N, X, F, B, C, D, T, INTERV, S, FLAG) and
flag = Spline_value(n, x, f, b, c, d, t, interval, s);
in the C++ version. The last two parameters are output, so their addresses must explic- itly be passed in C:
flag = Spline-value(n, x, f, b, c, d, t, &interval, &s);
As usual, arrays in the C and C++ versions are indexed starting at 0 rather than 1 as is typical of FORTRAN. The parameters N, X, F, B, C, and D have the same meaning for SPCOEF and Spline_coeff. The last three are input quantities that must have been set by a prior call to SPCOEF or Spline_coeff. The variable T holds the point where the evaluation is to be made and the answer comes back in S. If the index i satisfying xi < T < xi+1 is known, this can be input using the variable INTERV or interval, as
the case may be. However, it is not necessary to do this since the code will calculate the correct value and assign it to INTERV or interval. The normal value of FLAG (the return value in the C version) is zero. When N < 2, FLAG is returned with the value - 1. If T < x1, then FLAG is set to 1, and the cubic for [x1,x2] is used for S. If T > xN, then FLAG is set to 2, and the cubic for [xN-1, xN] is used for S.
Example 3.1O. A sample driver is provided to interpolate sinx over { 0, 0.2, 0.4, 0.6, 0.8 } ; the resulting S(x) is then tabulated at { 0.1, 0.3, 0.5, 0.7, 0.9 } to yield the following.
Note that only one call is made to SPCOEF or Spline_coeff even though the spline is
evaluated at five points. Why is FLAG = 2 in the last line? n
Example 3.11. A graph of the spline S(v) interpolating the 16 indicated data points from Example 3.6 appears in Figure 3.11. It is a dramatically better approximation than the polynomial of high degree appearing in Figure 3.2. Values of S at some of the reserved data points are S(0.29) = -2.88, S(1.11) = -1.57, S(5) = 5.50, S(8.5) =
1 1 5 3.5 SPLINE INTERPOLATION
Figure 3.11 S(v) from Example 3.11.
3.5 SPLINE INTERPOLATION 111
Back substitution is also easy:
CN := YN/~N
f o r k = N - l , N - 2 , . . . , 1 b e g i n c k : = ( Y k - P k * C k + l ) / a k end
k.
The whole computation costs only 3N - 3 multiplications and 2N - 1 divisions. Once the c vector is known, vector
d
can be computed from (3.41), and vectorb
from (3.40). The storage required is a small multiple of N rather than the N2 needed for a general system of equations.We finish this section by discussing some of the mathematical properties of the complete cubic interpolatory spline S(x). The physical spline used by drafters can be modeled using the theory of thin beams. In general, the curvature K(X) of a function
f(x) is
If’W I
K
(
x) =
(1+ (fyx))2)
3’
2and in this theory the expression is linearized to K(X) E
If’(x)I.
When (S’(X))~ < 1, the quantity J’F (S”)2d
x can be regarded as a measure of the curvature of the spline S(x). We prove now that in this measure, any smooth interpolating function satisfying the type (1) end conditions must have a curvature at least as large as that of S(x). This is sometimes referred to as theminimum curvature
property of the complete cubic spline. The same result is true for the natural cubic spline when the requirement that the interpolant satisfy the type (1) end conditions is dropped.Theorem 3.5.
If g is any C
2[x1
,XN]function that interpolates f over (xl, . . .,
xN ) and satisjes the type (1) end conditions, then
J” (sy
2dx 2 J” (g”)2dx,
Xl Xl