demoAINumerics
Gauss-Legendre quadrature against tanh-sinh on a smooth and a singular integral
Five Gauss-Legendre points integrate every polynomial of degree 9 or less exactly, and miss degree 10 by exactly what the error formula says. On seven points reach rounding. On , which is infinite at both ends of the interval, 128 points still miss by , and the error only halves when the points double. The tanh-sinh rule gets that integral to rounding with 37 points.
Open in the workbenchDownload .vivOutputs below are from the site build’s run; Run all runs it again in your browser.
Look inside this program
- Atlas
The program’s map: every function and which calls which.
There’s no recorded run of this program yet.
A quadrature rule replaces an integral by a weighted sum of the integrand's values: . With points there are numbers to choose, the nodes and the weights . The Gauss-Legendre rule chooses them so that the sum is exact for every polynomial of degree up to , which is as high as free numbers can reach. The nodes turn out to be the roots of the Legendre polynomial .
The cells below compute the nodes and weights, check the degree claim, and then try the rule on two integrands: , which is smooth, and , whose integral is although the integrand is infinite at . On the second one a different rule, tanh-sinh, does far better.
The reference is posts/refs/gauss-legendre-quadrature-and-where-tanh-sinh-wins.py
(output beside it): numpy's leggauss, which gets the nodes from the
eigenvalues of a tridiagonal matrix rather than by Newton's method as here,
closed-form integrals, and the tanh-sinh rule written in numpy.
Printed
Tests 2 of 2 pass
- the five nodes and weights are numpy's leggauss(5), to 3e-16 (passes)
- the weights add up to 2, the length of the interval (passes)
Exact through degree 9
The cell integrates for , whose exact integral is , with the 5-point rule and with the 3-point rule. (A power of alone would be a poor test: for odd the symmetric nodes give 0, which is right, for any rule.) The 5-point rule is exact to rounding through degree 9 and then misses. The 3-point rule misses from degree 6 on, as says.
At degree the miss is known exactly. The error of the -point rule is
for some in the interval, and for the tenth derivative is the constant , so with the error is .
In the figure an error of exactly 0 is drawn at , since a log axis has no 0.
Printed
Tests 3 of 3 pass
- the 5-point rule is exact to rounding (under 1e-15) for every degree up to 9 (passes)
- and degree 10 misses by 2^11 (5!)^4 / (11 (10!)^2) = 2.9318e-3, as numpy's rule does (passes)
- the 3-point rule is exact through degree 5 and off by 2.50e-3 (relative) at degree 6, as numpy finds (passes)
A smooth integrand and a singular one
is not a polynomial, but it is smooth everywhere, including off the real line, and polynomials approximate it well. The Gauss-Legendre error falls faster than any power of : at 5 points, rounding at 7.
is a different case. Its integral is , finite, but the function goes to infinity at both ends, and no polynomial follows it there. The rule never evaluates at , which is good, since the value there is infinite; but its outermost node sits a distance of order from the end, and the integral over that last stretch, over a length of order , is of order . The error behaves like that: it halves each time doubles, and times the error settles near 1.73. At that rate each further digit costs ten times as many points.
Printed
Tests 3 of 3 pass
- Gauss-Legendre misses the integral of e^x by 8.248e-10 with 5 points and by under 3e-15 from 7 on, as with numpy's nodes (passes)
- 128 Gauss-Legendre points still miss the integral of 1/sqrt(1 - x^2) by 1.3552e-2, numpy's figure (passes)
- and n times the error is 1.7279 at 64 points and 1.7346 at 128, as numpy finds (passes)
Tanh-sinh
The tanh-sinh rule, from Takahasi and Mori (1974), changes variables first:
As runs over the whole line, runs over , and the endpoints move off to . The factor falls like , a double exponential, which is faster than grows. So the new integrand is smooth and dies off quickly in both directions, and for such a function the plain trapezoid rule, equal steps in , converges very fast. Here the nodes are with . The last term kept on the singular integrand is at , whose last node is , and under for every smaller step; the terms left out are smaller still.
There is one trap. Once passes about 19, rounds to exactly 1, and computed from is 0: the singular integrand becomes . With that happens at 12 of the 37 nodes, and the sum is infinite. The fix is to compute directly as , which is tiny there but not 0, and to write the integrand in terms of it.
Halving doubles the points. On the singular integral the error goes , and about , which is rounding, at 9, 19 and 37 points: the number of correct digits more than doubles with each halving, until rounding stops it. Gauss-Legendre with 128 points was at . On the roles reverse: tanh-sinh needs 73 points for rounding where Gauss-Legendre needed 7. The substitution spends most of its points near the ends, and a smooth integrand has nothing there that needs them.
Printed
Tests 5 of 5 pass
- h = 1, 1/2, 1/4 use 9, 19 and 37 points; the last term is 2.073e-17 at h = 1, under 1e-28 after (passes)
- with h = 1/4, 1 - x*x rounds to 0 at 12 of the 37 nodes, as in numpy (passes)
- tanh-sinh misses the integral of 1/sqrt(1 - x^2) by 1.915e-3 and 1.972e-8 at 9 and 19 points, as numpy's rule does (passes)
- and under 2e-15 from 37 points on, where Gauss-Legendre at 128 points is off by 1.4e-2 (passes)
- tanh-sinh misses the integral of e^x by 8.14e-11 at 37 points and by under 4e-15 from 73 on (passes)