Calculator · Integrals

Riemann sum calculator

Type a function, an interval and a number of rectangles. The calculator draws every rectangle, lists every sample point, and tells you how far the sum is from the true area.

6 min read · 3 interactive figures

Four left rectangles under x² on [0, 2] give 1.75; the true area is 8/3 ≈ 2.6667. Raise n, change the rule, or type your own function.

A Riemann sum adds up the areas of n rectangles standing on equal pieces of [a, b], each as tall as the function at one chosen point of its piece. The calculator above computes it for any function you type, draws the rectangles, and compares the sum with the true area.

This page is a working tool first and an explanation second. It shows how to compute each kind of sum by hand, checks a worked example against the calculator, and explains how large n must be for a given accuracy. Why Riemann sums define the integral at all, and how Archimedes did the same thing for a parabola, is the subject of the Riemann sums chapter.

The formulas

Split [a, b] into n equal pieces of width Δx = (b − a)/n, with endpoints xi = a + iΔx for i = 0, 1, …, n. The three standard Riemann sums differ only in where each rectangle takes its height[1]:

Ln = Δx n∑i=1 f(xi−1),Rn = Δx n∑i=1 f(xi),Mn = Δx n∑i=1 f(xi−1 + xi2)(1)

The left sum uses the left end of each piece, the right sum the right end, and the midpoint sum the middle. The trapezoidal rule, which the calculator also offers, is the average of the left and right sums; it has its own page, the Trapezoidal rule calculator. Where the function is negative, a rectangle counts as negative area, so the sum measures signed area: area above the axis minus area below.

A worked example by hand

Take f(x) = x² on [0, 2] with n = 4, the calculator's starting example. Then Δx = 0.5 and the endpoints are 0, 0.5, 1, 1.5, 2, where x² is 0, 0.25, 1, 2.25 and 4.

  • Left sum: 0.5 × (0 + 0.25 + 1 + 2.25) = 1.75.
  • Right sum: 0.5 × (0.25 + 1 + 2.25 + 4) = 3.75.
  • Midpoint sum: the midpoints 0.25, 0.75, 1.25, 1.75 give 0.0625, 0.5625, 1.5625, 3.0625, so 0.5 × 5.25 = 2.625.
  • Trapezoid: (1.75 + 3.75)/2 = 2.75.

The exact area is ∫02 x² dx = 8/3 ≈ 2.6667. The left sum is too small and the right sum too big, as they must be for a rising function: every left rectangle sits under the curve and every right one pokes above it. The midpoint sum is off by only 0.042, and the trapezoid by 0.083, twice as much in the other direction. Open "Show the work" under the calculator to see the same four rows.

Question 1 Predict before you raise n in the calculator below.#
Figure 1 The calculator. Type f(x), set a, b and n, and choose the rule. Magenta dots mark the sample points; outlines disappear when the rectangles are too thin to see, so a large n reads as one shaded area. The result line gives the sum, the step Δx, a reference value of the integral from an adaptive method, the error, and Simpson’s rule for comparison.#

How many rectangles do you need?

For a function with a continuous derivative, the left and right sums have errors that shrink like 1/n: double n and the error halves. The midpoint and trapezoid errors shrink like 1/n² when f has a continuous second derivative: double n and the error falls by four[2]. In practice that difference is enormous. For x² on [0, 2], n = 100 leaves the left sum about 0.040 too small, but the midpoint sum only 0.000067 too small. To get six correct decimals from a left sum you need millions of rectangles; the midpoint sum needs about a thousand. Simpson’s rule, which errs like 1/n⁴, is exact for x² (it fits parabolas) and needs only a few dozen pieces for a typical smooth function. The figure below shows those rates on log–log axes, where each one is a straight line.

Figure 2 Error against the number of pieces n, both on logarithmic scales. Each rule is a straight line whose slope is its order: −1 for left and right, −2 for midpoint and trapezoid, −4 for Simpson. Drag n to read off the errors at any point.#

Where the "true area" comes from

The calculator needs a reference value to report an error, and for a typed function it cannot look one up. It uses adaptive Simpson quadrature[3]: it fits parabolas to small panels, compares one parabola with two half-panel parabolas, keeps splitting whichever panel disagrees most, and stops when the total disagreement is below a tight tolerance. Before it trusts any panel it also checks 4,096 scattered samples of f against the panel’s parabolas, so a spike or a wiggle that falls between a panel’s own points is caught. That gives about ten correct digits for well-behaved functions, which is plenty for judging a Riemann sum. The Riemann sums section of the Riemann sums chapter has a calculator built around that method alone.

When the sum means nothing

Type tan(x) on [−2, 2]. The function has vertical asymptotes at ±π/2, inside the interval, so the area is infinite on both sides of each one and the integral does not exist. A Riemann sum still produces a number, because it only samples finitely many points: the midpoint sum with n = 1,000 is about 10−12, since the samples are placed symmetrically and the huge positive and negative values cancel. That number is meaningless, and raising n will not make it settle. The calculator says so. Where its reference integral keeps splitting panels toward one point, it looks at how |f| grows beside that point. When the formula has a pole there of order 1 or more, as tan x has at π/2, and the measured growth agrees, the area is infinite, and it reports that the integral diverges instead of printing 0. Growth too close to 1/|x − c| to call, such as |x|−0.99 (finite area, 100 on [0, 1]), gets no answer rather than a guess. The same trap catches 1/x on [−1, 1]: with n = 6 the midpoint sum is 0 to every digit shown, because the samples pair off symmetrically, but the integral diverges and the calculator says so. If a sample point lands exactly on a pole, as with an even n for the right sum, the sum itself is undefined. Slower growth is different: 1/√|x| on [−1, 2] is unbounded at 0, yet its area is finite (2 + 2√2 ≈ 4.83), and the calculator reports it as an improper integral that converges.

A Riemann sum is defined with any sample point in each piece, not only the ends or the middle, and with pieces of unequal widths; the integral is the limit as the widest piece shrinks to zero, when that limit exists[4]. Equal widths and the three standard sample points are what courses compute, and they are what this calculator does.