Integrals: adding up

If the derivative breaks change into tiny pieces, the integral adds the pieces back up.

⏱ 18 minNeeds: Chapters 1–23 simulations
In plain words

A tap fills a tank at a rate that changes during the day. How much water is in the tank by evening? Chop the day into short intervals, multiply rate × time for each, and add. Make the intervals shorter and the answer gets more accurate. The limit of that sum is the integral.

You will learn
Level 1 · basicArea by rectangles; the fundamental theorem Level 2 · standardQuadrature rules and their accuracy Level 3 · advancedIntegrals in many dimensions; Monte Carlo; losses as integrals
Simplest example first · a constant function is a rectangle

Take the constant function $f(x) = 2$ on the interval $[1, 4]$. Its graph is a flat line at height 2, and the region under it is a rectangle of width $4 - 1 = 3$ and height $2$, so its area is $2 \times 3 = 6$. The integral gives exactly the same number: $$\int_1^4 2\,dx = \big[2x\big]_1^4 = 8 - 2 = 6 = \text{height} \times \text{width}.$$ That is why we say "the integral is the area under the curve". For a constant $c$ on $[a, b]$: $\int_a^b c\,dx = c\,(b - a)$. Next, try $f(x) = x$ on $[0, b]$: the region is a triangle, area $\tfrac12 \times b \times b$, and indeed $\int_0^b x\,dx = \tfrac{b^2}{2}$. For curved graphs there is no simple shape, so we cut the region into thin rectangles (Section 1) — and the integral is what those rectangles add up to.

Set a negative height: the rectangle lies below the axis and the integral is negative — integrals count area below the axis with a minus sign.

1. Area by rectangles

Animation: more and thinner rectangles under a curve; their total area approaches the exact integral.
Watch: More and thinner rectangles: the sum approaches the exact area, and the error falls quickly.

Split $[a,b]$ into $n$ pieces of width $\Delta x = (b-a)/n$ with points $x_i = a + i\,\Delta x$. On each piece build a rectangle whose height is the function value at some point $x_i^*$ of that piece. The total area of the rectangles is a Riemann sum: $$S_n = \sum_{i=1}^{n} f(x_i^*)\,\Delta x.$$

Definition · Definite integral

If the Riemann sums approach the same number for every choice of the points $x_i^*$ as $n \to \infty$, that number is the definite integral $$\int_a^b f(x)\,dx = \lim_{n\to\infty} \sum_{i=1}^{n} f(x_i^*)\,\Delta x.$$ Every continuous function on $[a,b]$ is integrable. Area below the $x$-axis counts as negative.

Stewart, Calculus: Early Transcendentals, 8th ed., Cengage (2016), chapter "Integrals".
Riemann sums and the trapezoid ruleadd more strips
The shaded area is the approximation $S_n$. Compare the three rules at the same $n$: midpoint and trapezoid are much better than left rectangles.

2. The fundamental theorem of calculus

Computing limits of sums by hand is painful. Luckily there is a shortcut, discovered by Newton and Leibniz: integration undoes differentiation.

Theorem · Fundamental theorem of calculus

Part 1. If $f$ is continuous on $[a,b]$, then $F(x) = \int_a^x f(t)\,dt$ is differentiable and $F'(x) = f(x)$.

Part 2. If $F$ is any antiderivative of $f$ (that is, $F' = f$), then $$\int_a^b f(x)\,dx = F(b) - F(a).$$

Stewart (2016), chapter "Integrals", section on the Fundamental Theorem of Calculus; Apostol, Calculus Vol. 1, 2nd ed., Wiley (1967).
Example

$\displaystyle\int_0^{\pi} \sin x\,dx = \big[-\cos x\big]_0^{\pi} = -\cos\pi + \cos 0 = 2.$ Exactly 2 — no rectangles needed.

Real world

If $q(t)$ is the production rate of an oil well (barrels per day), the cumulative production up to day $T$ is $\int_0^T q(t)\,dt$. Distance is the integral of speed; electric charge is the integral of current; the mass of a rod with density $\rho(x)$ is $\int_0^L \rho(x)\,dx$. Probability of an event is an integral of a probability density.

3. Integrals on a computer: quadrature

Most integrals in science have no formula for $F$ (try $\int e^{-x^2}dx$). Computers use quadrature rules: weighted sums of function values. With $h = (b-a)/n$ and $x_i = a + ih$:

RuleFormulaError
Midpoint$h\sum_{i=1}^n f\big(\tfrac{x_{i-1}+x_i}{2}\big)$$O(h^2)$
Trapezoid$h\big[\tfrac12 f(x_0) + f(x_1) + \dots + f(x_{n-1}) + \tfrac12 f(x_n)\big]$$O(h^2)$
Simpson ($n$ even)$\tfrac{h}{3}\big[f(x_0) + 4f(x_1) + 2f(x_2) + 4f(x_3) + \dots + f(x_n)\big]$$O(h^4)$
Monte Carlo$(b-a)\cdot\frac1N\sum_{k=1}^{N} f(X_k)$, $X_k$ random$O(N^{-1/2})$ on average
Theorem · Error of the composite trapezoid rule

If $f''$ is continuous on $[a,b]$, the composite trapezoid rule $T_n$ satisfies $$\int_a^b f(x)\,dx - T_n = -\frac{(b-a)\,h^2}{12} f''(\mu) \quad\text{for some } \mu \in (a,b).$$ For Simpson's rule the error is $-\frac{(b-a)h^4}{180} f^{(4)}(\mu)$.

Burden, Faires & Burden, Numerical Analysis, 10th ed., Cengage (2016), chapter 4 "Numerical Differentiation and Integration" (composite numerical integration).
How fast does each rule converge?error of ∫₀³ (1 + sin 2x) dx
left rectangles O(h)trapezoid O(h²)Simpson O(h⁴)Monte Carlo ≈ O(N^−½)
On a log-log plot, error $\propto n^{-p}$ is a straight line of slope $-p$. The steeper, the better. Monte Carlo is the slowest here — but its rate does not depend on the dimension, which is why it wins for integrals over many variables (and why PINNs sample random points).

4. Integrals in several variables

The integral of $f(x,y)$ over a region $\Omega$, written $\iint_\Omega f\,dA$, adds up $f$ times tiny areas — e.g. the total heat in a plate with temperature $u(x,y)$. For a box, it can be computed one variable at a time (Fubini's theorem): $$\int_0^1\!\!\int_0^1 xy^2\,dy\,dx = \int_0^1 x\cdot\tfrac13\,dx = \tfrac16.$$ Grid-based rules need $n^d$ points in $d$ dimensions — for $d = 10$ and $n = 10$ that is $10^{10}$ evaluations. This "curse of dimensionality" is a key reason for random (Monte Carlo) sampling in high dimensions.

Stewart (2016), chapter "Multiple Integrals"; Robert & Casella, Monte Carlo Statistical Methods, 2nd ed., Springer (2004).
Monte Carlo estimate of πthrow random darts at a square
The quarter circle has area $\pi/4 = \int_0^1\!\int_0^1 \mathbf 1[x^2+y^2\le 1]\,dy\,dx$. The fraction of darts inside estimates it. The error shrinks like $1/\sqrt N$: 100 times more darts give one more correct digit.
In SciML · The PINN loss is an integral

A PINN wants the equation residual $r(x)$ (how badly the equation is violated at $x$) to be zero everywhere in a domain $\Omega$. The natural measure is the integral $\int_\Omega r(x)^2\,dx$. In practice it is replaced by an average over $N$ collocation points: $$\int_\Omega r(x)^2\,dx \;\approx\; \frac{|\Omega|}{N}\sum_{k=1}^{N} r(x_k)^2,$$ which is exactly a Monte Carlo (or, on a grid, a rectangle-rule) quadrature. The mean squared error you will see in Chapter 16 is this sum.

5. Try it in Python

import numpy as np

f = lambda x: 1 + np.sin(2 * x)
a, b = 0.0, 3.0
exact = 3 + (1 - np.cos(6)) / 2          # from the fundamental theorem

for n in [4, 8, 16, 32, 64]:
    x = np.linspace(a, b, n + 1); h = (b - a) / n
    trap = h * (f(x[0]) / 2 + f(x[1:-1]).sum() + f(x[-1]) / 2)
    simp = h / 3 * (f(x[0]) + 4 * f(x[1:-1:2]).sum() + 2 * f(x[2:-1:2]).sum() + f(x[-1]))
    print(f"n={n:3d}  trapezoid error={abs(trap-exact):.2e}   Simpson error={abs(simp-exact):.2e}")

rng = np.random.default_rng(0)
for N in [100, 10_000, 1_000_000]:
    X = rng.uniform(a, b, N)
    print(f"Monte Carlo N={N:>9,}  error={abs((b-a)*f(X).mean()-exact):.2e}")

Notice: doubling $n$ divides the trapezoid error by about 4 ($= 2^2$) and the Simpson error by about 16 ($= 2^4$) — exactly as the theorem predicts.

If $F'(x) = f(x)$, then $\int_2^5 f(x)\,dx$ equals…

Right — the fundamental theorem of calculus, Part 2.

A rule has error $O(h^4)$. You halve $h$. The error becomes about…

Right: $(1/2)^4 = 1/16$. That is Simpson's rule.

Key takeaways

Further reading