Cubic Spline Interpolation
Cubic spline interpolation is a mathematical method commonly used to construct new points within the boundaries of a set of known points. These new points are function values of an interpolation function (referred to as spline), which itself consists of multiple cubic piecewise polynomials. This article explains how the computation works mathematically.
After an introduction, it defines the properties of a cubic spline, then it lists different boundary conditions (including visualizations), and provides a sample calculation. Furthermore, it acts as a reference for the mathematical background of the cubic spline interpolation tool on tools.timodenk.com which is introduced at the end of the article.
Introduction
Cubic spline interpolation is the process of constructing a spline
With properly chosen coefficients
Table of Notation
Symbol | Description |
---|---|
Interpolation function (spline) which predicts new points and consists of | |
Properties of a Cubic Spline
In order to determine the
Furthermore, the first and second derivative of all polynomials are identical in the points where they _touch _their adjacent polynomial. The derivatives of polynomials of degree three are
This adds another
Boundary Conditions
In order to be able to solve the system of equations, two more pieces of information are required. Arbitrary constraints like setting the third derivative in the (say) fourth point to zero may be used. However, the selection of a boundary condition, consisting of a pair of equations, is the commonly used method. The four conditions “natural spline”, “not-a-knot spline”, “periodic spline”, and “quadratic spline”, are described in detail below.
Natural Spline
The natural spline is defined as setting the second derivative of the first and the last polynomial equal to zero in the interpolation function’s boundary points:
Not-a-Knot Spline
For the not-a-knot boundary condition, the first two polynomials’ third derivatives are equal in the point where they touch (
After calculating the derivatives this can be broken down to setting
Periodic Spline
For a periodic interpolation, the last polynomial’s first and second derivative are set to be equal to the first polynomial’s first and second derivative. Graphically, this means that the function is tileable. This can be particularly helpful if
The corresponding equations are
Quadratic Spline
The quadratic boundary condition defines the first and the last polynomial to be quadratic which makes this method the simplest one. The two parabolas are highlighted in red in the plot below. Mathematically spoken, the first coefficient of both polynomials is equal to zero:
Sample Calculation
This section performs a sample calculation with a real set of data and the “natural” boundary condition. Its purpose is to demonstrate how the equations are determined and actually turned into an interpolation function.
Given the set of points
Furthermore, the first and second derivatives of all adjacent polynomials are equal in the point where they touch. The
The final two pieces of information are contained in the chosen boundary condition which in this example is the natural spline. The corresponding equations are
All equations are forming a linear system of equations which can be written in matrix notation.
- Row 1 to 8: Equations expressing that the polynomials traverse the points in
. - Row 9 to 11: The first derivative of two adjacent polynomials is equal in the point where they touch.
- Row 12 to 14: The second derivative of two adjacent polynomials is equal in the point where they touch.
- Row 15 and 16: The boundary condition (natural spline).
This system of equations can be solved (e.g. with Gaussian elimination) so that the function coefficients are calculated and the interpolation function
Online Tool
The cubic spline interpolation tool on tools.timodenk.com performs a cubic spline interpolation and visualizes the resulting function derived from a data set provided by the user. It offers selection of the boundary conditions and of the axes scaling. If needed, the corresponding
As a side note for developers, it is worth mentioning that a user of the tool reported a reproducible, weird interpolation result for a specific set of data. As it turned out, this was caused by the floating point precision of JavaScript which was too low for computing the coefficients from the system of equations without significant loss of precision. The issue was resolved by using the library Math.js. It features among other things a BigNumber
class for very high precision numbers, which are now in use.