Analysis

Where to take the samples

Every numerical integral is a weighted sum of samples of the curve. Spacing the samples evenly gives errors that shrink like a power of their number; choosing where they go, as Gauss did in 1814, makes six samples exact for every polynomial up to degree eleven and turns the power into a geometric rate. The surprise is that the humble trapezoid rule, evenly spaced and exact only for straight lines, does the same thing on any smooth curve that goes once round a circle — and that a single corner defeats every one of them.
20 min read 7 figures The same thing twiceSmall cases lie

Worth reading first: Adding up rectangles until they stop being rectangles · Area is the undoing of slope.

Adding up rectangles defined the integral as what the sum of thin rectangles approaches, and measured how slowly it approaches: on a smooth curve, the left-hand sum of nn rectangles is off by an amount proportional to 1/n1/n, so a thousand rectangles buy three decimal places and no more. Area is the undoing of slope then gave the integral a second meaning, the undoing of a derivative, and noted that it usually cannot be used: most integrands have no antiderivative that can be written down, and the sums come back.

What comes back, though, is not one sum but a family. Every method of numerical integration in use is a quadrature rule, a weighted sum

∫−11f(x) dx≈∑i=1nwi f(xi),\int_{-1}^{1} f(x)\,dx \approx \sum_{i=1}^{n} w_i\, f(x_i),

and the methods differ only in where they put the sample points xix_i and how much weight wiw_i they give each. This essay compares four of them by measurement, and the measurements say that where the samples go matters more than how many there are.

Where the samples go matters more than how many. n=10: trap -2.65, simp -1.70, cc -1.74, gauss -2.07; n=20: trap -3.69, simp -3.35, cc -3.89, gauss -4.21; n=40: trap -4.31, simp -7.10, cc -8.16, gauss -8.51; n=80: trap -4.93, simp -8.28, cc -14.49, gauss -15.26
Fig. 1 The error of four rules integrating 1/(1+16x2)1/(1 + 16x^2) from −1-1 to 11, against the number of samples. Evenly spaced samples give errors that shrink like a power; well-placed samples give errors that shrink geometrically.

Four ways to choose

The trapezoid rule takes evenly spaced samples, joins neighbouring ones by straight lines, and adds the areas of the trapezoids underneath. Its weights are all equal except at the two ends, where they are halved. It is exact for straight lines and its error shrinks like 1/n21/n^2. Simpson’s rule fits a parabola through each consecutive triple of samples instead; its weights alternate 13,43,23,43,…\tfrac13, \tfrac43, \tfrac23, \tfrac43, \ldots, and it is exact, by a lucky cancellation, not only for parabolas but for cubics. Its error shrinks like 1/n41/n^4. These are the rules taught first, and in the figure they are the two straight lines on the logarithmic scale — a fixed power of nn.

The lucky cancellation is worth a sentence, because it is the first sign of the idea Gauss pushed to its limit. On a single panel [−h,h][-h, h], Simpson’s rule samples at −h-h, 00 and hh, and any cubic can be split into a parabola plus a multiple of x3x^3. The parabola is integrated exactly by construction, and x3x^3 is odd, so its integral over the symmetric panel is nought — and so is the rule’s value for it, since the samples at ±h\pm h cancel and the sample at 00 is nought. A rule designed for degree two gets degree three free, because its samples are placed symmetrically. Placement, not just the number of samples, buys exactness. The midpoint rule has the same gift one level down: one sample at the centre of each panel is designed to get constants right and gets straight lines right as well.

The other two do not space their samples evenly. Clenshaw–Curtis, published by Charles Clenshaw and Alan Curtis in 1960, samples at the Chebyshev points cos⁡(kπ/m)\cos(k\pi/m), which crowd towards the two ends of the interval, and chooses weights that integrate exactly the polynomial through the samples. Gauss’s rule, from 1814, chooses both the points and the weights to make the rule exact for polynomials of the highest degree possible.

The figure’s integrand, 1/(1+16x2)1/(1 + 16x^2), is the function that made Carl Runge’s name in the points that ruin the fit: perfectly smooth on the real line, but with poles close to it at ±i/4\pm i/4, which is what makes equally spaced interpolation of it fail. Its integral is exactly 12arctan⁡4\tfrac12 \arctan 4, so every error can be measured. With eighty samples the trapezoid rule is still wrong in the fifth decimal place and Simpson’s in the eighth. Gauss’s rule reaches the limit of double-precision arithmetic at seventy-one samples, and Clenshaw–Curtis follows it down, a little behind.

Twice as many conditions as samples

Gauss’s idea is a count. A rule with nn samples has nn points and nn weights, 2n2n numbers in all. A polynomial of degree 2n−12n - 1 has 2n2n coefficients. So it is at least plausible that the 2n2n numbers can be chosen to integrate every polynomial of degree up to 2n−12n - 1 exactly — one condition for each power 1,x,x2,…,x2n−11, x, x^2, \ldots, x^{2n-1} — and Gauss proved that they can, in exactly one way.

Which powers six samples get exactly right. trapezoid, 6 samples: exact to degree 1; Simpson, 5 samples: exact to degree 3; Clenshaw–Curtis, 6 samples: exact to degree 5; Gauss, 6 samples: exact to degree 11
Fig. 2 The error each of four rules makes on the powers x0x^0 to x14x^{14} over [−1,1][-1, 1] with six samples. The shaded cells are exact to rounding. Gauss’s rule is exact up to degree eleven, the most six samples can achieve.

The table makes the count visible. With six samples, the trapezoid rule is exact for degree one and gets x2x^2 wrong already; Simpson’s rule, with its parabolas, is exact to degree three; Clenshaw–Curtis, which integrates the interpolating polynomial through its samples, is exact to degree five; and Gauss’s rule is exact all the way to degree eleven. Twelve is impossible for any rule with six samples, because the polynomial that is the square of (x−x1)⋯(x−x6)(x - x_1)\cdots(x - x_6) has degree twelve, is never negative and vanishes at every sample, so every rule with those samples assigns it the value nought while its integral is positive.

The same argument says where the samples must go. Let p(x)=(x−x1)⋯(x−xn)p(x) = (x - x_1)\cdots(x - x_n) be the polynomial vanishing at the nodes. For any polynomial qq of degree less than nn, the product p qp\,q has degree less than 2n2n and vanishes at every node, so an exact rule assigns it nought, and therefore

∫−11p(x) q(x) dx=0.\int_{-1}^{1} p(x)\, q(x)\, dx = 0.

The node polynomial must be perpendicular to every polynomial of lower degree, in the inner product given by the integral. That condition determines it: it is the Legendre polynomial PnP_n, which is what one subtraction clears a direction produced by applying Gram–Schmidt to 1,x,x2,x31, x, x^2, x^3 without aiming at anything in particular. The samples go at its roots.

Eight places to sample, and how much each counts. 0.960290:0.101229, 0.796666:0.222381, 0.525532:0.313707, 0.183435:0.362684, -0.183435:0.362684, -0.525532:0.313707, -0.796666:0.222381, -0.960290:0.101229
Fig. 3 The eight Gauss nodes on [−1,1][-1, 1], at the roots of the Legendre polynomial P8P_8, with bars proportional to their weights, beside eight evenly spaced midpoints. The nodes crowd towards the ends and the weights shrink there.

Two properties of the result are worth reading off the picture. The nodes are not evenly spaced: they crowd towards the ends of the interval, at a density proportional to 1/1−x21/\sqrt{1 - x^2} — the same crowding as the Chebyshev points, and for the same reason, which is that a polynomial of high degree is most able to misbehave near the ends. And every weight is positive. That is not automatic for a rule with many samples; Newton–Cotes rules with many evenly spaced samples have weights of both signs and become useless as their degree rises, because they add large numbers that cancel. Gauss’s weights are positive and add up to two, the length of the interval, so the rule is a genuine average of the samples and cannot amplify the errors in them.

The nodes are eigenvalues

Finding the roots of PnP_n for large nn is a computation in its own right, and the way it is done in practice connects the rule to a different part of linear algebra. The Legendre polynomials obey a three-term recurrence,

(k+1)Pk+1(x)=(2k+1) x Pk(x)−k Pk−1(x),(k + 1) P_{k+1}(x) = (2k + 1)\,x\,P_k(x) - k\,P_{k-1}(x),

which is how the figures evaluate them, and a recurrence of that shape says that multiplication by xx, acting on the polynomials P0,…,Pn−1P_0, \ldots, P_{n-1}, is described by a symmetric tridiagonal matrix once the polynomials are scaled to unit length. Gene Golub and John Welsch observed in 1969 that the eigenvalues of that n×nn \times n matrix are exactly the roots of PnP_n, and that the weights are twice the squares of the first components of its unit eigenvectors.

So computing a Gauss rule is an eigenvalue problem for a symmetric matrix, and the facts that make it well behaved are the facts symmetry forces a right angle established: the eigenvalues of a symmetric matrix are real, so the nodes lie on the real line, and its eigenvectors are perpendicular, which is what makes the weights come out positive. The figures here take the older route — Newton’s method on the recurrence, started from an approximation of each root by a cosine — and check each node by evaluating PnP_n there, but the two routes are computing the same numbers.

The eigenvalue picture also explains why the nodes interlace. The roots of Pn−1P_{n-1} separate those of PnP_n, one between each neighbouring pair, because the smaller matrix is the larger with its last row and column removed, and the eigenvalues of a symmetric matrix and of such a submatrix always interlace. Adding one sample to a Gauss rule therefore moves every existing sample, which is the practical drawback the comparison below turns on.

Exact for polynomials is not the point

Exactness for polynomials is how the rules are designed, but it is not what the hero figure measures. The integrand there is not a polynomial, and the reason the well-placed rules converge geometrically on it is a theorem about approximation rather than about integration. If ff is analytic inside an ellipse in the complex plane with foci at ±1\pm 1, the best polynomial approximations of degree mm to ff on the interval have errors shrinking like ρ−m\rho^{-m}, where ρ\rho is the sum of the ellipse’s semi-axes. That is the content of an ellipse, not a disc. A rule exact for degree dd makes an error no larger than a fixed multiple of the best approximation of degree dd, so Gauss’s rule, with d=2n−1d = 2n - 1, converges like ρ−2n\rho^{-2n} and Clenshaw–Curtis, with d=nd = n, would seem to converge only like ρ−n\rho^{-n}.

For 1/(1+16x2)1/(1 + 16x^2) the poles at ±i/4\pm i/4 fix the ellipse, and ρ=14+1+1/16≈1.28\rho = \tfrac14 + \sqrt{1 + 1/16} \approx 1.28. Gauss’s errors should then fall by a factor of about 1.282≈1.641.28^2 \approx 1.64 per sample, which is a decade every 4.7 samples; the hero figure’s Gauss line falls by about that much. But Clenshaw–Curtis does not lag by the factor of two that its degree suggests. Its line is nearly parallel to Gauss’s.

Gauss against Clenshaw–Curtis

That near-equality is the subject of a 2008 paper by Nick Trefethen with the title Is Gauss quadrature better than Clenshaw–Curtis?, and the next figure repeats its comparison on four integrands.

Gauss against Clenshaw–Curtis, sample for sample. eˣ: 1e-8 at 5/8; log10 error at 40 -15.05/-15.35; 1/(1 + 16x²): 1e-8 at 38/39; log10 error at 40 -8.51/-8.16; |x|³: 1e-8 at null/null; log10 error at 40 -6.28/-6.21; |x − 0.3|: 1e-8 at null/null; log10 error at 40 -3.37/-3.69
Fig. 4 Gauss and Clenshaw–Curtis with the same number of samples on four integrands over [−1,1][-1, 1]: the entire function exe^x, Runge’s function, ∣x∣3|x|^3 with a jump in its third derivative, and ∣x−0.3∣|x - 0.3| with a corner.

For exe^x, which is analytic everywhere, Gauss’s doubled degree is visible: it reaches 10−810^{-8} with five samples against eight. For Runge’s function the two lines nearly coincide, 38 samples against 39. For the two rough integrands, both rules slow to a power of nn and finish within a decade of each other. The factor of two in the degree of exactness buys a factor of two in the number of samples only for functions so smooth that both rules finish almost at once anyway.

Trefethen’s explanation is that the Clenshaw–Curtis error is controlled not by its degree but by how well the integrand’s Chebyshev series has converged, and for any function whose series coefficients decay slower than ρ−2n\rho^{-2n} — any function with a singularity anywhere in the plane — the rule’s error tracks the coefficient at index 2n2n rather than nn, the same as Gauss’s. Clenshaw–Curtis has practical advantages that this result makes decisive: its points are known in closed form, its weights come from a fast cosine transform, and the samples for nn points are reused when the number is doubled. Gauss’s points must be computed, and change completely when nn changes.

The trapezoid rule round a circle

The trapezoid rule looks hopeless beside these, and on most integrands it is. But one class of integrand reverses the verdict entirely.

The trapezoid rule round a circle. n=4: periodic -1.46, interval -2.05; n=8: periodic -5.90, interval -2.65; n=12: periodic -11.19, interval -3.00; n=20: periodic -15.05, interval -3.45; n=40: periodic -15.05, interval -4.05
Fig. 5 The plain trapezoid rule on two smooth integrands: ecos⁡θe^{\cos\theta} over a full turn, whose integral is 2πI0(1)2\pi I_0(1), and exe^x on [0,1][0, 1]. On the interval the error falls like 1/n21/n^2; round the circle it falls geometrically.

On exe^x over [0,1][0, 1] the trapezoid rule behaves as the textbooks say: doubling the number of panels quarters the error. On ecos⁡θe^{\cos\theta} over a whole period, the same rule with the same evenly spaced samples reaches rounding error at fifteen panels. The difference is not that the second integrand is smoother — both are entire functions — but that it is periodic, and the trapezoid rule over a whole period has no ends.

The reason is clearest in Fourier’s language. Expand a periodic integrand as ∑kckeikθ\sum_k c_k e^{ik\theta}. The exact integral over a period is 2πc02\pi c_0, since every other term integrates to nought. The trapezoid rule with nn equally spaced samples sums each term eikθe^{ik\theta} exactly to nought too — unless kk is a multiple of nn, when every sample of eikθe^{ik\theta} equals one. So the rule computes 2π(c0+cn+c−n+c2n+⋯ )2\pi(c_0 + c_n + c_{-n} + c_{2n} + \cdots) and its error is the sum of the coefficients at multiples of nn. That is aliasing, exactly as in a wave known only at its ticks: sampled nn times per turn, the wave of frequency nn is indistinguishable from a constant. For a smooth periodic function the Fourier coefficients fall off quickly, so the rule’s error is as small as the nn-th coefficient.

The Euler–Maclaurin formula says the same thing from the other side. The trapezoid rule’s error on an interval is a series in powers of the panel width, with coefficients that involve the differences between the derivatives of ff at the two ends. On a whole period every one of those differences is nought, and every term of the series vanishes. What is left is smaller than any power of the panel width.

The rate is set by the nearest singularity

How much smaller depends on how far the integrand can be continued into the complex plane before it breaks.

The rate is set by the nearest singularity. a=1.1: measured 0.1935 decades per sample, predicted 0.1926; a=1.5: measured 0.4182 decades per sample, predicted 0.4180; a=3: measured 0.7656 decades per sample, predicted 0.7656
Fig. 6 The trapezoid rule’s error on 1/(a−cos⁡θ)1/(a - \cos\theta) over a full turn for a=1.1a = 1.1, 1.51.5 and 33, with the predicted rate (a+a2−1)−n(a + \sqrt{a^2 - 1})^{-n} dashed. The closer the integrand’s poles lie to the real axis, the slower the convergence.

This is the same principle that sets a power series’ radius of convergence in one point’s worth of information: how fast an expansion converges is decided by the nearest place the function breaks, even when that place is off the line where the function is being used. The integrand 1/(a−cos⁡θ)1/(a - \cos\theta) has poles where cos⁡θ=a\cos\theta = a, at imaginary distance arccosh⁡a\operatorname{arccosh} a from the real axis, and its Fourier coefficients decay like (a+a2−1)−∣k∣(a + \sqrt{a^2 - 1})^{-|k|}. The aliasing argument then predicts that each extra panel divides the error by a+a2−1a + \sqrt{a^2 - 1}, and the measured slopes match it: factors of 1.558, 2.618 and 5.828 per panel for the three values of aa. Even the integrand with a pole close to the line, at a=1.1a = 1.1, reaches full precision with eighty panels — a geometric rate, only a slow one.

This is the periodic counterpart of the ellipse for Gauss’s rule, and the two are connected by a substitution. Putting x=cos⁡θx = \cos\theta turns an integral over [−1,1][-1, 1] into an integral of a periodic function over a half-turn, and turns the Chebyshev points into equally spaced angles. Clenshaw–Curtis is, essentially, the trapezoid rule applied after that substitution — which is why it converges geometrically, and why its points crowd towards the ends of the interval: they are equally spaced round the circle and crowd only when projected onto the diameter.

A corner that no choice of samples gets round

Every rule above was rescued by smoothness. The last figure removes it.

A corner that no choice of samples gets round. trapezoid: n=50 -3.67, n=200 -4.64; Simpson: n=50 -3.73, n=200 -5.62; Clenshaw–Curtis: n=50 -4.10, n=200 -5.82; Gauss: n=50 -3.59, n=200 -4.59
Fig. 7 Each rule on ∣x−0.3∣|x - 0.3| over [−1,1][-1, 1], whose corner falls between samples, with the largest error over each stretch of nn shown so that lucky cancellations do not hide the trend. Every rule settles near 2/n22/n^2.

The integrand ∣x−0.3∣|x - 0.3| has a corner, and none of the four rules can integrate it faster than about 1/n21/n^2. Gauss’s rule, which was exact to degree 2n−12n - 1, is no better than the trapezoid rule, which was exact to degree one. The polynomial approximation that drove the geometric rates is now algebraically slow, because no polynomial can follow a corner closely, and the rules inherit that slowness whatever their degree. The errors also jump about as nn changes, because they depend on where the corner falls between the nearest samples, which is why the figure plots the worst error over each stretch rather than every value.

The remedy is not a cleverer placement but knowledge of the integrand: split the interval at 0.30.3, and each half is a straight line, integrated exactly by any of the rules. Practical integration software does this adaptively, estimating the error on each piece and subdividing where it is large, which finds corners without being told where they are.

What these measurements do not settle

Every integrand here has an exact integral, so every error is measured rather than estimated. That is not how quadrature is used. A real integrand comes without its answer, and a rule’s error must be estimated from the samples themselves — usually by comparing two rules, such as Gauss with nn points and a related rule with more, and treating their difference as the error of the less accurate one. Such estimates can be fooled. An integrand with a narrow spike between samples looks flat to every rule here, and both rules in the comparison agree on the wrong answer.

The comparisons are also one-dimensional. In two or three dimensions the product of nn-point rules needs n2n^2 or n3n^3 samples, and in high dimensions no grid of any kind is affordable, which is where the error that does not care how many dimensions takes over: random samples, with an error of 1/n1/\sqrt n whatever the dimension. The geometric rates in these figures are a one-dimensional luxury.

Still open: the best points for a class of functions

Gauss’s rule is optimal for polynomials, and the comparison with Clenshaw–Curtis shows that being optimal for polynomials is not the same as being best for the functions people integrate. For a given class of functions — analytic in a given ellipse, or in a strip, or with a given number of derivatives — the rule with nn samples that minimises the worst-case error over the class is a well-defined object, and for some classes it is known: for functions analytic in a strip, the trapezoid rule after a suitable change of variables is close to optimal, which is the basis of the double-exponential rules of Hidetosi Takahasi and Masatake Mori. For most classes the optimal points have no formula, the same predicament as the best nodes have no formula for interpolation, and they are found, when they are found, by numerical optimisation.

A sharper question concerns the comparison itself. Trefethen’s analysis shows that Clenshaw–Curtis loses at most a factor of two in the number of samples, and usually nothing; whether there is a natural family of rules, cheap to construct like Clenshaw–Curtis, that matches Gauss’s rule on entire functions while keeping Clenshaw–Curtis’s advantages elsewhere has been studied in several forms, and no single answer has displaced the two old ones.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A dashed tag is an object no other essay names yet.

AliasingAnalytic functionChebyshev polynomialConvergence rateFourier seriesInterpolationNumerical integrationOrthogonalityRiemann sumSingularity