This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Notebooks · posts/gauss-legendre-quadrature-and-where-tanh-sinh-wins

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.

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.

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.

[1]lines 30–92

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.

[2]lines 116–184

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.

[3]lines 203–247

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.

[4]lines 283–390