Spline interpolation is useful for creating “continuous” curves out of discrete data points. These curves are can have C1 (first derivative) and C2 (second derivative) continuity, allowing for a smooth join between segments of the curve. An algorithm for computing natural cubic splines was listed on Wikipedia, and have reproduced it here so as to have the full math -> code translation in one place.
Definition of a Cubic Spline
Given a list of data values
where is an n-tuple defined as
with corresponding location values defined as
we wish to compute a value of Y for any value in the range . We will do this for some
by evaluating a cubic spline
.
A cubic spline is of the form
where is a 5-tuple describing the parameters of
.
Given our set of data and locations
, we wish to find
polynomials
for
such that
We can compute the parameters of these polynomials using the algorithm below.
Computation of Cubic Spline Parameters
Input: list of points , with
, with locations
Output: list spline parameters composed of 5-tuples.
- Create new arrays
of size
- Create new arrays
of size
- Set
- Set
- For
- Set
- Set
- Set
- Set
- Set
- Set
- For
- Set
- Set
- Set
- For
- Set
For a C++ implementation of this algorithm, see Cubic Spline Interpolation