Calculus
Riemann Sum
Approximate area under a curve by adding up rectangles — the definition of the integral
A Riemann sum approximates the area under a curve by dividing the interval into rectangles and summing their areas. As the rectangles get thinner (n → ∞), the sum approaches the exact integral. It's the formal definition of the definite integral — and the practical foundation of all numerical integration methods used in physics, engineering, and computer graphics.
- Formula∑_{i=1}^n f(xᵢ) Δx
- Limit defines integral∫_a^b f(x) dx = lim_{n→∞} ∑ f(xᵢ) Δx
- Three sample-point conventionsLeft, right, midpoint (or random)
- Required for convergencef bounded on [a, b]; integrable
- OriginatorBernhard Riemann (1854)
- Real-world equivalentsTrapezoidal rule, Simpson's rule, Monte Carlo integration
Interactive visualization
Press play, or step through manually. The visualization is yours to drive — try it before reading on.
Watch the 60-second explainer
A condensed visual walkthrough — narrated, captioned, under a minute.
How a Riemann sum works
To approximate ∫_a^b f(x) dx:
- Divide [a, b] into n equal subintervals, each of width Δx = (b − a)/n.
- In each subinterval, pick a sample point — left endpoint, right endpoint, or midpoint.
- Form a rectangle of width Δx and height f(sample point).
- Sum the rectangle areas — that's the Riemann sum.
Riemann sum = ∑_{i=1}^n f(xᵢ) · Δx
As n → ∞ (rectangles become infinitely thin), the Riemann sum approaches the exact area under the curve — that's the integral.
Three sample-point conventions
| Type | Sample point in i-th subinterval | Best for | Approximation error |
|---|---|---|---|
| Left | x = a + (i−1)·Δx | Decreasing functions (overestimates) or increasing (underestimates) | O(1/n) |
| Right | x = a + i·Δx | Same — opposite over/under | O(1/n) |
| Midpoint | x = a + (i − 0.5)·Δx | General use; cancels first-order errors | O(1/n²) |
| Trapezoid (avg of L and R) | (L + R) / 2 area per strip | Smooth functions | O(1/n²) |
| Simpson's rule | Quadratic interpolation per pair of strips | Smooth functions; better accuracy | O(1/n⁴) |
For numerical work, midpoint or Simpson's rule is preferred. Left and right are mostly pedagogical — easy to compute by hand, easy to bound the error.
Worked example — ∫_0^1 x² dx
Exact answer (via FTC) — x³/3 evaluated 0 to 1 gives 1/3.
Left Riemann sum with n = 4 (Δx = 0.25):
x₀, x₁, x₂, x₃ = 0, 0.25, 0.50, 0.75 (left endpoints of 4 subintervals)
f(x₀) + f(x₁) + f(x₂) + f(x₃) = 0 + 0.0625 + 0.25 + 0.5625 = 0.875
Sum × Δx = 0.875 × 0.25 = 0.21875
Right Riemann sum with n = 4:
x₁, x₂, x₃, x₄ = 0.25, 0.50, 0.75, 1.0 (right endpoints)
f's = 0.0625 + 0.25 + 0.5625 + 1.0 = 1.875
Sum × Δx = 1.875 × 0.25 = 0.46875
True value 1/3 ≈ 0.333. The left sum (0.219) underestimates — function is increasing, left endpoints are smaller. The right sum (0.469) overestimates. Their average (0.344) is much closer — that's the trapezoid rule.
Midpoint Riemann sum with n = 4:
Midpoints — 0.125, 0.375, 0.625, 0.875
f's = 0.015625 + 0.140625 + 0.390625 + 0.765625 = 1.3125
Sum × Δx = 1.3125 × 0.25 = 0.328125
0.328 vs true 0.333 — error 0.005, much better than left or right. Midpoint cancels the first-order error term that left and right share.
JavaScript — Riemann and friends
function leftRiemann(f, a, b, n = 1000) {
const dx = (b - a) / n;
let sum = 0;
for (let i = 0; i < n; i++) sum += f(a + i * dx);
return sum * dx;
}
function rightRiemann(f, a, b, n = 1000) {
const dx = (b - a) / n;
let sum = 0;
for (let i = 1; i <= n; i++) sum += f(a + i * dx);
return sum * dx;
}
function midpointRiemann(f, a, b, n = 1000) {
const dx = (b - a) / n;
let sum = 0;
for (let i = 0; i < n; i++) sum += f(a + (i + 0.5) * dx);
return sum * dx;
}
function trapezoid(f, a, b, n = 1000) {
const dx = (b - a) / n;
let sum = 0.5 * (f(a) + f(b));
for (let i = 1; i < n; i++) sum += f(a + i * dx);
return sum * dx;
}
function simpson(f, a, b, n = 1000) {
if (n % 2) n++;
const dx = (b - a) / n;
let sum = f(a) + f(b);
for (let i = 1; i < n; i++) {
sum += (i % 2 === 0 ? 2 : 4) * f(a + i * dx);
}
return sum * dx / 3;
}
const f = x => Math.exp(-x*x); // Gaussian — no closed-form antiderivative
console.log(leftRiemann(f, 0, 1)); // ≈ 0.7508
console.log(rightRiemann(f, 0, 1)); // ≈ 0.7472
console.log(midpointRiemann(f, 0, 1)); // ≈ 0.7468
console.log(trapezoid(f, 0, 1)); // ≈ 0.7468
console.log(simpson(f, 0, 1)); // ≈ 0.74682 (most accurate)
// True value (erf-based): ≈ 0.74682
Riemann sums and numerical integration
For functions whose antiderivative is unknown or unwieldy, numerical integration uses Riemann-style summation. The advanced methods all build on the same idea:
| Method | Idea | Convergence | Notes |
|---|---|---|---|
| Left/right Riemann | Constant per strip | O(1/n) | Pedagogical only |
| Trapezoidal rule | Linear per strip | O(1/n²) | Average of left and right |
| Simpson's 1/3 rule | Quadratic per pair | O(1/n⁴) | Standard for smooth functions |
| Gauss-Legendre quadrature | Polynomial-fit weighted samples | O((1/n)^(2k+1)) | Highest accuracy per evaluation |
| Monte Carlo | Random sampling, average | O(1/√n) | Best for high-dimensional integrals |
| Adaptive quadrature | Refine where curve changes fast | Adaptive | Used in production scientific code |
For most engineering applications, trapezoidal or Simpson's rules are sufficient. For high-dimensional integrals (more than ~5 dimensions), Monte Carlo's curse-of-dimensionality-resistance wins.
When you'd use a Riemann sum directly
- The antiderivative isn't expressible in elementary functions. Many physics integrals (Gaussian e^(−x²), elliptic integrals, certain probability densities). Numerical integration is the only option.
- You have data points, not a function. Measured quantities — temperature over time, voltage over time, sales over months. Sum the data times the time step to get total quantity.
- Teaching the concept of integration. Riemann sums show what an integral fundamentally IS — the limit of approximating sums. The FTC bypasses this for calculation but obscures the concept.
- Verifying analytical answers. Numerical integration gives a sanity check on closed-form computations.
- Computer graphics — shading, lighting integrals. Render equations are integrals over hemispheres of light directions; Monte Carlo Riemann-like sampling estimates them.
- Probability and statistics. Computing tail probabilities of distributions whose CDF has no closed form (like the Normal CDF).
Common mistakes
- Confusing the Riemann sum approximation with the exact integral. A Riemann sum APPROACHES the integral as n → ∞. For finite n, there's an error. The FTC gives the exact value when an antiderivative exists.
- Wrong sample point for the convention. Left Riemann uses x_i (left endpoint of i-th subinterval), not x_{i+1}. Off-by-one errors are easy in the indexing.
- Forgetting Δx. Each rectangle's area is f(sample) × Δx. The sum is over n terms, each scaled by Δx. Forgetting it gives the wrong result by a factor of n.
- Using too few subdivisions. n = 10 is fine for smooth functions; n = 1000 for moderately oscillatory; more for sharp features. Estimate by repeating with double the n; if the answers differ significantly, refine more.
- Floating-point error accumulation. Summing many small numbers loses precision. Use Kahan summation or Neumaier's variant for production numerical integration.
- Trying Riemann sums on integrals with singularities. ∫_0^1 1/x dx diverges; standard Riemann sums silently give garbage. Use improper-integral techniques or transform variables.
Frequently asked questions
Why approximate the area as rectangles?
Because rectangles are easy to compute. Width × height is one multiplication. Adding many rectangle areas approximates the curve's area as a sum. The approximation gets exact in the limit as rectangle width → 0. This idea — summing simpler shapes to approximate complex ones — generalizes to many areas of math (volume by triangulation, integrals via cylindrical shells).
What's the difference between left, right, and midpoint Riemann sums?
They differ by where you evaluate f within each subinterval. Left Riemann sum uses f(xᵢ) at the left endpoint. Right uses f(xᵢ₊₁). Midpoint uses f((xᵢ + xᵢ₊₁)/2). For a small subinterval, all three give similar values; for finite n, they differ. Midpoint is generally most accurate (cancels first-order errors), then trapezoid, then left/right tied for last.
Why does the Riemann sum equal the integral in the limit?
Because as the rectangles get thinner, their combined area approaches the actual area under the curve. Mathematically — for any continuous (or "Riemann integrable") f, the limit of Riemann sums is the same regardless of which sample points you pick (as long as the partition becomes infinitely fine). That common limit is, by definition, the integral.
What does Riemann integrability mean?
A function is Riemann integrable on [a, b] if its Riemann sums converge to a single value, regardless of partition or sample points. Bounded continuous functions are integrable. Bounded functions with countably many discontinuities are integrable. Functions with too many discontinuities (Dirichlet function — 1 on rationals, 0 on irrationals) are not Riemann integrable but may be Lebesgue integrable.
When would I use a Riemann sum directly instead of an antiderivative?
When the antiderivative isn't computable in closed form (like ∫ e^(−x²) dx). When you have data points instead of a function (numerical integration of measured data). When you're teaching the concept of an integral. When you're proving theorems about integrals. For day-to-day calculation, the FTC bypasses Riemann sums.
What's the difference between Riemann and Lebesgue integration?
Riemann partitions the x-axis (domain). Lebesgue partitions the y-axis (codomain). Lebesgue handles more pathological functions (the Dirichlet function is Lebesgue but not Riemann integrable). For "nice" functions, both give the same answer. Modern probability theory and measure theory use Lebesgue; introductory calculus teaches Riemann.
How is a Riemann sum used in physics?
Computing total quantities from rates. Total displacement from velocity — ∫v(t) dt becomes a Riemann sum. Work from force — ∫F·dx. Charge from current — ∫I(t) dt. Whenever you have a "rate" that varies, summing the rate times short intervals gives the total — that's a Riemann sum.