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} of n+1 point-value pairs, where x0<x1<⋯<xn. We wish to fit a piecewise-cubic curve (spline) f(x) to the points. That is, the curve f(x) is made up of n cubic polynomials fi(x)=ai+bix+cix2+dix3 for i=0,1,…,n−1, where if x falls in the range xi≤x≤xi+1, then the value of the curve is given by f(x)=fi(x−xi). The points xi at which the cubic polynomials are "pasted" together are called knots. For simplicity, we shall assume that xi=i for i=0,1,…,n.
To ensure continuity of f(x), we require that
f(xi)f(xi+1)=fi(0)=yi,=fi(1)=yi+1
for i=0,1,…,n−1. To ensure that 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)
for i=0,1,…,n−2.
a. Suppose that for i=0,1,…,n, we are given not only the point-value pairs {(xi,yi)} but also the first derivatives Di=f′(xi) at each knot. Express each coefficient ai, bi, ci and di in terms of the values yi, yi+1, Di, and Di+1. (Remember that xi=i.) How quickly can we compute the 4n coefficients from the point-value pairs and first derivatives?
The question remains of how to choose the first derivatives of 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)
for i=0,1,…,n−2. At the first and last knots, we assume that f′′(x0)=f0′′(0)=0 and f′′(xn)=fn−1′′(1)=0; these assumptions make f(x) a natural cubic spline.
b. Use the continuity constraints on the second derivative to show that for i=1,2,…,n−1,
d. Rewrite equations (28.21)–(28.23) as a matrix equation involving the vector D=⟨D0,D1,…,Dn⟩ or unknowns. What attributes does the matrix in your equation have?
e. Argue that a natural cubic spline can interpolate a set of n+1 point-value pairs in O(n) time (see Problem 28-1).
f. Show how to determine a natural cubic spline that interpolates a set of n+1 points (xi,yi) satisfying x0<x1<⋯<xn, even when xi is not necessarily equal to i. What matrix equation must your method solve, and how quickly does your algorithm run?
a. We have ai=fi(0)=yi and bi=fi′(0)=f′(xi)=Di. Since fi(1)=ai+bi+ci+di and fi′(1)=bi+2ci+3di, we have di=Di+1−2yi+1+2yi+Di which implies ci=3yi+1−3yi−Di+1−2Di. Since each coefficient can be computed in constant time from the known values, we can compute the 4n coefficients in linear time.
b. By the continuity constraints, we have fi′′(1)=fi+1′′(0) which implies that 2ci+6di=2ci+1, or ci+3di=ci+1. Using our equations from above, this is equivalent to
c. The condition on the left endpoint tells us that f0′′(0)=0, which implies 2c0=0. By part (a), this means 3(y1−y0)=2D0+D1. The condition on the right endpoint tells us that fn−1′′(1)=0, which implies cn−1+3dn−1=0. By part (a), this means 3(yn−yn−1)=Dn−1+2Dn.
d. The matrix equation has the form AD=Y, where A is symmetric and tridiagonal. It looks like this:
e. Since the matrix is symmetric and tridiagonal, Problem 28-1 (e) tells us that we can solve the equation in O(n) time by performing an LUP decomposition. By part (a), once we know each Di we can compute each fi in O(n) time.
f. For the general case of solving the nonuniform natural cubic spline problem, we require that f(xi+1)=fi(xi+1−xi)=yi+1, f′(xi+1)=fi′(xi+1−xi)=fi+1′(0) and f′′(xi+1)=fi′′(xi+1−xi)=fi+1′′(0). We can still solve for each of ai, bi, ci and di in terms of yi, yi+1, Di and Di+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) time.