Cubic Splines

Numerical Methods

Source inspiration: (Mathew 2000-2019).

Description

A cubic spline is a piecewise-cubic interpolant built on nodes \(x_0<\cdots<x_n\) so that each panel polynomial \(S_i(x)\) interpolates endpoint values and the full spline is globally smooth. The construction enforces continuity of \(S(x)\), \(S'(x)\), and \(S''(x)\) at interior knots.

For a natural spline, the boundary conditions are

\[ S''(x_0)=0,\qquad S''(x_n)=0. \]

The unknown second derivatives are found from a tridiagonal linear system, then each cubic panel is reconstructed. For smooth functions, spline interpolation error typically scales like \(O(h^4)\) with mesh spacing \(h\).

Animations

The animation below shows a natural cubic spline fitted to equally-spaced data from \(f(x) = \sin(x)\) on \([0, 2\pi]\), with the number of nodes increasing. The natural spline satisfies \(S''(a) = S''(b) = 0\) at the endpoints.

Julia source scripts that generated these animations are linked under each case.

Case 1 — Natural cubic spline convergence for \(\sin(x)\)

Behavior: Adding nodes shrinks the maximum error rapidly. The natural cubic spline converges at \(O(h^4)\) for smooth functions, where \(h\) is the maximum node spacing. Each frame uses a fully recomputed spline (solving the tridiagonal second-derivative system) for the new set of nodes.

Julia source

Natural cubic spline approximating sin(x) on [0, 2pi] with n=3,4,5,6,7,9 nodes; the spline closely tracks the true curve as nodes increase

References

Mathew, John H. 2000-2019. Numerical Analysis - Numerical Methods Modules. https://web.archive.org/web/20190808102217/http://mathfaculty.fullerton.edu/mathews/n2003/NumericalUndergradMod.html.