Search code examples
splinederivative

How do I calculate the first derivative on a discrete set of points in cubic spline implementation?


I'm trying to implement a cubic spline interpolation. Cubic splines are not that hard to understand mathematically, but I have a problem understanding the term "Di" as shown in the fourth equation on mathworld.

Does Di refer to a finite difference calculated by taking the current and the next point (y_i and y_i+1 respectively) from the discrete set of points?


Solution

  • Di is simply the first derivative vector of the cubic spline at point y_i. It can be calculated by various ways including finite difference. Catmull-Rom spline is a commonly-used spline that infers first derivative of y_i from y_(i-1) and y_(i+1).

    The first part of that MathWorld document (up to eq. 9) simply shows that if you have n points and n first derivatives, the cubic spline that passes thru all the n points will be fully defined. Note that this cubic spline is consist of (n-1) cubic polynomial and is in general only C1 continuous.

    The 2nd part of that document after eq. 9 shows that you can eliminate the need of the first derivatives by making the 2nd derivatives between cubic polynomials the same at all interior points (i.e., eq. 13). However, doing so will result in (4n-2) equations with 4n unknowns. Therefore, we need to add two more conditions. Zero 2nd derivatives at first and the last point (eq 16, 17) are commonly used for these 2 conditions and this will also result in the so-called natural cubic spline.