Cubic Spline Quadrature

← Numerical Methods

Source inspiration: (Mathew 2000-2019).

Description

Cubic spline quadrature approximates \(\int_a^b f(x)\,dx\) from samples \(y_i = f(x_i)\) at \(m = n+1\) nodes \(a = x_0 < x_1 < \cdots < x_n = b\). It fits the natural cubic spline \(S(x)\) through the samples and integrates the spline exactly. On each panel \([x_i, x_{i+1}]\) the spline is a cubic \(S_i(x)\), and neighbouring cubics match in value, first derivative and second derivative at the knots. With \(h_i = x_{i+1} - x_i\) and \(M_i = S''(x_i)\), the natural spline sets \(M_0 = M_n = 0\) and finds the other \(M_i\) from the tridiagonal system

\[ h_{i-1} M_{i-1} + 2 (h_{i-1} + h_i) M_i + h_i M_{i+1} = 6 \Bigl(\frac{y_{i+1} - y_i}{h_i} - \frac{y_i - y_{i-1}}{h_{i-1}}\Bigr), \qquad i = 1, \ldots, n-1, \]

and integrating each cubic gives

\[ Q_m = \int_a^b S(x)\,dx = \sum_{i=0}^{n-1} \left[\frac{h_i}{2}\,(y_i + y_{i+1}) - \frac{h_i^3}{24}\,(M_i + M_{i+1})\right]. \]

The sum is the trapezoidal rule plus a curvature correction on each panel. The method needs only the sampled values at distinct nodes, not a formula for \(f\); Mathews notes that the natural spline through them exists and is unique. The animation uses \(m\) equally spaced nodes, \(h = (b-a)/(m-1)\).

In general (Mathews’ module gives no error term), if \(f \in C^4[a, b]\) and the nodes are equally spaced, the error is

\[ Q_m - \int_a^b f(x)\,dx \approx \frac{f''(a) + f''(b)}{24\sqrt{3}}\,h^3, \]

so it is \(O(h^3)\), and halving \(h\) divides it by about 8. The natural end condition costs this order: \(M_0 = M_n = 0\) is wrong unless \(f''(a) = f''(b) = 0\), and the error it puts into the \(M_i\) near each end does not shrink with \(h\). When \(f''(a) = f''(b) = 0\), or when the spline is clamped to the true end slopes, the error is \(O(h^4)\), as for Simpson’s rule.

Case 1 does not show either rate, because its integrand \(f(x) = 1 + e^{-x}\sin(8x^{2/3})\) has \(f'(x) \approx \tfrac{16}{3}x^{-1/3}\) near \(x = 0\), unbounded, so \(f \notin C^1[0,2]\). In the frames that halve \(h\) (the table below), the error is positive only at \(m = 3\), and on the last two halvings it shrinks by about 4, which looks like \(O(h^2)\). That is not the true rate: halving \(h\) from \(m = 41\) beyond the animation divides the error by 3.50, 3.31 and 3.23, and further halvings approach \(2^{5/3} \approx 3.17\), so it shrinks only like \(h^{5/3}\). The Trapezoidal Rule on the same samples has the same rate, so the spline gains only a constant factor: its error is about half the trapezoidal error at \(m = 41\), and larger than it for \(m \le 7\).

\(m\) \(h\) \(Q_m\) \(Q_m - I\) ratio
3 \(1\) 2.461678 \(+0.4454\)
5 \(1/2\) 1.733199 \(-0.2831\) \(-1.57\)
9 \(1/4\) 1.907477 \(-0.1088\) 2.60
17 \(1/8\) 1.991279 \(-0.02500\) 4.35
33 \(1/16\) 2.010073 \(-0.006207\) 4.03

The rows are frames of Case 1, against \(I = \int_0^2 f(x)\,dx \approx 2.016280\); the ratio is the previous row’s error divided by this row’s.

The method builds on Cubic Splines and uses the same samples as the Trapezoidal Rule and Simpson’s Rule; Romberg Integration and Gauss-Legendre Quadrature reach high order on smooth integrands by other means.

Animations

Each animation below shows the natural cubic spline quadrature diagram for \(f(x)=1+e^{-x}\sin(8x^{2/3})\) on \([0,2]\). After an opening frame showing \(f\) alone, each frame recomputes the spline over the full interval using a denser odd sample count from \(m=3\) to \(m=41\) points.

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

Case 1 — Natural Cubic Spline Quadrature, \(f(x)=1+e^{-x}\sin(8x^{2/3})\) on \([0,2]\)

Behavior: The spline interpolant and integral estimate are rebuilt globally at each density level. The filled spline area tracks the curve more tightly as \(m\) increases: the area between spline and curve shrinks at every step, from 0.71 at \(m=3\) to 0.0061 at \(m=41\). The largest pointwise gap does not shrink at first: it sits under the narrow peak near \(x\approx0.08\), which the spline undershoots by more at \(m=5\) (1.11) and \(m=7\) (1.09) than at \(m=3\) (0.88). From \(m=5\) on it falls at every step, to 0.16 at \(m=41\), but it does not stay at the peak: it moves left onto the steep rise next to \(x=0\), where the \(x^{2/3}\) term gives \(f\) an infinite slope, to \(x\approx0.06\) at \(m=11\), \(0.03\) at \(m=21\) and \(0.013\) at \(m=41\). At \(m=41\) the spline’s overshoot of the peak value is only 0.03.

Julia source

Cubic Spline Quadrature animation over [0,2] with full-interval density refinement: after an opening frame of the curve alone, each frame increases sample points, rebuilds the spline, and updates the integral estimate

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.