MathsCalculus

Riemann Sum
calculator.

Add up rectangles under a curve, and see how close they get to the true area.

How it's worked out

You split the interval from a to b into n strips of equal width, build a rectangle on each strip, and add the areas. OpenStax defines these sums with a strip width and a sample point in each strip. The only choice left is where you measure each height.

Δx\displaystyle \Delta x=b−an\displaystyle {}= \frac{b - a}{n}
xi\displaystyle x_i=a+iΔx\displaystyle {}= a + i\,\Delta x
Ln\displaystyle L_n=∑i=1nf(xi−1)Δx\displaystyle {}= \sum_{i=1}^{n} f(x_{i-1})\,\Delta x
Rn\displaystyle R_n=∑i=1nf(xi)Δx\displaystyle {}= \sum_{i=1}^{n} f(x_i)\,\Delta x

The left sum LnL_n takes each height at the start of a strip, and the right sum RnR_n takes it at the end. The midpoint sum takes it halfway across. The trapezoid sum joins the two ends of a strip with a straight line, and that works out to the average of the left and right sums.

Mn\displaystyle M_n=∑i=1nf(xi−1+xi2)Δx\displaystyle {}= \sum_{i=1}^{n} f\!\left(\frac{x_{i-1} + x_i}{2}\right)\Delta x
Tn\displaystyle T_n=Ln+Rn2\displaystyle {}= \frac{L_n + R_n}{2}

Where f is below the x-axis, a height is negative, so that strip subtracts from the total. A Riemann sum therefore estimates signed area, as the definite integral does. As n grows, the sums close in on the integral, which is how the integral is defined in the first place.

One more figure is worth having. Two thirds of the midpoint sum plus one third of the trapezoid sum is Simpson's rule with twice as many strips. For 1x\frac{1}{x} from 1 to 2 with n = 5, this mix gives 0.69315, while the integral is 0.693147.

Worked examples

Nadia checks her homework with four rectangles

Nadia has to estimate the area under x2x^2 from 0 to 1 with n = 4, so each strip is 0.25 wide. Her left sum uses the heights at 0, 0.25, 0.5, 0.75, and it comes to 0.21875. Her right sum uses the heights at 0.25, 0.5, 0.75, 1, and it comes to 0.46875.

Four sums for x squared from 0 to 1, n = 4
SumValueSize of error
Left0.218750.114583
Right0.468750.135417
Midpoint0.3281250.005208
Trapezoid0.343750.010417

The true area is 0.333333. It sits between her left and right sums, as it must, because x2x^2 only rises here. The midpoint sum misses by only 0.005208, even though it uses the same number of rectangles.

Open this example in the tool

Omar turns speed readings into a distance

Omar logs a test car every 5 seconds for 40 seconds. Its speed in meters per second follows 30(1−e−t/10)30\left(1 - e^{-t/10}\right), so type x in place of t. Speed times time is distance, so the area under this curve is the distance covered.

The speed only rises, so the left sum of 825.8 m is too low and the right sum of 973 m is too high. The curve bends downward, so each straight trapezoid top sits under it, and the trapezoid sum of 899.4 m is a little low. The integral is 905.5 m. Omar can report the trapezoid figure and know which way it errs.

Open this example in the tool

Rosa sees positive and negative strips cancel

Rosa tries x3−3xx^3 - 3x from −2 to 2 with n = 8. This curve has an odd shape: its value at −x is minus its value at x. The midpoint sum is 0 and the integral is 0, because each strip on one side cancels its partner on the other.

The left sum is −1 and the right sum is 1. Neither one measures the region's size. For the total area, integrate the absolute value of f, or use the area between curves calculator with 0 as the second curve.

Open this example in the tool

Reading your result

Which side of the integral. You can often predict it. A left sum on a rising curve takes the lowest height on each strip, so it runs low, and a right sum runs high. On a falling curve those swap. Bend matters for the other two: on a curve that bends upward, the trapezoid sum runs high and the midpoint sum runs low. These rules need the shape to hold across the whole interval.

Sums against the integral, n = 4
f(x), rangeShapeLeft, rightMidpoint, trapezoid
x2x^2, 0 to 2Rises, bends upUnder, OverUnder, Over
x\sqrt{x}, 1 to 4Rises, bends downUnder, OverOver, Under
e−xe^{-x}, 0 to 2Falls, bends upOver, UnderUnder, Over
cos⁡x\cos x, 0 to 1.5Falls, bends downOver, UnderOver, Under

How fast the error falls. For exe^x from 0 to 1, going from 32 strips to 64 cuts the left error 1.99 times, and it cuts the midpoint and trapezoid errors 4 and 4 times. So the left and right errors shrink in step with n, while the other two shrink with n squared. OpenStax gives the bounds for the second pair, with M the largest size of f′′f'' on the interval.

midpoint error\displaystyle \text{midpoint error}≤M(b−a)324n2\displaystyle {}\le \frac{M(b - a)^3}{24 n^2}
trapezoid error\displaystyle \text{trapezoid error}≤M(b−a)312n2\displaystyle {}\le \frac{M(b - a)^3}{12 n^2}

How many strips you need. For x2x^2 from 0 to 2, staying within 0.01 of the integral takes 400 strips with the left sum and 9 with the midpoint sum. The search assumes the error keeps falling as n grows, which is true of smooth curves once n is not tiny.

What the error means. The default case, x2x^2 from 0 to 2 with n = 8, has a left sum of 2.1875 against an integral of 2.666667, a relative error of 17.97%. The integral here comes from adaptive quadrature, not from your rectangles, so it is a fair yardstick.

Common mistakes

  • Counting one height too many. With n strips you have n + 1 dividing points. The left sum drops the last point and the right sum drops the first, so each adds exactly n heights.
  • Forgetting to multiply by the width. Adding the heights alone gives a number with the wrong size. Every height needs the factor Δx\Delta x, which you can take outside the sum.
  • Calling a signed sum the area. Strips below the axis subtract. A sum of 0 can sit on a large region, as with x3−3xx^3 - 3x from −2 to 2.
  • Applying the over and under rules to a mixed curve. On sin⁡x\sin x from 0 to π\pi, the curve rises and then falls, so the left and right sums come out equal. Split the interval where the shape changes.
  • Sampling at a point where f has no value. 1x\frac{1}{x} from −1 to 1 has no value at 0, and every n puts a strip edge or a midpoint there. The tool names the point and gives no sum, and the integral across 0 does not exist in any case.

Questions people ask

Write the width first, then number the points from a. The left sum runs i from 0 to n − 1 and uses f at a + i times the width. The right sum runs i from 1 to n with the same formula, which is the only change.

Sources