Back

Gauss-Legendre vs Trapezoid Quadrature

What you are seeing: two ways of approximating 11f(x)dx\int_{-1}^{1} f(x)\, dx. The trapezoidal rule uses n+1n + 1 equispaced points and converges as O(h2)O(h^2) for smooth ff. Gauss-Legendre uses nn optimized nodes (roots of PnP_n) with matching weights, exact for polynomials up to degree 2n12n - 1. For analytic ff the GL error decays exponentially.

Top panel: f(x)f(x) with nodes overlaid (orange = trapezoid, cyan = GL). Bottom: log-error vs nn. For smooth cos(2x)\cos(2x), GL reaches machine precision by n=16n = 16 while trapezoid still loses accuracy as O(h2)O(h^2).

Figure 1. Quadrature shootout. Method: Gauss-Legendre via Golub-Welsch eigenproblem and trapezoid via equispaced sum.
n nodes8
functioncos
speed2

WHAT TO TRY

  • Raise the node count n: the trapezoid error falls slowly as 1/n-squared, while Gauss-Legendre plunges to machine precision within a handful of nodes. The error-vs-n panel shows the exponential gap.
  • Note where Gauss puts its nodes: clustered, unequally weighted, and never at the endpoints, which is how n points integrate polynomials up to degree 2n-1 exactly.
  • Change the integrand: smooth functions let Gauss converge spectrally, but a kink or singularity drags both schemes back to algebraic convergence.