Gauss-Legendre vs Trapezoid Quadrature
What you are seeing: two ways of approximating . The trapezoidal rule uses equispaced points and converges as for smooth . Gauss-Legendre uses optimized nodes (roots of ) with matching weights, exact for polynomials up to degree . For analytic the GL error decays exponentially.
Top panel: with nodes overlaid (orange = trapezoid, cyan = GL). Bottom: log-error vs . For smooth , GL reaches machine precision by while trapezoid still loses accuracy as .
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.