Controls

5

			

Approximating definite integrals with quadrature rules

A quadrature rule approximates a definite integral by a weighted sum of function values at a finite number of sample points ("nodes"): $$\int_a^b f(x)\,dx \;\approx\; \sum_{i=1}^{n} w_i \, f(x_i).$$ All rules below are stated on the reference interval $u \in [-1,1]$. An integral over an arbitrary interval $[a,b]$ is mapped onto it with the linear substitution $$\int_a^b f(x)\,dx = \frac{b-a}{2} \int_{-1}^{1} f\!\left(\frac{b-a}{2}u + \frac{a+b}{2}\right) du,$$ so any rule for $[-1,1]$ immediately gives a rule for $[a,b]$.

Standard (Newton–Cotes) rules

All Newton–Cotes rules follow the same recipe: fix the nodes to be equally spaced in advance, replace $f$ on each subinterval by the polynomial that interpolates it at those nodes, and integrate that polynomial exactly — its integral is a fixed linear combination of the sampled values, which is exactly where the weights $w_i$ come from. Because the nodes are fixed rather than optimized, this only pins down $n$ degrees of freedom (the weights), not the $2n$ that Gauss–Legendre gets to use below. With $n$ subintervals of width $h = 2/n$ on $[-1,1]$:

All three formulas above assume $f$ is smooth enough for the relevant derivative ($f''$ or $f^{(4)}$) to exist and stay bounded; the "Non-smooth: $|x|$" test function violates this at $u=0$; try it with each rule and note how the error columns in the results table drop to the common, much slower, first-order rate regardless of which rule is selected.

Gauss–Legendre quadrature

Gauss–Legendre quadrature keeps the same basic shape as the rules above — a weighted sum of $n$ samples, $$\int_{-1}^{1}\! f(u)\,du \;\approx\; \sum_{a=1}^{n} w_a\, f(s_a) \;=\; w_1 f(s_1) + w_2 f(s_2) + \cdots + w_n f(s_n),$$ but no longer fixes the nodes $s_a$ in advance. Instead it treats both the $n$ nodes $s_a$ and the $n$ weights $w_a$ as free parameters — $2n$ degrees of freedom in total — and chooses them so that the rule integrates every polynomial of degree $\le 2n-1$ exactly. This is twice the polynomial degree a Newton–Cotes rule can reach with the same number of samples. The optimal nodes turn out to be the roots of the $n$-th Legendre polynomial $P_n$, and the corresponding weights follow directly from them: $$P_n(s_a) = 0, \qquad w_a = \frac{2}{\left(1-s_a^2\right)\left[P_n'(s_a)\right]^2}.$$ The Legendre polynomials satisfy the recurrence $P_0=1$, $P_1(u)=u$, $k P_k(u) = (2k-1)\,u\,P_{k-1}(u) - (k-1)P_{k-2}(u)$, which is used together with Newton's method (starting from $s_a^{(0)} = \cos\!\big(\pi\,\frac{a-0.25}{n+0.5}\big)$) to compute the $s_a$ in the code below — the same approach as the classic gauleg routine [PTVF07].

Because the weights are no longer all equal, the right plot draws the $a$-th term $w_a f(s_a)$ of the sum as a rectangle of height $\psi(s_a)$ centred at the node $s_a$, whose width is the weight $w_a$ itself (widths sum to $2$, the length of $[-1,1]$, just like the subinterval widths on the left). A wide rectangle marks a node that "represents" a larger share of the interval; nodes cluster more densely near the ends of $[-1,1]$, where the Legendre polynomial's roots naturally accumulate, which is what keeps their individual weights roughly balanced.

Why Gauss–Legendre usually wins

For a smooth (analytic) integrand, Gauss–Legendre quadrature converges spectrally — the error shrinks faster than any polynomial power of $1/n$ — while the Newton–Cotes rules above are stuck at their fixed algebraic order ($O(h^2)$ or $O(h^4)$). Increase $n$ for the smooth test functions below and watch the reported error columns: the Gauss–Legendre error collapses to machine precision within a handful of points, long before the other rules catch up. This advantage disappears for the non-smooth test function $f(x)=|x|$, whose derivative jumps at $x=0$: there, Gauss–Legendre still converges, but only at the same modest algebraic rate as the other rules, because the argument that guarantees spectral convergence relies on $f$ being (arbitrarily often) differentiable.

References