Skip to main content

Section 23.2 Cubic splines

A cubic spline is a function \(S\) that is piecewise a degree 3 polynomial, and such that \(S, S', S''\) are continuous (have the same value from both sides at the points where pieces meet). In our context of fitting to data points \((x_k, y_k)\text{,}\) \(k=1, \dots, n\text{,}\) it is natural to have each cubic polynomial defined on \([x_{k}, x_{k+1}]\text{.}\) The continuity of first and second derivatives means that the graph of a cubic spline has no corners of sudden changes of curvature, it flows quite naturally. Compare the plots below, for the same data set.

Figure 23.2.1. Piecewise linear interpolation
Figure 23.2.2. Interpolating polynomial
Figure 23.2.3. Cubic spline

Cubic splines were also used by the first version of SU COVID dashboard:

Figure 23.2.4. Dashboard with splines

Let us count unknowns and equations involved in the construction of a cubic spline.

  • We have \(n-1\) subintervals between \(n\) data points.
  • On each subinterval we choose a degree 3 polynomial; this means we have \(4(n-1)\) coefficients to choose.
  • In order for \(S, S', S''\) to be continuous at each of \(n-2\) interior data points, there are \(3(n-2)\) equations to satisfy.
  • The spline must pass through each of \(n\) data points. This adds \(n\) equations.
  • The total is \(3(n-2)+n=4n-6\) equations with \(4(n-1)=4n-4\) unknowns, so need two more equations to make the solution unique.

When discussing spline coefficients, it should be noted they are not necessarily the coefficients of powers of \(x\text{.}\) It is more natural and numerically safer to represent the spline on each subinterval \([x_k, x_{k+1}]\) in terms of the powers of \((x-x_k)\text{.}\) That is,

\begin{equation} S(x) = A_k(x-x_k)^3 + B_k(x-x_k)^2 + C_k(x-x_k) + D_k\label{eq-piece-spline}\tag{23.2.1} \end{equation}

where the coefficients \(a_k, b_k, c_k, d_k\) are to be determined. Of course, \(D_k=y_k\) but to find the rest one needs to solve a linear system.

Here are three different ways to add two more equations to the linear system for coefficients; they result in three different kinds of cubic splines:

  • Require \(S''(a) = 0 = S''(b)\text{.}\) This is a “natural spline”.
  • Require \(S'(a) = p\) and \(S'(b)=q\) for some values \(p, q\text{.}\) This is a “clamped spline”.
  • Require \(S'''\) to be continuous at \(x_2\) and at \(x_{n-1}\text{.}\) This is a “not-a-knot spline”.

This terminology needs some explanation. The word spline used to refer to a long thin strip of flexible material (e.g., metal) which, if constrained at several points, assumes the shape of a smooth curve passing through these points. Unlike an elastic band which minimizes its length (creating a piecewise linear shape), the spline minimizes its bending energy. A mathematical idealization of the bending energy of a curve \(y=f(x)\) is the integral \(\int_a^b f''(x)^2\,dx\text{.}\) It can be shown that among all functions (of whatever form) that satisfy the constraints \(f(x_k)=y_k\text{,}\) the “natural spline” has the smallest value of \(\int_a^b f''(x)^2\,dx\text{.}\) This attractive mathematical property earned it the “natural” name… but upon further reflection, the boundary conditions \(S''(a) = 0 = S''(b)\) are actually not that natural. If we are interpolating some smooth function \(y=f(x)\text{,}\) there is no reason to assume that \(f''\) happens to be \(0\) at the endpoints.

Clamped splines refer to holding the ends of the metal strip with a clamp, which restricts not only its position but also the angle. This spline is useful for for modeling a problem with boundary conditions on the derivative.

The knots of a spline are the transition points where it turns from one polynomial to another. These are often, but not always, the points \(x_k\) from the given data. Requiring \(S'''\) to be continuous at \(x_2\) implies we have the same polynomial on both intervals \([x_1, x_2]\) and \([x_2, x_3]\) (why?), and therefore \(x_2\) is not a knot for this spline. Same for \(x_{n-1}\text{.}\) The not-a-knot condition is the standard choice when we do not have any reason to impose other conditions.

Is the following function a spline? Why or why not?

\begin{equation*} f(x)=\begin{cases} 1+x-x^3, & 0\le x\le 1,\\ x,\quad & 1\le x\le 2\end{cases} \end{equation*}

Complete the formula

\begin{equation*} S(x)=\begin{cases} 1+x-x^3 & 0\le x\le 1,\\ \dots & 1\le x\le 2\end{cases} \end{equation*}

given that it describes a natural spline on \([0, 2]\text{.}\)

Answer

The continuity of \(S, S', S''\) implies that the difference of two polynomials used on both sides of \(x=1\) must be a constant multiple of \((x-1)^3\text{.}\) Therefore, the second polynomial must be of the form \(1+x-x^3+c(x-1)^3\text{.}\) Using the natural spline condition \(S''(2)=0\) we can find \(c=2\text{.}\) Thus,

\begin{equation*} S(x)=\begin{cases} 1+x-x^3,\quad & 0\le x\le 1,\\ 1+x-x^3 + 2(x-1)^3 \quad & 1\le x\le 2\end{cases} \end{equation*}