Riemann Integral

∫ f(x) dx = lim(N→∞) Σ f(xᵢ)·Δx — the area under a curve as the limit of rectangle sums

Mathematics Calculus Numerical Methods Convergence
Function:
Method:
N = 10 Riemann sum ≈ Exact ∫ = Error = Δx =

∫ The Riemann Integral

The definite integral is defined as the limit of Riemann sums: ∫ₐᵇ f(x) dx = lim(N→∞) Σᵢ f(x*ᵢ)·Δx where Δx = (b−a)/N and x*ᵢ is a sample point in the i-th subinterval.

Four classic approximation rules:

  • Left: x*ᵢ = a + i·Δx — uses left endpoint of each rectangle. Error O(Δx).
  • Right: x*ᵢ = a + (i+1)·Δx — uses right endpoint. Also O(Δx).
  • Midpoint: x*ᵢ = a + (i+½)·Δx — more accurate, error O(Δx²).
  • Trapezoid: ½(f(xᵢ)+f(xᵢ₊₁))·Δx — connects top corners with a line. Error O(Δx²).

The error bound: |Error| ≤ M·(b−a)³/(12N²) for Midpoint and Trapezoid (M = max|f″|). Midpoint is exactly twice as accurate as Trapezoid for smooth functions, and together form Simpson's Rule = (2×Midpoint + Trapezoid) / 3 with error O(Δx⁴).

About this simulation

This simulator visualises Riemann sums converging to the exact definite integral of five functions — sin x, x², eˣ, |x|, and √x — over an adjustable interval [a, b]. Each of the N subintervals has a fixed width Δx = (b−a)/N, and its rectangle's height is sampled per one of four classic rules — Left endpoint f(x₀), Right endpoint f(x₁), Midpoint f((x₀+x₁)/2), or Trapezoid ½(f(x₀)+f(x₁)) — then summed and multiplied by Δx. The canvas draws these N shapes beneath the true curve, while a lower strip re-plots the absolute error |sum−exact| for every n from 1 up to min(N+1, 50), so you can watch first-order and second-order convergence side by side.

🔬 What it shows

The main canvas overlays N Left/Right/Midpoint rectangles or Trapezoid panels against the true function curve (drawn in white) between the dashed bounds a and b; the strip beneath it plots how the approximation error shrinks as N grows, letting you compare how quickly each rule converges.

🎮 How to use it

Pick a function (sin x, x², eˣ, |x|, or √x), choose a summation rule, then drag the Subintervals slider (1–200) and the a/b sliders to reshape the interval. The stats bar below the canvas updates the Riemann sum, exact integral, error, and Δx live as you move any control.

💡 Did you know?

The simulator recomputes the whole error-vs-N curve every frame by re-running riemannSum() for n = 1 up to min(N+1, 50). Averaging Midpoint and Trapezoid two-to-one — (2×Midpoint + Trapezoid)/3 — reproduces Simpson's Rule, whose error shrinks as O(Δx⁴), far faster than either rule alone.

Frequently asked questions

Why does the Trapezoid rule draw slanted tops instead of flat rectangles?

In the draw() function, method === "trap" is the only branch that fills a quadrilateral connecting (x₀,0) → (x₀,f(x₀)) → (x₁,f(x₁)) → (x₁,0), instead of a single sampled height like Left, Right, and Midpoint. That slanted top is exactly the average ½(f(x₀)+f(x₁)) used by riemannSum() for the trapezoid method.

Why is the plotted error so much larger for eˣ than for sin x at the same N?

The theoretical bound |error| ≤ M·(b−a)³/(12N²) depends on M = max|f″| over [a,b]. Since sin x has |f″| bounded by 1 everywhere, while eˣ's second derivative is eˣ itself and grows without bound as b increases, the same N produces a visibly larger error curve for eˣ in the lower panel.

Why does √x behave oddly when the lower bound a is negative?

FUNS.sqrt.f returns 0 for any x < 0 (x ≥ 0 ? Math.sqrt(x) : 0), and its exact() clamps both limits to max(0,·) before integrating. So dragging a below zero doesn't extrapolate a negative square root — the simulator simply treats the integrand as zero on that portion of the interval, matching √x's real domain restriction.

What exactly does the lower error panel plot, and why does it stop at 50?

For every n from 1 up to min(N+1, 50), the script calls riemannSum(fn, a, b, n, method), takes the absolute difference from the exact integral, and plots the resulting curve. The cap at 50 points keeps the per-frame recomputation cheap even when the Subintervals slider N is pushed up to its maximum of 200.

Why do Midpoint and Trapezoid errors shrink faster than Left and Right as N increases?

Left and Right sample only one endpoint of each subinterval, so their local error per panel is O(Δx) and the total error scales as O(Δx); Midpoint and Trapezoid effectively average out the curvature of f across the panel, giving O(Δx²) local error and total error — the same second-order convergence guaranteed by the |error| ≤ M(b−a)³/(12N²) bound shown in the info panel.