Riemann Sums, Explained

After reading this you can build left, right, midpoint and trapezoid sums by hand, predict which one is most accurate for a given function, and read how fast each rule closes in on the true integral.

What a Riemann sum does

A definite integral \int_a^b f(x)\,dx is the signed area between the curve and the x-axis. A Riemann sum approximates that area by slicing [a,b] into n strips and replacing each strip with a rectangle or trapezoid whose area you can compute exactly.

Take f(x)=x^2 on [0,2]. The exact area is \int_0^2 x^2\,dx = 8/3 \approx 2.6667. With just n=10 strips, the midpoint rule gives 2.66, off by 0.0067. The trapezoid rule gives 2.68, off by 0.0133. The left sum gives 2.28 and the right sum gives 3.08, both off by more than 0.38. Same strips, very different accuracy. The rest of this article explains why.

The four rules and their formulas

Split [a,b] into n equal strips of width h = (b-a)/n. Label the grid points x_i = a + i\,h for i = 0, 1, \dots, n. Each rule chooses a different height for the rectangle on strip i.

L_n = h\sum_{i=0}^{n-1} f(x_i)

The left sum uses the value at the left edge of each strip. If f is increasing, every rectangle sits below the curve, so L_n underestimates the area.

R_n = h\sum_{i=1}^{n} f(x_i)

The right sum uses the right edge. For an increasing function every rectangle pokes above the curve, so R_n overestimates. Notice R_n - L_n = h\,(f(b) - f(a)), the reason left and right straddle the truth.

M_n = h\sum_{i=0}^{n-1} f\!\left(\tfrac{x_i + x_{i+1}}{2}\right)

The midpoint sum takes the height at the center of each strip. The rectangle overshoots on one side of the midpoint and undershoots on the other, and those errors partly cancel.

T_n = \tfrac{h}{2}\left(f(x_0) + 2\sum_{i=1}^{n-1} f(x_i) + f(x_n)\right)

The trapezoid sum connects consecutive points with straight lines instead of flat tops. It equals the average of the left and right sums, T_n = (L_n + R_n)/2.

Midpoint and trapezoid both use straight-line pieces, yet midpoint is usually about twice as accurate. For a curve that bends one way (constant sign of the second derivative), midpoint errs on one side and trapezoid errs on the other, so a weighted blend of the two, (2M_n + T_n)/3, is Simpson's rule.

How fast each rule converges

Convergence rate is the exponent p in the error size, roughly proportional to h^p. Halving h (doubling n) shrinks the error by a factor of 2^p.

Error behavior on a smooth function
RuleError orderError factor when n doubles
LeftO(h)2
RightO(h)2
MidpointO(h^2)4
TrapezoidO(h^2)4

So the left and right rules are first order: double the work, halve the error. Midpoint and trapezoid are second order: double the work, cut the error to a quarter. For f(x)=x^2 on [0,2] the midpoint error at n=10 is 0.006667. At n=20 it drops to 0.001667, exactly one quarter, matching the second-order prediction.

Worked example: x squared on the interval from 0 to 2

Reproducing the demo

Use the demo values expr = x^2, a = 0, b = 2, n = 10. Then h = 2/10 = 0.2 and the grid is 0, 0.2, 0.4, ... , 2.0.

  1. Left sum: add f at 0, 0.2, ..., 1.8. The squares sum to 11.4, so L_{10} = 0.2 \times 11.4 = 2.28.
  2. Right sum: add f at 0.2, 0.4, ..., 2.0. The squares sum to 15.4, so R_{10} = 0.2 \times 15.4 = 3.08.
  3. Trapezoid: average the two, T_{10} = (2.28 + 3.08)/2 = 2.68.
  4. Midpoint: use 0.1, 0.3, ..., 1.9. Their squares sum to 13.3, so M_{10} = 0.2 \times 13.3 = 2.66.
  5. Compare to the exact value 8/3 = 2.6667. Errors: left -0.3867, right +0.4133, trapezoid +0.0133, midpoint -0.0067.

The midpoint error is about half the trapezoid error and has the opposite sign, exactly as the geometry predicts for a curve that is concave up everywhere.

Left and right errors dwarf the trapezoid and midpoint errors. The midpoint bar is roughly half the trapezoid bar.

Watching the rectangles converge

For f(x)=x^2 on [0,2], raising n from 4 to 8 cuts the midpoint error from about 0.0417 to 0.0104 (a factor of 4), while the left error only drops from about 0.75 to 0.4375 (a factor near 2).

When to use each rule, and when not to

Reach for the midpoint or trapezoid rule whenever f is smooth, because their second-order accuracy pays off fast. Use the left or right sum mainly to teach the definition or to bracket the answer: for a monotone function, one is a guaranteed underestimate and the other a guaranteed overestimate, so the truth lies between them.

Riemann sums with equal strips struggle in three situations. If f has a sharp peak or kink, uniform strips waste effort in flat regions. If f has a vertical asymptote near an endpoint, as with 1/\sqrt{x} at 0, the sampled height blows up and the sum is unreliable. If the interval is infinite, no finite n covers it.

When accuracy matters more than seeing the rectangles, switch to the Numerical Integrator, which places sample points adaptively where the function bends most. For an exact symbolic answer, use the Derivative & Integral Calculator.

Reading and interpreting the results

The calculator reports all four sums plus a reference value from adaptive quadrature. Compare the signs first. If left is below and right is above the reference, your function is increasing across the interval; if the pattern flips, it is decreasing. When left and right sit close together, f is nearly flat and every rule agrees.

Next compare magnitudes. Expect midpoint and trapezoid errors to be far smaller than left and right. If they are not, your function probably has a kink or a spike inside [a,b], and the smooth-function error theory no longer applies. Finally, double n and watch the errors. A midpoint error that falls by a factor near 4 confirms smooth behavior; an error that barely moves warns of a singularity.

Common mistakes

The most frequent error is an off-by-one in the sum index. The left sum runs i = 0 to n-1 and excludes f(b). The right sum runs i = 1 to n and excludes f(a). Including both endpoints in a one-sided sum inflates the answer by one extra strip height.

Two more traps. First, the trapezoid interior weights are all 2, but the two endpoint weights are 1; forgetting the factor of h/2 out front doubles the result. Second, the midpoint rule samples at strip centers a + (i + 0.5)h, not at grid points, so it uses n new sample points that share nothing with the left or right grids.

One subtle point about signs: these sums return signed area. On \int_0^{2\pi} \sin x\,dx the exact value is 0, and every rule returns a number near 0 even though the graph clearly encloses area. That is correct, because the area above and below the axis cancels.

Related tools

Once you understand approximation error, these calculators extend the idea. The Taylor & Maclaurin Series tool shows the same trade of accuracy for terms in a different setting. The Function Analyzer & Grapher finds where a function bends, which tells you where uniform strips lose accuracy. For fitting a curve through sampled points rather than integrating one, see the Interpolation Calculator. If your integrand comes from a differential equation, solve it first with the Differential Equation Solver. To check limiting behavior at an endpoint before you integrate, use the Limit Calculator, and for partial-derivative work see the Gradient & Hessian Calculator.

Frequently asked questions

Which Riemann sum is most accurate?

For smooth functions the midpoint rule is usually best, with error about half the trapezoid error and an eighth or less of the left or right error. On x^2 over [0,2] at n=10, midpoint errs by 0.0067 versus 0.0133 for trapezoid and 0.39 for the one-sided sums.

Why do left and right sums bracket the true value?

Only when f is monotone. If f increases, the left height is the smallest value on each strip and the right height is the largest, so L_n \le \int_a^b f \le R_n. If f is not monotone, neither is guaranteed to bracket.

How large should n be?

Pick n so that doubling it changes the answer less than your tolerance. With a second-order rule, an answer stable to 0.001 at n=20 is typically accurate to about 0.0003, since the next doubling cuts the error by four.

What is the difference from Simpson's rule?

Simpson's rule fits parabolas instead of lines and equals the blend (2M_n + T_n)/3. It is fourth order, so doubling n cuts its error by about 16, far faster than the second-order midpoint and trapezoid rules.

Can a Riemann sum be exact?

Yes, in special cases. The trapezoid rule is exact for any straight line, and the midpoint rule is exact for any straight line as well. For a genuine curve like x^2, a finite sum always carries some error, though Simpson's rule is exact for cubics.