Interpolating splines

January 22nd, 2006 ~ Posted in: Mathematics, Programming

Note: I spent a weekend writing a simple implementation of the inefficent spline paradigm described here, in PyGTK; check it out: get the python spline fitting and evaluation code, and PyGTK stub interface. The feature set is small: add new points in the interface by left clicking, and middle click to clear.

Here’s the motivating problem: you have points \{x_i, y_i\} observed at times \{t_i\}– say, samples of the position function of a particle tracing out a path– and you’d like to fit a smooth curve \vec{g}(t) to these points. An intuitive approach to this problem is to represent \vec{g}(t) = (g_x(t), g_y(t)) parametrically, and fit smooth curves to the abscissa and ordinates; this reduces the problem to that of fitting curves to points, which we can solve in many different ways (Lagrange interpolation, etc.) One approach which has several desirable features is that of interpolating splines.

Interpolating splines can be developed under the energy minimization paradigm, in an interesting manner. Under this paradigm, the problem is to, given the points y = \{t_i, \xi_i\}, find a smooth function \hat{g} satisfying:

 \displaystyle \hat{g} = \text{argmin}_g U(g\:|\:y),

where U(g\:|\:y), the energy function, represents some measure of the undesirability of using the function g to represent y. The energy function has two components, U_1(g), the internal energy, which corresponds to a measure of the desirability of g, and U_2(g\:|\:y), the external energy, which corresponds to the compatibility of y and g. To develop splines, we use

 \displaystyle U(g\:|\:y) = \alpha U_1(g) + U_2(g\:|\:y) = \alpha \int \left(\left(D^m g\right)(t)\right)^2\:dt + \sum_{i=1}^n \left(\xi_i - g(t_i)\right)^2,

that is, we look for functions whose variation (measured as the L^2 norm of their m-th derivative) is minimal, and which fit the data points in a least means squared sense. The parameter \alpha controls the relative importance we assign to U_1 and U_2; in what follows, when it matters, we assume \alpha is fixed.

Here’s the first amazing result: Such \hat{g} exist, and furthermore

  1. The curve \hat{g} is a polynomial of degree m+1 on each interval (t_i, t_{i+1}).
  2. At each point t_i, the first m derivatives of \hat{g} are continuous, while the (m+1)-th can be discontinuous.
  3. In each of the intervals (-\infty, t_1) and (t_n, \infty), D^m\hat{g}(t) = 0.

I’d love to see a proof of this theorem– I suspect it uses the variational calculus. Certainly (ii) and (iii) are obvious, but (i) is not to me.

Now, we can define splines:

A curve that satisfies (i) and (ii) with m=2 is called a cubic or bending spline with knots {\cal T} = \{t_i\}. If it also satisfies (iii), it is called a natural cubic spline. An interpolation spline is a cubic spline which passes through the points \{t_i, \xi_i\}.

So the above theorem guarantees the existence of interpolating cubic splines. But we can do even better, with the next theorem, which shows us how to construct such splines:

Let \xi=\{\xi_i\} be n values set according to t_i. There is only one natural spline g interpolating these \xi_i values.

Proof:
By (i) above, on each interval (t_i, t_{i+1}), g has the form

 \displaystyle g(t) = a_i(t - t_i)^3 + b_i(t-t_i)^2 + c_i(t-t_i) + d_i.

For convenience, set h_{i+1} = t_{i+1} - t_i and M_i = D^2g(t_i), then you can show


\displaystyle
b_i = M_i/2 \quad a_i = (M_{i+1} - M_i)/(6h_{i+1}) \\
d_i = \chi_i \quad c_i = \frac{\xi_{i+1} - \xi_i}{h_{i+1}} - h_{i+1}\frac{2M_i + M_{i+1}}{6},

and from (ii) above, we haveD^1g(t_i^-) = D^1g(t_i^+) for i=2,\ldot,n-1, giving

 \displaystyle M_{i-1}h_i + 2M_i(h_i + h_{i+1}) + M_{i+1}h_{i+1} = 6 \left( \frac{\xi_{i+1} - \xi_i}{h_{i+1}} - \frac{\xi_i - \xi_{i-1}}{h_i}\right).

This gives us n-2 equations we can solve for the n-2 unknowns M_i, \: i=1,\ldots,n (we know by (iii) M_1 = M_n = 0); these relations can be written in the linear system RM = Q\xi, whereR is a tridiagonal n-2 \times n-2 matrix, Q is a tridiagonal n-2 \times n matrix, M is the n-2-vector of 2nd derivatives of g, and \xi is the n-vector of the values g should interpolate. This system can be solved for M = R^-1 Q \xi, and these values can be used to calculate the a_i, b_i, c_i, d_i

See my code for implementation of this scheme for 2d interpolation. At some point, I’m going to show how the energy minimization scheme can be adapted from interpolation to smoothing.

This entry was posted on Sunday, January 22nd, 2006 at 1:24 pm and is filed under Mathematics, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply