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

← Posts

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.

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

X5: [-0.90618, -0.538469, 0, 0.538469, 0.90618]
W5: [0.236927, 0.478629, 0.568889, 0.478629, 0.236927]
node_diff: 1.11022e-16
weight_diff: 2.22045e-16

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

ERR5: [0, 1.11022e-16, 1.66533e-16, 2.22045e-16, 5.55112e-16, 4.996e-16, 5.82867e-16, 6.66134e-16, 7.49401e-16, 8.32667e-16, 1.5747e-05, 9.44822e-05, 0.000319752, 0.000805286]
ERR3: [2.22045e-16, 3.33067e-16, 3.33067e-16, 4.44089e-16, 5.55112e-16, 3.33067e-16, 0.0025, 0.01, 0.023875, 0.044375, 0.0709813, 0.102763, 0.138632, 0.177507]
miss10: 0.00293181
formula10: 0.00293181
Relative error on the integral of (1+x)k
5 points3 points
10⁻¹⁶10⁻¹²10⁻⁸10⁻⁴10⁰024681012degree krelative error

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

gl_smooth_err: [0.350402, 0.0077063, 6.54586e-05, 2.95131e-07, 8.24777e-10, 1.56808e-12, 1.77636e-15, 0, 0, 8.88178e-16, 0, 4.44089e-16]
gl_sing_err: [0.386188, 0.204751, 0.105522, 0.0535788, 0.0269979, 0.0135516]
[1.54475, 1.638, 1.68835, 1.71452, 1.72786, 1.7346]

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

ts_step: [1, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625]
ts_points: [9, 19, 37, 73, 145, 289, 577]
ts_exp_err: [0.0943617, 0.000120312, 8.13616e-11, 8.88178e-16, 0, 4.44089e-16, 3.10862e-15]
ts_sing_err: [0.00191533, 1.97159e-08, 8.88178e-16, 8.88178e-16, 4.44089e-16, 4.44089e-16, 8.88178e-16]
ts_zeros: [2, 6, 12, 22, 42, 84, 168]
2.07293e-17
1/1−x2 on [−1,1]: error against points10⁻¹⁵10⁻¹⁰10⁻⁵10⁰10¹10²points|error|
Gauss-Legendretanh-sinh
ex on [−1,1]: error against points10⁻¹⁵10⁻¹⁰10⁻⁵10⁰10⁰10¹10²points|error|
Gauss-Legendretanh-sinh

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)

Checked when the site was built: 13 of 13 tests passed, in under 0.1 s. Download 2026-09-25-gauss-legendre-quadrature-and-where-tanh-sinh-wins.viv