Skip to article frontmatterSkip to article content

Spline functions

from pylab import *

Consider a grid for the interval [a,b][a,b]

a=x0<x1<<xN=ba = x_0 < x_1 < \ldots < x_N = b

The derivative of a spline of order mm is a spline of order m1m-1, and similarly for anti- derivatives. Note that m=2m=2 corresponds to piecewise linear function which is continuous, and m=3m=3 is a piecewise quadratic function which is continuous and has continuous derivatives Kincaid & Cheney, 2002.

1Cubic spline interpolation

Consider the case of m=4m=4 where s(x)s(x) is a cubic polynomial in every sub-interval [xi1,xi][x_{i-1},x_i] and we are given the values of some function

yi=f(xi),i=0,1,,Ny_i = f(x_i), \qquad i=0,1,\ldots,N

Let us count the number of unknowns and number of equations available. We can write, for i=1,2,,Ni=1,2,\ldots,N

s(x)=ai+bix+cix2+dix3,x[xi1,xi]s(x) = a_i + b_i x + c_i x^2 + d_i x^3, \qquad x \in [x_{i-1},x_i]

Hence we have 4N4N unknown coefficients {ai,bi,ci,di}\{a_i,b_i,c_i,d_i\}. The spline function must satisfy some constraints; firstly, it must interpolate the given function values

s(xi)=yi,i=0,1,,Ns(x_i) = y_i, \qquad i=0,1,\ldots,N

and its derivatives must be continuous at the interior points

s(r)(xi)=s(r)(xi+),i=1,2,,N1,r=0,1,2s^{(r)}(x_i^-) = s^{(r)}(x_i^+), \qquad i=1,2,\ldots,N-1, \qquad r=0,1,2

This gives us (N+1)+3(N1)=4N2(N+1)+3(N-1) = 4N-2 equations, and we are short of two equations since we have 4N4N unknown coefficients. There are different ways in which the two missing equations can be supplied.

2Construction of cubic spline

Let

Mi=s(xi),i=0,1,,NM_i = s''(x_i), \qquad i=0,1,\ldots,N

Since s(x)s(x) is cubic on [xi,xi+1][x_i,x_{i+1}], then s(x)s''(x) is linear and thus

s(x)=(xi+1x)Mi+(xxi)Mi+1hi,i=0,1,,N1s''(x) = \frac{(x_{i+1}-x)M_i + (x-x_i)M_{i+1}}{h_i}, \qquad i=0,1,\ldots,N-1

where hi=xi+1xih_i = x_{i+1}-x_i. By the above construction s(x)s''(x) is continuous on [a,b][a,b]. Integrating twice, we get

s(x)=16hi[(xi+1x)3Mi+(xxi)3Mi+1]+Ci(xi+1x)+Di(xxi),x[xi,xi+1]\begin{align*} s(x) =& \frac{1}{6h_i}[ (x_{i+1}-x)^3 M_i + (x-x_i)^3 M_{i+1}] \\ & + C_i (x_{i+1}-x) + D_i (x-x_i), \quad x \in [x_i,x_{i+1}] \end{align*}

with Ci,Di,MiC_i,D_i,M_i to be determined. Using the interpolation conditions s(xi)=yis(x_i)=y_i and s(xi+1)=yi+1s(x_{i+1}) = y_{i+1} gives

Ci=yihihiMi6,Di=yi+1hihiMi+16,i=0,1,,N1C_i = \frac{y_i}{h_i} - \frac{h_iM_i}{6}, \qquad D_i = \frac{y_{i+1}}{h_i} - \frac{h_i M_{i+1}}{6}, \qquad i=0,1,\ldots,N-1

At this stage, the only unknowns are the MiM_i values. Note that s(x)s(x) and s(x)s''(x) are continuous on [a,b][a,b]. We will now enforce continuity of s(x)s'(x) at the interior nodes. On [xi,xi+1][x_i,x_{i+1}]

s(x)=12hi[(xi+1x)2Mi+(xxi)2Mi+1]+yi+1yihi(Mi+1Mi)hi6s'(x) = \frac{1}{2 h_i}[-(x_{i+1}-x)^2 M_i + (x-x_i)^2 M_{i+1}] + \frac{y_{i+1}-y_i} {h_i} - \frac{(M_{i+1}-M_i)h_i}{6}

and on [xi1,xi][x_{i-1},x_i]

s(x)=12hi1[(xix)2Mi1+(xxi1)2Mi]+yiyi1hi1(MiMi1)hi16s'(x) = \frac{1}{2 h_{i-1}}[-(x_{i}-x)^2 M_{i-1} + (x-x_{i-1})^2 M_{i}] + \frac{y_{i}- y_{i-1}}{h_{i-1}} - \frac{(M_{i}-M_{i-1})h_{i-1}}{6}

so that continuity of derivative at x=xix=x_i, s(xi)=s(xi+)s'(x_i^-) = s(x_i^+), yields

hi16Mi1+hi1+hi3Mi+hi6Mi+1=yi+1yihiyiyi1hi1,i=1,2,,N1\frac{h_{i-1}}{6}M_{i-1} + \frac{h_{i-1}+h_i}{3}M_i + \frac{h_i}{6}M_{i+1} = \frac{y_{i+1}-y_i}{h_i} - \frac{y_i - y_{i-1}}{h_{i-1}}, \quad i=1,2,\ldots,N-1

We have N1N-1 equations for the N+1N+1 unknowns M0,M1,,MNM_0, M_1, \ldots,M_N.

3End-point derivative conditions

To obtain the two extra equations, we impose

s(x0)=y0,s(xN)=yNs'(x_0) = y_0', \qquad s'(x_N) = y_N'

where y0=f(x0)y_0'=f'(x_0), yN=f(xN)y_N'=f'(x_N) are given to us. This yields the equations

h03M0+h06M1=y1y0h0y0hN16MN1+hN13MN=yNyNyN1hN1\begin{aligned} \frac{h_0}{3} M_0 + \frac{h_0}{6} M_1 &= \frac{y_1-y_0}{h_0} - y_0' \\ \frac{h_{N-1}}{6} M_{N-1} + \frac{h_{N-1}}{3}M_N &= y_N' - \frac{y_N - y_{N-1}} {h_{N-1}} \end{aligned}

We now have N+1N+1 equations for the N+1N+1 unknowns. They can be written in matrix form

AM=bAM = b

where

b=[y1y0h0y0{yi+1yihiyiyi1hi1}i=1N1yNyNyN1hN1],M=[M0M1MN]b = \begin{bmatrix} \frac{y_1-y_0}{h_0} - y_0' \\ \left\{ \frac{y_{i+1}-y_i}{h_i} - \frac{y_i - y_{i-1}}{h_{i-1}} \right\}_{i=1}^{N-1} \\ y_N' - \frac{y_N - y_{N-1}}{h_{N-1}} \end{bmatrix}, \qquad M = \begin{bmatrix} M_0 \\ M_1 \\ \vdots \\ M_N \end{bmatrix}
A=[h03h060diag{hi16,hi1+hi3hi6}i=1N10hN16hN13]A = \left[ \begin{aligned} & \frac{h_0}{3} \quad \frac{h_0}{6} \quad 0 \quad \cdots \\ & \textrm{diag}\left\{ \frac{h_{i-1}}{6}, \quad \frac{h_{i-1}+h_i}{3} \quad \frac{h_i} {6} \right\}_{i=1}^{N-1} \\ & \qquad\quad\quad \cdots \quad 0 \quad \frac{h_{N-1}}{6} \quad \frac{h_{N-1}}{3} \end{aligned} \right]

The matrix AA is symmetric, positive definite and diagonally dominant. Hence the problem is uniquely solvable. The matrix is tridiagonal and can be solved rapidly using about 8N8N arithmetic operations (Thomas Algorithm). The resulting cubic spline is called the complete cubic spline interpolant, and we denote it by sc(x)s_c(x).

4Optimality property

The cubic spline gives approximations which have less oscillations compared to other types of approximation. This is shown in the Python example. We first prove the optimality result.

5Not-a-knot condition

If the end derivative conditions f(a)f'(a), f(b)f'(b) are not known, then we need other conditions to complete the system of equations. We can demand that s(x)s'''(x) is continuous at x1x_1 and xN1x_{N-1}. This is equivalent to requiring that s(x)s(x) be a cubic spline function with knots

{x0,x2,x3,,xN2,xN}\{ x_0, x_2, x_3, \ldots, x_{N-2}, x_N\}

while still interpolating at all node points

{x0,x1,x2,,xN1,xN}\{x_0,x_1,x_2,\ldots,x_{N-1},x_N\}

This again leads to a tri-diagonal linear system of equations.

6Natural cubic spline

In the natural cubic spline approximation, the two end-point conditions

s(x0)=s(xN)=0s''(x_0) = s''(x_N) = 0

are used. This leads to a tridiagonal system of equations. The cubic spline approximations converge slowly near the end-points. The natural cubic spline also has an optimality property, for proof, see the problems collection.

References
  1. Kincaid, D., & Cheney, W. (2002). Numerical Analysis: Mathematics of Scientific Computing (3rd ed.). American Mathematics Society.