An integral is a sum over a continuum: the area under a curve, cut into thin strips and added up. In a program it is a loop with an accumulator. This chapter defines it, connects it to the derivative (they undo each other, the way a prefix sum and an adjacent difference do), gives the two hand techniques worth knowing, and ends with the numerical rules that compute it when no formula does. The book needs integrals for two things: the solutions of differential equations (chapter 6) and densities, the continuous form of a probability distribution (section 12.8). There an integral is a weighted average, the continuous twin of the softmax-weighted sum at the heart of attention.
To find the area under \(f\) between \(a\) and \(b\), cut \([a, b]\) into \(n\) strips of width \(h = (b - a)/n\). Over each strip, pretend \(f\) is constant and take a rectangle: height \(f\) at some point of the strip, width \(h\). Add the rectangles:
\[ \sum_{i=0}^{n-1} f(x_i^*) \, h, \qquad x_i^* \in [a + i h, \, a + (i+1) h]. \]This is a Riemann sum. Taking \(x_i^*\) at the left end of each strip gives the left sum, at the right end the right sum, in the middle the midpoint sum.
As \(n\) grows the rectangles hug the curve better. For \(f(x) = x^2\) on \([0, 1]\) the area is \(1/3\) (section 3.3 shows why), and the three sums give:
| \(n\) | left | right | midpoint | midpoint error |
|---|---|---|---|---|
| 10 | 0.2850000000 | 0.3850000000 | 0.3325000000 | \(-8.3 \times 10^{-4}\) |
| 100 | 0.3283500000 | 0.3383500000 | 0.3333250000 | \(-8.3 \times 10^{-6}\) |
| 1000 | 0.3328335000 | 0.3338335000 | 0.3333332500 | \(-8.3 \times 10^{-8}\) |
The left and right sums are off by about \(1/(2n)\) in opposite directions: their error is \(O(h)\). The midpoint sum is off by \(1/(12 n^2)\): ten times more strips, a hundred times less error, \(O(h^2)\). The triangles in the figure explain it: at the midpoint, what the rectangle gains on one side it loses on the other, to first order. It is the same cancellation that made the central difference better than the forward one (section 2.5).
Definition. The definite integral of \(f\) over \([a, b]\) is the limit of its Riemann sums as the strip width goes to 0,
\[ \int_a^b f(x) \, dx = \lim_{h \to 0} \sum_i f(x_i^*) \, h, \]when the limit exists and does not depend on the choice of the points \(x_i^*\). It exists for every continuous \(f\) on \([a, b]\) [spivak].
Read \(\int\) as a stretched S, for sum, and \(dx\) as the width \(h\) in the limit. The \(x\) is a loop variable: \(\int_a^b f(x)\,dx\) and \(\int_a^b f(t)\,dt\) are the same number. Where \(f\) is negative the rectangles have negative height, so the integral is a signed area: \(\int_0^{2\pi} \sin x \, dx = 0\).
Properties that follow from the sums, each the obvious fact about a loop:
The integral as an average. The mean of an array is its sum divided by its length. The mean of \(f\) over \([a, b]\) is its integral divided by the length:
\[ \bar{f} = \frac{1}{b - a} \int_a^b f(x) \, dx. \]The mean of \(\sin\) over \([0, \pi]\) is \(2/\pi \approx 0.637\). More generally, if \(w(x) \ge 0\) and \(\int w = 1\), then \(\int f(x)\, w(x)\, dx\) is the average of \(f\) weighted by \(w\): the continuous form of \(\sum_i \alpha_i v_i\) with \(\alpha_i \ge 0\), \(\sum_i \alpha_i = 1\). A \(w\) like that is a probability density, and the weighted average is an expectation (section 12.8). The monotone property above says such an average lies between the smallest and largest value of \(f\), exactly as a convex combination of numbers does (section 8.2).
Integrals and derivatives undo each other. You know this from arrays. Given f[], its prefix sum F[i] = f[0] + ... + f[i-1] has adjacent difference F[i+1] - F[i] = f[i], and a range sum is a difference of two prefix sums: f[a] + ... + f[b-1] = F[b] - F[a]. The continuous version:
Theorem (fundamental theorem of calculus). Let \(f\) be continuous on \([a, b]\).
1. The running integral \(F(x) = \int_a^x f(t)\, dt\) is differentiable and \(F'(x) = f(x)\).
2. If \(G\) is any function with \(G' = f\) on \([a, b]\), then \(\displaystyle \int_a^b f(x)\, dx = G(b) - G(a)\).
Proofs in [spivak] and [openstax] (volume 1, section 5.3).
Why part 1 holds: \(F(x + h) - F(x)\) is the area of one thin strip, about \(f(x) \cdot h\); divide by \(h\) and let \(h \to 0\). Part 2 follows: \(F\) and \(G\) have the same derivative, so by the mean value theorem (section 2.8) they differ by a constant, which cancels in \(G(b) - G(a)\).
So an area becomes an evaluation. \((x^3/3)' = x^2\), hence \(\int_0^1 x^2 \, dx = \tfrac13 - 0 = \tfrac13\), the limit section 3.1 was approaching. \((-\cos x)' = \sin x\), hence \(\int_0^\pi \sin x \, dx = -\cos \pi + \cos 0 = 2\). The notation \([G(x)]_a^b\) means \(G(b) - G(a)\).
Part 1 also gives functions that have no formula. \(F(x) = \int_0^x e^{-t^2} dt\) is not built from exp, powers and the like, but it is a perfectly good function with \(F'(x) = e^{-x^2}\); up to a factor it is the error function erf of <math.h>. Numerically (with Simpson's rule, section 3.7), the central difference of \(F\) at 0.8 is 0.5272924, and \(e^{-0.64} = 0.5272924\). The same check confirms \(\int_0^\pi \sin x\,dx = 2\) and the examples of the next two sections to 12 digits.
A function \(G\) with \(G' = f\) is an antiderivative of \(f\), written \(\int f(x)\,dx = G(x) + C\): defined up to a constant \(C\), since constants have derivative 0. Reading the table of derivatives (section 2.2) right to left gives
\[ \begin{aligned} \int x^n dx &= \frac{x^{n+1}}{n + 1} + C \;\; (n \ne -1), & \int \frac{dx}{x} &= \ln |x| + C, & \int e^x dx &= e^x + C, \\ \int \sin x \, dx &= -\cos x + C, & \int \cos x \, dx &= \sin x + C. \end{aligned} \]Differentiating is mechanical; antidifferentiating is a search. There is no product rule or chain rule to run backwards automatically, and many simple functions (\(e^{-x^2}\), \(\sin(x)/x\)) have no antiderivative built from the usual functions at all. Two techniques cover most cases that do.
Substitution is the chain rule backwards. Since \(\big(G(g(x))\big)' = G'(g(x))\, g'(x)\),
\[ \int_a^b f(g(x))\, g'(x)\, dx = \int_{g(a)}^{g(b)} f(u)\, du. \]In practice: spot an inner function \(u = g(x)\) whose derivative also appears as a factor, write \(du = g'(x)\,dx\), and change the limits. Example: in \(\int_0^1 2x\, e^{x^2} dx\), put \(u = x^2\), \(du = 2x\,dx\), and the limits 0, 1 stay 0, 1:
\[ \int_0^1 2x\, e^{x^2} dx = \int_0^1 e^u \, du = e - 1. \]The same move gives \(\int x\, e^{-x^2/2} dx = -e^{-x^2/2} + C\) (with \(u = -x^2/2\)), which is what makes the normal distribution's mean and variance computable (section 12.8). A linear substitution \(u = c x\) gives the rule for scales: \(\int f(cx)\, dx = \frac{1}{c} G(cx) + C\).
The product rule \((fg)' = f'g + fg'\), integrated over \([a, b]\) with the fundamental theorem, gives
\[ \int_a^b f(x)\, g'(x)\, dx = \big[ f(x) g(x) \big]_a^b - \int_a^b f'(x)\, g(x)\, dx. \]It trades one integral for another; it helps when \(f'\) is simpler than \(f\) and \(g'\) is easy to integrate. Example: with \(f = x\), \(g' = e^x\) (so \(f' = 1\), \(g = e^x\)),
\[ \int_0^1 x e^x dx = \big[x e^x\big]_0^1 - \int_0^1 e^x dx = e - (e - 1) = 1. \]And \(\ln\), which looks hopeless, falls to \(f = \ln x\), \(g' = 1\): \(\int_1^e \ln x \, dx = [x \ln x]_1^e - \int_1^e x \cdot \frac{1}{x} dx = e - (e - 1) = 1\). Both are checked in the program of section 3.3. The discrete twin, summation by parts, rearranges sums the same way [gkp].
An integral over an infinite range is the limit of integrals over finite ones:
\[ \int_a^\infty f(x)\, dx = \lim_{R \to \infty} \int_a^R f(x)\, dx, \]if the limit exists (the integral converges). The area of an infinitely long region can be finite, if the curve falls fast enough:
\[ \int_0^\infty e^{-x} dx = \lim_{R \to \infty} \big[-e^{-x}\big]_0^R = \lim_{R \to \infty} (1 - e^{-R}) = 1, \qquad \int_1^\infty \frac{dx}{x^2} = \lim_{R \to \infty} \left(1 - \frac{1}{R}\right) = 1. \]But \(\int_1^\infty dx/x = \lim \ln R = \infty\): \(1/x\) falls too slowly. Between them is the dividing line: \(\int_1^\infty x^{-p} dx\) converges exactly when \(p > 1\), the same rule as for \(\sum 1/n^p\) (section 5.1).
The integral the normal distribution rests on is over the whole line:
\[ \int_{-\infty}^{\infty} e^{-x^2/2} dx = \sqrt{2\pi}. \]\(e^{-x^2/2}\) has no elementary antiderivative, so the fundamental theorem cannot do this one. The standard proof squares the integral, reads it as a volume over the plane, and switches to polar coordinates, where the substitution of section 3.4 works [openstax] (volume 3, double integrals in polar coordinates) [blitzstein]. Dividing by \(\sqrt{2\pi}\) gives the standard normal density, whose total weight is 1 (section 12.8). Numerically a finite range does it, because the tails die fast. \(\int_{-R}^{R} e^{-x^2/2} dx\) is 1.7112 for \(R = 1\), 2.3926 for \(R = 2\), 2.506469 for \(R = 4\), and 2.506628274631 for \(R = 8\), which is \(\sqrt{2\pi}\) to all 12 digits: the tail beyond 8 is below \(e^{-32}\). Compare \(\int_0^R e^{-x} dx = 1 - e^{-R}\), whose tail \(e^{-R}\) shrinks much more slowly (0.99966 at \(R = 8\)), and \(\int_1^R dx/x = \ln R\), 3.4657 at \(R = 32\) and still growing.
When there is no antiderivative, or \(f\) is a program, the integral is computed from samples \(f_i = f(a + i h)\), \(i = 0, \dots, n\). Two rules [burden]:
The order shows in what happens when \(h\) halves: an \(O(h^p)\) error drops by \(2^p\). Halving \(h\) should divide the trapezoid error by 4 and Simpson's by 16. On \(\int_0^1 e^x dx = e - 1\):
#include <math.h>
#include <stdio.h>
int main(void)
{
double exact = exp(1) - 1, et_old = 0, es_old = 0; /* int_0^1 e^x dx */
for (int n = 2; n <= 512; n *= 2) {
double h = 1.0 / n, t = (exp(0) + exp(1)) / 2, s = exp(0) + exp(1);
for (int i = 1; i < n; i++) {
t += exp(i * h);
s += exp(i * h) * (i % 2 ? 4 : 2);
}
double et = fabs(t * h - exact), es = fabs(s * h / 3 - exact);
printf("n = %3d trapezoid err %.3e", n, et);
if (n > 2) printf(" (ratio %5.2f)", et_old / et); else printf(" ");
printf(" Simpson err %.3e", es);
if (n > 2) printf(" (ratio %5.2f)", es_old / es);
printf("\n");
et_old = et; es_old = es;
}
return 0;
}
n = 2 trapezoid err 3.565e-02 Simpson err 5.793e-04 n = 4 trapezoid err 8.940e-03 (ratio 3.99) Simpson err 3.701e-05 (ratio 15.65) n = 8 trapezoid err 2.237e-03 (ratio 4.00) Simpson err 2.326e-06 (ratio 15.91) n = 16 trapezoid err 5.593e-04 (ratio 4.00) Simpson err 1.456e-07 (ratio 15.98) n = 32 trapezoid err 1.398e-04 (ratio 4.00) Simpson err 9.103e-09 (ratio 15.99) n = 64 trapezoid err 3.496e-05 (ratio 4.00) Simpson err 5.690e-10 (ratio 16.00) n = 128 trapezoid err 8.740e-06 (ratio 4.00) Simpson err 3.556e-11 (ratio 16.00) n = 256 trapezoid err 2.185e-06 (ratio 4.00) Simpson err 2.222e-12 (ratio 16.00) n = 512 trapezoid err 5.462e-07 (ratio 4.00) Simpson err 1.403e-13 (ratio 15.83)
Printed on FreeBSD 15.1 on amd64 (Core i5-9400), clang 19. It depends on the machine: elsewhere the timings, the random numbers or the last digits differ.
How it maps to the formulas. h = 1.0 / n is \(h = (b - a)/n\) with \([a, b] = [0, 1]\); the loop visits the interior samples \(x_i = ih\), \(i = 1, \dots, n - 1\), and both sums start from the endpoints. The trapezoid sum t gives the ends weight \(\tfrac12\) and every interior point weight 1, because each interior point is the right end of one trapezoid and the left end of the next, and each trapezoid gives its two ends \(\tfrac{h}{2}\). Simpson's s gives odd \(i\) weight 4 and even interior \(i\) weight 2: each pair of strips \([x_{2j}, x_{2j+2}]\) is a parabola through three points, whose integral is \(\tfrac{h}{3}(f_{2j} + 4 f_{2j+1} + f_{2j+2})\), and neighbouring pairs share the endpoint \(x_{2j+2}\), so it collects \(1 + 1 = 2\). That is also why \(n\) must be even, here a power of 2: the strips come in pairs. The common factors \(h\) and \(h/3\) are applied once at the end rather than per term. The ratio printed is error at \(n/2\) over error at \(n\), the \(2^p\) the order predicts. The test integrand \(e^x\) is smooth, with all derivatives between 1 and \(e\), so the asymptotic error formulas apply from the start.
The ratios settle at 4.00 and 16.00. Simpson's error at 512 strips is \(10^{-13}\), and the last ratio, 15.83, is the first sign of rounding: the sum's own floating-point error (section 14.5) is starting to matter. The same table is a practical tool. Compute with \(n\) and \(2n\) strips; the ratio of the changes tells you whether the rule is in its asymptotic regime, and the last change estimates the remaining error. Richardson extrapolation and Romberg integration automate this [burden].
Watch out. The \(O(h^4)\) promise needs a smooth \(f\): it is a statement about \(f^{(4)}\). At a kink or a jump (an if in the function) Simpson degrades to the accuracy of the crudest rule, and the halving ratios stop being 16. Integrate piecewise, splitting at the kink.
\([x^3 - x^2]_0^2 = 8 - 4 = 4\).
Substitute \(u = x^2/2\), \(du = x\,dx\): \(\int_0^\infty e^{-u} du = 1\).
By parts with \(f = x\), \(g' = e^{-x}\): \([-x e^{-x}]_0^\infty + \int_0^\infty e^{-x} dx = 0 + 1 = 1\).
\(\dfrac{1}{2\delta} \int_{-\delta}^{\delta} e^x dx = \dfrac{e^\delta - e^{-\delta}}{2\delta} = \dfrac{\sinh \delta}{\delta}\) (section 4.5). At \(\delta = 1\): \(1.1752\) against \(\cosh 1 = 1.5431\). The cosh bound is the worst case, all deviations at the ends (section 4.6); for small \(\delta\) the even spread gives \(1 + \delta^2/6\), a third of the worst-case excess \(\delta^2/2\).
For \(\int_0^1 e^x\) with \(n = 8\), both give 1.718281974051892, and \((M - I)/(T - I) = -0.4999\). Since \(M \approx I - E/2\) and \(T \approx I + E\), \((2M + T)/3 \approx I + (-E + E)/3 = I\): the leading errors cancel. Writing out the weights shows it is exactly Simpson's \(1, 4, 2, 4, \dots\) pattern on the finer grid.
Simpson: between 64 (\(5.7 \times 10^{-10}\)) and 128 (\(3.6 \times 10^{-11}\)), about 100. Trapezoid: the error is \(3.6 \times 10^{-2}\) at \(n = 2\) and falls as \(1/n^2\), so \(n \approx 2 \sqrt{3.6 \times 10^{-2} / 10^{-10}} \approx 38000\). Same loop, same cost per sample, 400 times fewer samples: order matters more than constant factors.