28-2 Splines

A pratical method for interpolating a set of points with a curve is to use cubic splines. We are given a set {(xi,yi):i=0,1,,n}\{(x_i, y_i): i = 0, 1, \ldots, n\} of n+1n + 1 point-value pairs, where x0<x1<<xnx_0 < x_1 < \cdots < x_n. We wish to fit a piecewise-cubic curve (spline) f(x)f(x) to the points. That is, the curve f(x)f(x) is made up of nn cubic polynomials fi(x)=ai+bix+cix2+dix3f_i(x) = a_i + b_ix + c_ix^2 + d_ix^3 for i=0,1,,n1i = 0, 1, \ldots, n - 1, where if xx falls in the range xixxi+1x_i \le x \le x_{i + 1}, then the value of the curve is given by f(x)=fi(xxi)f(x) = f_i(x - x_i). The points xix_i at which the cubic polynomials are "pasted" together are called knots. For simplicity, we shall assume that xi=ix_i = i for i=0,1,,ni = 0, 1, \ldots, n.

To ensure continuity of f(x)f(x), we require that

f(xi)=fi(0)=yi,f(xi+1)=fi(1)=yi+1 \begin{aligned} f(x_i) & = f_i(0) = y_i, \\ f(x_{i + 1}) & = f_i(1) = y_{i + 1} \end{aligned}

for i=0,1,,n1i = 0, 1, \ldots, n - 1. To ensure that f(x)f(x) is sufficiently smooth, we also insist that the first derivative be continuous at each knot:

f(xi+1)=fi(1)=fi+1(0)f'(x_{i + 1}) = f'_i(1) = f'_{i + 1}(0)

for i=0,1,,n2i = 0, 1, \ldots, n - 2.

a. Suppose that for i=0,1,,ni = 0, 1, \ldots, n, we are given not only the point-value pairs {(xi,yi)}\{(x_i, y_i)\} but also the first derivatives Di=f(xi)D_i = f'(x_i) at each knot. Express each coefficient aia_i, bib_i, cic_i and did_i in terms of the values yiy_i, yi+1y_{i + 1}, DiD_i, and Di+1D_{i + 1}. (Remember that xi=ix_i = i.) How quickly can we compute the 4n4n coefficients from the point-value pairs and first derivatives?

The question remains of how to choose the first derivatives of f(x)f(x) at the knots. One method is to require the second derivatives to be continuous at the knots:

f(xi+1)=fi(1)=fi+1(0)f''(x_{i + 1}) = f''_i(1) = f''_{i + 1}(0)

for i=0,1,,n2i = 0, 1, \ldots, n - 2. At the first and last knots, we assume that f(x0)=f0(0)=0f''(x_0) = f''_0(0) = 0 and f(xn)=fn1(1)=0f''(x_n) = f''_{n - 1}(1) = 0; these assumptions make f(x)f(x) a natural cubic spline.

b. Use the continuity constraints on the second derivative to show that for i=1,2,,n1i = 1, 2, \ldots, n - 1,

Di1+4Di+Di+1=3(yi+1yi1).(23.21)D_{i - 1} + 4D_i + D_{i + 1} = 3(y_{i + 1} - y_{i - 1}). \tag{23.21}

c. Show that

2D0+D1=3(y1y0),(28.22)Dn1+2Dn=3(ynyn1).(28.23) \begin{aligned} 2D_0 + D_1 & = 3(y_1 - y_0), & \text{(28.22)} \\ D_{n - 1} + 2D_n & = 3(y_n - y_{n - 1}). & \text{(28.23)} \end{aligned}

d. Rewrite equations (28.21)\text{(28.21)}(28.23)\text{(28.23)} as a matrix equation involving the vector D=D0,D1,,DnD = \langle D_0, D_1, \ldots, D_n \rangle or unknowns. What attributes does the matrix in your equation have?

e. Argue that a natural cubic spline can interpolate a set of n+1n + 1 point-value pairs in O(n)O(n) time (see Problem 28-1).

f. Show how to determine a natural cubic spline that interpolates a set of n+1n + 1 points (xi,yi)(x_i, y_i) satisfying x0<x1<<xnx_0 < x_1 < \cdots < x_n, even when xix_i is not necessarily equal to ii. What matrix equation must your method solve, and how quickly does your algorithm run?

a. We have ai=fi(0)=yia_i = f_i(0) = y_i and bi=fi(0)=f(xi)=Dib_i = f_i'(0) = f'(x_i) = D_i. Since fi(1)=ai+bi+ci+dif_i(1) = a_i + b_i + c_i + d_i and fi(1)=bi+2ci+3dif_i'(1) = b_i + 2c_i + 3d_i, we have di=Di+12yi+1+2yi+Did_i = D_{i + 1} - 2y_{i + 1} + 2y_i + D_i which implies ci=3yi+13yiDi+12Dic_i = 3y_{i + 1} - 3y_i - D_{i + 1} - 2D_i. Since each coefficient can be computed in constant time from the known values, we can compute the 4n4n coefficients in linear time.

b. By the continuity constraints, we have fi(1)=fi+1(0)f_i''(1) = f_{i + 1}''(0) which implies that 2ci+6di=2ci+12c_i + 6d_i = 2c_{i + 1}, or ci+3di=ci+1c_i + 3d_i = c_{i + 1}. Using our equations from above, this is equivalent to

Di+2Di+1+3yi3yi+1=3yi+23yi+1Di+22Di+1.D_i + 2D_{i + 1} + 3y_i - 3y_{i + 1} = 3y_{i + 2} - 3y_{i + 1} - D_{i + 2} - 2D_{i + 1}.

Rearranging gives the desired equation

Di+4Di+1+Di+2=3(yi+2yi).D_i + 4D_{i + 1} + D_{i + 2} = 3(y_{i + 2} - y_i).

c. The condition on the left endpoint tells us that f0(0)=0f_0''(0) = 0, which implies 2c0=02c_0 = 0. By part (a), this means 3(y1y0)=2D0+D13(y_1 − y_0) = 2D_0 + D_1. The condition on the right endpoint tells us that fn1(1)=0f_{n - 1}''(1) = 0, which implies cn1+3dn1=0c_{n - 1} + 3d_{n - 1} = 0. By part (a), this means 3(ynyn1)=Dn1+2Dn3(y_n - y_{n - 1}) = D_{n - 1} + 2D_n.

d. The matrix equation has the form AD=YAD = Y, where AA is symmetric and tridiagonal. It looks like this:

(2100014100014100014100012)(D0D1D2Dn1Dn)=(3(y1y0)3(y2y0)3(y3y1)3(ynyn2)3(ynyn1)). \begin{pmatrix} 2 & 1 & 0 & 0 & \cdots & 0 \\ 1 & 4 & 1 & 0 & \cdots & 0 \\ 0 & \ddots & \ddots & \ddots & \cdots & \vdots \\ \vdots & \cdots & 1 & 4 & 1 & 0 \\ 0 & \cdots & 0 & 1 & 4 & 1 \\ 0 & \cdots & 0 & 0 & 1 & 2 \\ \end{pmatrix} \begin{pmatrix} D_0 \\ D_1 \\ D_2 \\ \vdots \\ D_{n - 1} \\ D_n \end{pmatrix} = \begin{pmatrix} 3(y_1 - y_0) \\ 3(y_2 - y_0) \\ 3(y_3 - y_1) \\ \vdots \\ 3(y_n - y_{n - 2}) \\ 3(y_n - y_{n - 1}) \end{pmatrix} .

e. Since the matrix is symmetric and tridiagonal, Problem 28-1 (e) tells us that we can solve the equation in O(n)O(n) time by performing an LUP decomposition. By part (a), once we know each DiD_i we can compute each fif_i in O(n)O(n) time.

f. For the general case of solving the nonuniform natural cubic spline problem, we require that f(xi+1)=fi(xi+1xi)=yi+1f(x_{i + 1}) = f_i(x_{i + 1} − x_i) = y_{i + 1}, f(xi+1)=fi(xi+1xi)=fi+1(0)f'(x_{i + 1}) = f_i'(x_{i + 1} - x_i) = f_{i + 1}'(0) and f(xi+1)=fi(xi+1xi)=fi+1(0)f''(x_{i + 1}) = f_i''(x_{i + 1} - x_i) = f_{i + 1}''(0). We can still solve for each of aia_i, bib_i, cic_i and did_i in terms of yiy_i, yi+1y_{i + 1}, DiD_i and Di+1D_{i + 1}, so we still get a tridiagonal matrix equation. The solution will be slightly messier, but ultimately it is solved just like the simpler case, in O(n)O(n) time.