What a Riemann sum is
A Riemann sum replaces the area under a curve with a row of rectangles. You cut [a, b] into n pieces of equal width Δx = (b − a)/n, pick one point in each piece, use the height of the curve there as the height of a rectangle, and add up the areas. The result approximates ∫f(x)dx, and Riemann's definition of the integral is the statement that as n → ∞ this sum converges to a single number no matter which sample point you choose in each piece.
That last clause is the reason left, right and midpoint sums all appear in a first calculus course. They give different answers for finite n and the same answer in the limit, which is what makes the integral well defined. Watching them converge to a common value is how the definition stops being an abstraction.
The sum is signed. Where f is negative the rectangle height is negative and its contribution subtracts, so a Riemann sum for sin x over [0, 2π] comes out near zero rather than near 4. If you want geometric area rather than signed area, integrate abs(f(x)) — the calculator accepts abs for exactly this reason.
Riemann sums are not only pedagogy. Every quadrature rule in numerical analysis is a weighted sum of function values on a grid, and the left, right, midpoint and trapezoid rules are the simplest members of that family. If you have data rather than a formula, a sum like this is the only integral available to you.
The three sampling rules, and why the midpoint rule wins
Each rule differs only in where xₖ* sits inside subinterval k. The left rule takes xₖ* = a + kΔx for k = 0…n−1; the right rule takes xₖ* = a + kΔx for k = 1…n; the midpoint rule takes xₖ* = a + (k + ½)Δx. The trapezoid rule is not a Riemann sum at all — it averages the two edge heights — but it is the natural comparison because it equals the average of the left and right sums.
On a function that is increasing throughout the interval, the left rule samples the lowest point of each subinterval and therefore underestimates, while the right rule samples the highest and overestimates. Reverse both statements on a decreasing function. Those are the only two monotone cases; on a function that rises and falls the two errors partly cancel and no such rule holds.
The midpoint rule is different in kind. Its error term is −(b − a)h²f''(ξ)/24 against the trapezoid rule's +(b − a)h²f''(ξ)/12, where h = Δx. Two things follow. First, both are second order, so halving Δx divides the error by about four, while the left and right rules are only first order and halve it. Second, the midpoint error is half the size of the trapezoid error and carries the opposite sign — so for a function with a consistent concavity, the midpoint rule is the better of the two and the true value lies between them.
The geometric reason the midpoint rule does so well is worth seeing. Its rectangle has the same area as the trapezoid formed by the tangent line at the midpoint, and the tangent line matches the curve to first order. The rectangle overshoots on one half of the subinterval by nearly exactly what it undershoots on the other, so the linear part of the error cancels and only the curvature term survives.
Worked example: f(x) = x² on [0, 1] with n = 4
Here Δx = (1 − 0)/4 = 0.25 and the grid is 0, 0.25, 0.5, 0.75, 1. The exact integral is [x³/3]₀¹ = 1/3 ≈ 0.333333.
- Left sum. Heights at 0, 0.25, 0.5, 0.75 are 0, 0.0625, 0.25, 0.5625. They add to 0.875, and 0.875 × 0.25 = 0.21875. The error is 0.333333 − 0.21875 = 0.114583, an underestimate as expected for an increasing function.
- Right sum. Heights at 0.25, 0.5, 0.75, 1 are 0.0625, 0.25, 0.5625, 1, adding to 1.875, and 1.875 × 0.25 = 0.46875. The error is 0.135417, an overestimate.
- Check the relationship. Right minus left is 0.46875 − 0.21875 = 0.25, which must equal [f(b) − f(a)]Δx = (1 − 0)(0.25) = 0.25. Every interior height appears in both sums, so only the two ends survive the subtraction — a fast way to get one sum from the other.
- Midpoint sum. Sample points are 0.125, 0.375, 0.625, 0.875, with heights 0.015625, 0.140625, 0.390625, 0.765625. They add to 1.3125, and 1.3125 × 0.25 = 0.328125. The error is only 0.005208 — about 22 times smaller than the left sum's error from the same four evaluations.
- Trapezoid. The average of the left and right sums is (0.21875 + 0.46875)/2 = 0.34375, with error 0.010417 — exactly twice the midpoint error, and of opposite sign, as the error formulas predict.
Doubling to n = 8 gives a left sum of 0.2734375, with error 0.059896. Compared with 0.114583 at n = 4, that is a factor of 1.91 — heading towards the factor of 2 that first-order convergence promises.
How to read the error and the convergence rate
Read the absolute error first, then the ratio between it and the error at double n. That ratio is the practical signature of the rule: near 2 for left and right sums, near 4 for midpoint and trapezoid, on any function with a continuous second derivative. If your ratio is far from the expected value, something in the problem violates the assumptions — a kink, an infinite derivative at an endpoint, or a wildly oscillatory integrand sampled too coarsely.
A ratio much better than expected is a real phenomenon, not a bug. Integrating a periodic function over an exact whole number of periods makes the trapezoid rule converge spectacularly fast, because the endpoint error terms cancel identically. Symmetry can also make an error vanish outright: the midpoint rule is exact for any straight line, and the trapezoid rule is too.
Relative error is the figure to quote when the integral is large; absolute error is the one that matters when the integral is near zero, where relative error is meaningless or undefined. The calculator returns relative error only when the reference integral is non-zero, which is why that field is blank for a function that integrates to zero over a symmetric interval.
Finally, remember that increasing n is not free and not unboundedly useful. Each doubling doubles the function evaluations, and once the accumulated round-off in summing n terms is comparable with the truncation error, more rectangles stop helping. For a smooth integrand, moving to a higher-order rule — Simpson, Gauss–Legendre — buys far more than more rectangles of a low-order rule.
Reference: approximating ∫₀¹ x² dx = 1/3
| n | Left | Right | Midpoint | Trapezoid |
|---|---|---|---|---|
| 1 | 0 | 1 | 0.25 | 0.5 |
| 2 | 0.125 | 0.625 | 0.3125 | 0.375 |
| 4 | 0.21875 | 0.46875 | 0.328125 | 0.34375 |
| 8 | 0.2734375 | 0.3984375 | 0.33203125 | 0.3359375 |
| 16 | 0.302734375 | 0.365234375 | 0.3330078125 | 0.333984375 |
| 32 | 0.3178710938 | 0.3491210938 | 0.3332519531 | 0.3334960938 |
Left and right errors halve per row; midpoint and trapezoid errors quarter per row. The midpoint error is always half the trapezoid error and on the opposite side of the true value, because x² has constant positive curvature.
Pitfalls and limits of this method
- Confusing signed area with geometric area. Below the axis the contributions are negative. Integrate
abs(f(x))if you want total area, and expect slower convergence because the absolute value introduces a kink. - Sampling a singularity. A left sum of 1/x starting at a = 0 evaluates f(0) and fails. The midpoint rule avoids both endpoints and is the standard fix for an integrable endpoint singularity.
- Assuming left always underestimates. That holds only where f is increasing. On a function that turns inside the interval, neither the left nor the right sum is a reliable bound.
- Trusting a small error at small n. Errors can cancel by accident. Convergence is a property of the sequence, so check that the ratio between successive errors matches the rule's order before you believe a single result.
- Using equal subintervals on a peaked integrand. A function with a narrow spike needs its rectangles concentrated there; a uniform grid either misses the spike or wastes thousands of evaluations on flat regions. Adaptive quadrature exists for this.
- Forgetting that this calculator uses uniform Δx. Riemann's definition allows unequal widths, provided the largest width tends to zero. Non-uniform partitions matter in theory and in adaptive methods, but not here.
How the reference value is obtained
The "exact integral" shown is composite Simpson's rule on 2,000 panels, not a symbolic antiderivative. For a smooth integrand its error is of order h⁴ with h = (b − a)/2000, which is well below the precision displayed. It is not exact for a function with a singularity or a discontinuous derivative on the interval, and in that case the error figures should be read as indicative. If you need the length of the curve rather than the area under it, use the arc length calculator, which integrates √(1 + f'²) by the same quadrature machinery.
Where Riemann sums lead
Simpson's rule is the next step up and costs nothing extra in evaluations: fit a parabola through each consecutive triple of grid points instead of a line through each pair, and the order jumps from two to four. Combining the trapezoid rule with Richardson extrapolation gives Romberg integration, which reaches machine precision on smooth integrands in a few dozen evaluations. Gauss–Legendre quadrature abandons the uniform grid entirely and places nodes where they do the most good.
Applications of the definite integral all reduce to setting up the right integrand and then approximating it. The volume of a solid of revolution is π∫R² dx, which the disk and washer calculator evaluates; arc length is ∫√(1 + f'²) dx. Both need the same convergence judgement you are making here, and both inherit any roughness in f.
The derivative side of the same coin is the numerical derivative calculator. Compare their behaviour: integration is stable and forgiving because averaging suppresses noise, while differentiation amplifies it by 1/h. That asymmetry is why you can integrate noisy data usefully but should never differentiate it without smoothing first.
If your integrand is a limit rather than a function value — an integrand of the form 0/0 at an endpoint, say — resolve the limit first with the L'Hôpital's rule calculator and integrate the resulting continuous extension. And when the region you want is bounded by a curve you must locate first, find the intersection with the bisection method calculator before choosing the limits.
