III.12 · Calculus
How do thin pieces add up to a total?
Slice an interval into panels, multiply each height by its width, and add. Refine the slices and the sum settles on the integral — signed accumulation, which is not the same as area.
Accumulate signed area panel by panel
left endpoints under f(x) = 0.25x³ − x
Graph of f(x) = 0.25x³ − x from a = −2 to b = 3 with 10 panels using left endpoints. Green panels add, gold panels subtract. The sum is 0.703125; the integral is 1.5625.
- Lower limit
- −2
- Upper limit
- 3
- Panels
- 10
- Panel width (signed)
- 0.5
- Sum with left endpoints
- 0.703125
- Integral (signed accumulation)
- 1.5625
- Error of the sum
- −0.859375
- Geometric area between graph and axis
- 3.5625
- Area above the axis
- 2.5625
- Signed area below the axis
- −1
Exact values use =; binary64 approximations use ≈. Undefined values state their reason.
Exceptional case: The graph crosses the axis: the integral 1.5625 is SIGNED accumulation (above minus below), while the geometric area is 3.5625. The sum 0.703125 approximates the signed value.
| n | Δx | sum | sum − integral |
|---|---|---|---|
| 1 | 5 | 0 | −1.5625 |
| 2 | 2.5 | −1.171875 | −2.734375 |
| 4 | 1.25 | −0.29296875 | −1.85546875 |
| 8 | 0.625 | 0.5126953125 | −1.0498046875 |
| 16 | 0.3125 | 1.007080078125 | −0.555419921875 |
| 32 | 0.15625 | 1.27716064453125 | −0.28533935546875 |
| 64 | 0.078125 | 1.4179229736328125 | −0.1445770263671875 |
Panel 1: [−2, −1.5]
The sampled height times the signed width Δx gives the contribution 0; a negative height or a negative Δx makes it negative.
from −3 to 3, up to 2 decimal places
from −3 to 3, up to 2 decimal places
from 1 to 64, whole numbers only
The opening function is positive on part of and negative on another part. With ten left-endpoint panels the sum is , while the integral is . The geometric area between the graph and the axis is larger still, : the integral subtracts what lies below the axis. Increase and watch the table: the sums approach .
Intuition: rate times time, piece by piece
If a speed were constant, distance would be speed × time. When it changes, chop time into short intervals on which it is nearly constant, use one sampled speed per interval, and add the products. A negative speed (moving backward) subtracts distance. The total is a signed accumulation; refining the chopping makes it exact in the limit.
Definition
Partition into panels of width with points . A Riemann sum chooses a sample in each panel:
The left, right and midpoint sums use , or the centre; the trapezoid sum averages the two end heights. For continuous , all of them converge to the same limit, the definite integral
By the same formula, reversing the limits reverses the sign of : , and .
Mechanics: what the explorer computes
For the polynomial integrands every sum is computed exactly with rational arithmetic, and the integral comes from an exact antiderivative; their difference is the exact error. The positive part, the negative part and the geometric area are obtained by splitting at the exact rational roots. For the values are binary64 and marked . Each drawn panel is the panel used in the sum, with the same height and width.
Derivation: signed area and geometric area
Split at the points where changes sign. On a subinterval where , the integral is the area under the graph; where , it is minus the area above the graph. Hence
For on the roots are , and : the area above is , the area below is , so the integral is and the geometric area .
Worked example
on with left panels: , samples . The exact sum is . The right sum is , the midpoint sum and the trapezoid sum . With the midpoint and trapezoid sums are within of , while the left and right sums are still off by about : the sample point matters for the rate.
Replay the worked derivation
Panel 1: [−2, −1.5]
The sampled height times the signed width Δx gives the contribution 0; a negative height or a negative Δx makes it negative.
Exceptional cases
- Exact cancellation. For on the integral is exactly although the area is .
- Reversed limits. With , every is negative and the sum changes sign.
- Non-integrable behaviour. A function with a pole inside the interval, such as on , has no Riemann integral at all; sample points can land arbitrarily close to the pole and the sums do not settle. The explorer’s integrands are bounded on the whole window.
Executable reference code
The reference computes exact left, right, midpoint and trapezoid sums with Fraction and compares them with the antiderivative. The NumPy version vectorises the sums for increasing and shows the errors shrinking at the expected rates.
"""Exact Riemann and trapezoid sums with Fractions; signed integral versus geometric area (III.12)."""
from fractions import Fraction as F
f = lambda x: x**3 / 4 - x
antiderivative = lambda x: x**4 / 16 - x**2 / 2
def riemann(f, a, b, n, rule):
dx = (b - a) / n
xs = [a + i * dx for i in range(n + 1)]
if rule == "left":
return sum(f(x) for x in xs[:-1]) * dx
if rule == "right":
return sum(f(x) for x in xs[1:]) * dx
if rule == "midpoint":
return sum(f((x0 + x1) / 2) for x0, x1 in zip(xs, xs[1:])) * dx
return sum((f(x0) + f(x1)) / 2 for x0, x1 in zip(xs, xs[1:])) * dx # trapezoid
a, b = F(-2), F(3)
exact = antiderivative(b) - antiderivative(a)
assert exact == F(25, 16)
assert [riemann(f, a, b, 10, r) for r in ("left", "right", "midpoint", "trapezoid")] == [F(45, 64), F(165, 64), F(195, 128), F(105, 64)]
# Geometric area: split at the roots -2, 0, 2 and add absolute values.
pieces = [(F(-2), F(0)), (F(0), F(2)), (F(2), F(3))]
area = sum(abs(antiderivative(q) - antiderivative(p)) for p, q in pieces)
assert area == F(57, 16) and area != exact
# Reversing the limits reverses the sign (negative dx).
assert riemann(f, b, a, 10, "left") == -riemann(f, a, b, 10, "right")"""Vectorised Riemann sums: left/right errors shrink like 1/n, midpoint/trapezoid like 1/n**2 (III.12)."""
import numpy as np
f = lambda x: x**3 / 4 - x
a, b, exact = -2.0, 3.0, 25 / 16
errors = {}
for n in (16, 32, 64, 128):
x = np.linspace(a, b, n + 1)
dx = (b - a) / n
errors[n] = {
"left": abs(np.sum(f(x[:-1])) * dx - exact),
"midpoint": abs(np.sum(f((x[:-1] + x[1:]) / 2)) * dx - exact),
"trapezoid": abs(np.trapezoid(f(x), x) - exact),
}
assert 1.8 < errors[64]["left"] / errors[128]["left"] < 2.2
assert 3.8 < errors[64]["trapezoid"] / errors[128]["trapezoid"] < 4.2
assert 3.8 < errors[64]["midpoint"] / errors[128]["midpoint"] < 4.2Explore
On Positive: 4 − x², the integral and the area agree. Switch rules on the same : left and right sums bracket the integral when is monotone on each panel. Drag past and read the sign change. On sin x, compare midpoint and trapezoid errors.
Common mistakes
Treating the integral as area when the graph dips below the axis. Forgetting that is negative when . Using panels but heights in a left or right sum (the trapezoid uses points, weighted).
Numerical considerations
Floating-point sums of many small terms accumulate rounding error; summing in exact arithmetic avoids it, and in binary64 the order of summation (or compensated summation) matters for very large . Riemann sums converge slowly — the left and right rules have error proportional to — which is why III.15 studies better rules.
Engineering and ML connections
Integrals compute totals from rates: energy from power, distance from velocity, probability from a density (Part IV), expected values as weighted sums. Discretised integrals appear in every simulation and in numerical estimates of areas under ROC curves.
Exercises
Summary and checkpoints
A Riemann sum multiplies sampled heights by panel widths and adds; its limit is the definite integral, a signed accumulation. Area above the axis adds, area below subtracts; reversing the limits reverses the sign. Next, III.13 lets the upper limit move and discovers that the rate of accumulation is the integrand itself.