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

  • Vary each control and watch the rail readouts respond.
  • Compare the diagnostic plot against the live scene.