A Clamped Cubic Spline Function Coefficient Command and examples of its use

© Copyright Joseph M. Herrmann 1994

This code was adapted from the Clamped Cubic Spline Algorithm in Richard L. Burden, J. Douglas Faires, and Albert  C. Reynolds, Numerical Analysis, Prindle, Weber, and Schmidt, Boston, 1978, pp. 122-123.   

This command currently has no error checking capability and assumes that the list of data points and other input arguments are correctly inputed.

Clamped cubic splines are used to create a function that interpolates a set of data points and has specified derivatives at the x coordinate of the first and last data point.  This function consists of piecewise cubic polynomials on each interval between the x coordinates of the data points and is constructed to be continuous with continuous first and second derivatives.

The Clamped Cubic Spline Function Coefficient Command

If S is a list of the data points with x coordinates ordered from minimum to maximum, db is the specified derivative value at x coordinate of the first data point, and de is the specified derivative value at x coordinate of the last data point, then the function CSplineCoef[S,db,de,a,b,c,d,n,t] computes the coefficients of the clamped cubic spline functions in the vectors a, b, c, and d.  The number of data points is returned in the constant n and the x coordinates of the data is returned in the vector t. 

These coefficients can be used to build the clamped cubic spline functions:
Sp(x)=a1+b1(x-x1)+c1(x-x1)2+d1(x-x1)3   for     x1≤x≤x2
          =a2+b2(x-x2)+c2(x-x2)2+d2(x-x2)3   for     x2≤x≤x3
         . . .
         =an-1+bn-1(x-xn-1)+cn-1(x-xn-1)2+dn-1(x-xn-1)3   for     xn-1≤x≤xn

An example shows how to use this function to calculate the coefficients and then build the clamped cubic spline functions automatically as Sp[1,x], ..., Sp[n-1,x] and then plot these functions with the data.  Execute the bold face Mathematica commands as you read the notebook.

CSplineCoef[S_,db_,de_,a_,b_,c_,d_,n_,t_]:=Module[{f,alpha,
		mu,z,L,h},
	n=Length[S];
	f=Table[S[[i,2]],{i,1,n}];
	t=Table[S[[i,1]],{i,1,n}]; 
	a=Table[0,{i,1,n}];
	b=Table[0,{i,1,n}];
	c=Table[0,{i,1,n}];
	d=Table[0,{i,1,n}];
	alpha=Table[0,{i,1,n}];
	mu=Table[0,{i,1,n}];
	z=Table[0,{i,1,n}];
	L=Table[0,{i,1,n}];
	h=Table[0,{i,1,n}];
	h[[1]]=t[[2]]-t[[1]];
	alpha[[1]]=3(f[[2]]-f[[1]])/h[[1]]-3db;
	L[[1]]=2h[[1]];
	mu[[1]]=1/2;
	z[[1]]=alpha[[1]]/L[[1]];
	b[[1]]=db;
	For[i=2,i<n,i++,
		h[[i]]=t[[i+1]]-t[[i]];
		alpha[[i]]=(3/(h[[i]]*h[[i-1]]))*(f[[i+1]]*h[[i-1]]-
			f[[i]]*(h[[i]]+h[[i-1]])+f[[i-1]]*h[[i]]);
		L[[i]]=2*(h[[i]]+h[[i-1]])-h[[i-1]]*mu[[i-1]];
		mu[[i]]=h[[i]]/L[[i]];
		z[[i]]=(alpha[[i]]-h[[i-1]]*z[[i-1]])/L[[i]]
		];
	alpha[[n]]=3de-3(f[[n]]-f[[n-1]])/h[[n-1]];
	L[[n]]=h[[n-1]]*(2-mu[[n-1]]);
	z[[n]]=(alpha[[n]]-h[[n-1]]z[[n-1]])/L[[n]];
	c[[n]]=z[[n]];
	For[j=n-1,j>0,j--,
		c[[j]]=z[[j]]-mu[[j]]*c[[j+1]];
		b[[j]]=(f[[j+1]]-f[[j]])/h[[j]]-h[[j]]*
			(c[[j+1]]+2*c[[j]])/3;
		d[[j]]=(c[[j+1]]-c[[j]])/(3*h[[j]]);
		a[[j]]=f[[j]]
		];
	Print["finished"]]

Let's consider the following data points.

B={{46,40},{49,50},{51,55},{52,63},{54,72},{56,70},{57,77},
{58,73},{59,90},{60,93},{61,96},{62,88},{63,99},{64,110},
{66,113},{67,120},{68,127},{71,137},{72,132}}

We shall now determine the coefficients of the clamped cubic spline functions which have a derivative equal to 0 at x=46 and a derivative of -100 at x=72.

CSplineCoef[B,0,-100,a,b,c,d,n,t]
a
b
c
d

The following creates the spline functions automatically as Sp[1,x], ..., Sp[n-1,x]

Clear[Sp]
For[i=1,i<n,i++,
		Sp[i,x_]=a[[i]]+b[[i]]*(x-t[[i]])+
			c[[i]]*(x-t[[i]])^2+d[[i]]*(x-t[[i]])^3]
?Sp

What is the function value when x=47?  Since 47 is between 46 and 49, we use the clamped cubic spline function for the first interval and find:

N[Sp[1,47]]

What is the function value when x=70?  Since 70 is between 68 and 71, we use the 17th spline function and find:

N[Sp[17,70]]

Let's check that we have the correct derivative values at the endpoints.  The following creates the derivative functions at each end point and then we shall evaluate them at 46 and 72 and check that we get 0 and -100 as we specified in the CSplineCoef function.

f[x_]=D[Sp[1,x],x]
g[x_]=D[Sp[18,x],x]
f[46]
g[72]

We shall now plot each spline function and then plot them together with the data.  Note how the large negative derivative at 72 contorts the curve between 68 to 72.

For[i=1,i<n,i++,
	P[i]=Plot[Sp[i,x],{x,t[[i]],t[[i+1]]}]]
B1=ListPlot[B,Prolog->PointSize[.015]]
SP=Show[Table[P[i],{i,1,n-1}]]
Show[B1,SP]

The use of the clamped cubic spline function to calculate the derivative or integral of a function which interpolates the data points

The clamped cubic spline function can be used to estimate the derivative or integral of a function approximated by the set of data points.  This can be accomplished by differentiating or integrating the clamped cubic spline function formed from the data points.  The procedure to find such a derivative or integral is described in the natural cubic spline function coefficient notebook.