Numerical Integration Playground

Midpoint · Simpson · Gauss–Legendre · Monte-Carlo · ← Back to index

1. Pick a function and an integration rule

Drag the red dots left/right to place samples by hand; weights are auto-chosen by matching monomial moments.
f(ξ) sample point ξi weight stem (height ∝ Wi · f) exact integral region
Exact ∫ f dξ : Estimate Σ W f : Error : Rel. error : Polynomial deg : Rule exact thru deg :
Gauss–Legendre with n points is exact for every polynomial of degree ≤ 2n−1. Try the “polynomial of degree 5” preset with Gauss n=3: the error should be zero to machine precision.

2. Moment-matching gauges — the heart of the derivation

Each gauge compares Σi Wi ξik (computed from your points) to −11 ξk (the true moment). A rule with n points has exactly 2n free parameters (the positions ξi and weights Wi), so it can match 2n moments — i.e. it is exact for every polynomial of degree ≤ 2n−1.

3. Monte-Carlo “rain” (physics-flavored sampling)

raindrop caught (below curve) missed (above curve) bounding box
Drops so far : 0 Caught below f : 0 MC estimate : Exact : MC error : Gauss n=2 error :
Drops fall from the top of the bounding box at random ξ. Those below the curve are “caught” (green). The integral is approximated by (hits / total) × (box area). This is the slowest method on the page — error shrinks like 1/√N. Notice how many drops it takes to beat a 2-point Gauss rule.

4. Convergence — how fast does each rule get it right?

For the currently-selected function, the plot shows the absolute error of each rule as the number of evaluations N grows. Both axes are log-scaled. On a smooth function, Gauss quadrature converges exponentially while the midpoint rule only manages O(1/N²). On the non-smooth functions (|ξ| and Runge), the story is messier — try them and see.
— Gauss–Legendre — Simpson (composite) — Midpoint (composite)