Skip to article frontmatterSkip to article content

Cubic splines

A piecewise linear interpolant is continuous but has discontinuities in its derivative. We often desire a smoother interpolant, i.e., one that has some continuous derivatives. By far the most popular choice is piecewise cubic.

We use S(x)S(x) to denote the cubic spline interpolant. As before, suppose that distinct nodes t0<t1<<tnt_0 < t_1 < \cdots < t_n (not necessarily equally spaced) and data y0,,yny_0,\ldots,y_n are given. For any k=1,,nk=1,\ldots,n, the spline S(x)S(x) on the interval [tk1,tk][t_{k-1},t_k] is by definition a cubic polynomial Sk(x)S_k(x), which we express as

Sk(x)=ak+bk(xtk1)+ck(xtk1)2+dk(xtk1)3,k=1,,n, S_k(x) = a_k + b_k(x-t_{k-1}) + c_k(x-t_{k-1})^2 + d_k(x-t_{k-1})^3, \qquad k=1,\ldots,n,

where ak,bk,ck,dka_k,b_k,c_k,d_k are values to be determined. Overall there are 4n4n such undetermined coefficients.

5.3.1Smoothness conditions

We are able to ensure that SS has at least two continuous derivatives everywhere by means of the following constraints.

1. Interpolation by SkS_k at both of its endpoints.

Algebraically we require Sk(tk1)=yk1S_k(t_{k-1})=y_{k-1} and Sk(tk)=ykS_k(t_k)=y_k for every k=1,,nk=1,\dots,n. In terms of (5.3.1), these conditions are

ak=yk1,a_k = y_{k-1},
ak+bkhk+ckhk2+dkhk3=yk,k=1,,n, a_k + b_k h_k + c_k h_k^2 + d_k h_k^3 = y_{k}, \qquad k=1,\ldots,n,

where we have used the definition

hk=tktk1,k=1,,n.h_k = t_{k}-t_{k-1}, \qquad k=1,\ldots,n.

The values of hkh_k are derived from the nodes. Crucially, the unknown coefficients appear only linearly in the constraint equations. So we will express the constraints using linear algebra. The left endpoint interpolation constraints (5.3.2) are, in matrix form,

[I000][abcd]=[y0yn1],\begin{bmatrix} \mathbf{I} & \boldsymbol{0} & \boldsymbol{0} & \boldsymbol{0} \end{bmatrix} \begin{bmatrix} \mathbf{a} \\ \mathbf{b} \\ \mathbf{c} \\ \mathbf{d} \end{bmatrix} = \begin{bmatrix} y_0 \\ \vdots \\ y_{n-1} \end{bmatrix},

with I\mathbf{I} being an n×nn\times n identity. The right endpoint interpolation constraints, given by (5.3.3), become

[IHH2H3][abcd]=[y1yn],\begin{bmatrix} \mathbf{I} & \mathbf{H} & \mathbf{H}^2 & \mathbf{H}^3 \end{bmatrix} \begin{bmatrix} \mathbf{a} \\ \mathbf{b} \\ \mathbf{c} \\ \mathbf{d} \end{bmatrix} = \begin{bmatrix} y_1 \\ \vdots \\ y_{n} \end{bmatrix},

where we have defined the diagonal matrix

H=[h1h2hn].\mathbf{H} = \begin{bmatrix} h_1 & & & \\ & h_2 & & \\ & & \ddots & \\ & & & h_n \end{bmatrix}.

Collectively, (5.3.5) and (5.3.6) express 2n2n scalar constraints on the unknowns.

2. Continuity of S(x)S'(x) at interior nodes.

We do not know what the slope of the interpolant should be at the nodes, but we do want the same slope whether a node is approached from the left or the right. Thus, we obtain constraints at the nodes that sit between two neighboring piecewise definitions, so that S1(t1)=S2(t1)S_1'(t_1)=S_2'(t_1), and so on. Altogether these are

bk+2ckhk+3dkhk2=bk+1,k=1,,n1.b_k + 2 c_k h_k + 3 d_k h_k^2 = b_{k+1}, \qquad k=1,\dots,n-1.

Moving the unknowns to the left side, as a system these become

E[0J2H3H2][abcd]=0,\mathbf{E} \begin{bmatrix} \boldsymbol{0} & \mathbf{J} & 2\mathbf{H} & 3\mathbf{H}^2 \end{bmatrix} \begin{bmatrix} \mathbf{a} \\ \mathbf{b} \\ \mathbf{c} \\ \mathbf{d} \end{bmatrix} = \boldsymbol{0},

where now we have defined

J=[1111111],\mathbf{J} = \begin{bmatrix} 1 & -1 & & & \\ & 1 & -1 & & \\ & & \ddots & \ddots & \\ & & &1 & -1 \\ & & & & 1 \end{bmatrix},

and E\mathbf{E} is the (n1)×n(n-1)\times n matrix resulting from deleting the last row of the identity:

E=[101010].\mathbf{E} = \begin{bmatrix} 1 & 0 & & & \\ & 1 & 0 & & \\ & & \ddots & \ddots & \\ & & & 1& 0 \end{bmatrix}.

Left-multiplying by E\mathbf{E} deletes the last row of any matrix or vector. Hence, (5.3.9) represents n1n-1 constraints on the unknowns. (Remember, there are only n1n-1 interior nodes.)

3. Continuity of S(x)S''(x) at interior nodes.

These again apply only at the interior nodes t1,,tn1t_1,\dots,t_{n-1}, in the form S1(t1)=S2(t1)S_1''(t_1)=S_2''(t_1) and so on. Using (5.3.1) once more, we obtain

2ck+6dkhk=2ck+1,k=1,,n1. 2 c_k + 6 d_k h_k = 2c_{k+1}, \qquad k=1,\dots,n-1.

In system form (after canceling a factor of 2 from each side) we get

E[00J3H][abcd]=0.\mathbf{E} \begin{bmatrix} \boldsymbol{0} & \boldsymbol{0} & \mathbf{J} & 3\mathbf{H} \end{bmatrix} \begin{bmatrix} \mathbf{a} \\ \mathbf{b} \\ \mathbf{c} \\ \mathbf{d} \end{bmatrix} = \boldsymbol{0}.

5.3.2End conditions

So far the equations (5.3.5), (5.3.6), (5.3.9), and (5.3.13) form 2n+(n1)+(n1)=4n22n+(n-1)+(n-1)=4n-2 linear conditions on the 4n4n unknowns in the piecewise definition (5.3.1). In order to obtain a square system, we must add two more constraints. If the application prescribes values for SS' or SS'' at the endpoints, those may be applied. Otherwise, there are two major alternatives:

While natural splines have important theoretical properties, not-a-knot splines give better pointwise accuracy, and they are the only type we consider further.

In the not-a-knot spline, the values and first three derivatives of the cubic polynomials S1S_1 and S2S_2 agree at the node t1t_1. Hence, they must be the same cubic polynomial! The same is true of Sn1S_{n-1} and SnS_n.[1] We could use these facts to eliminate some of the undetermined coefficients from our linear system of constraints. However, rather than rework the algebra we just append two more rows to the system, expressing the conditions

d1=d2,dn1=dn.d_1=d_2, \quad d_{n-1}=d_n.

Collectively, (5.3.5), (5.3.6), (5.3.9), (5.3.13), and (5.3.14) comprise a square linear system of size 4n4n which can be solved for the coefficients defining the piecewise cubics in (5.3.1). This is a major difference from the piecewise linear interpolant, for which there is no linear system to solve. Indeed, while it is possible to find a basis for the cubic spline interpolant analogous to the hat functions, it is not possible in closed form to construct a cardinal basis, so the solution of a linear system cannot be avoided.

5.3.3Implementation

Function 5.3.1 gives an implementation of cubic not-a-knot spline interpolation. For clarity, it stays very close to the description given above. There are some possible shortcuts—for example, one could avoid using E\mathbf{E} and instead directly delete the last row of any matrix it left-multiplies. Observe that the linear system is assembled and solved just once, and the returned evaluation function simply uses the resulting coefficients. This allows us to make multiple calls to evaluate SS without unnecessarily repeating the linear algebra.

5.3.4Conditioning and convergence

Besides having more smoothness than a piecewise linear interpolant, the not-a-knot cubic spline improves the order of accuracy to 4.

The conditioning of spline interpolation is much more complicated than for the piecewise linear case. First, the fact that the coefficients of all the cubics must be solved for simultaneously implies that each data value in y\mathbf{y} has an influence on SS over the entire interval. Second, SS can take on values larger in magnitude than all the values in y\mathbf{y} (see Exercise 5.3.5). The details may be found in more advanced texts.

5.3.5Exercises

Footnotes
  1. This explains the name of the not-a-knot spline—for splines, “knots” are the points at which different piecewise definitions meet.