Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 37 ยท Numerical Differentiation and Integration

Gaussian and Adaptive Quadrature

Get exact answers for polynomials of degree 2n-1 with n points. Learn Gauss-Legendre, adaptive Simpson and SciPy's quad for difficult integrals.

Newton-Cotes rules fix the nodes in advance (equally spaced) and choose only the weights. Gaussian quadrature frees the nodes too: with n points you choose both the positions and the weights, which gives 2n free numbers and lets the rule integrate every polynomial up to degree 2n - 1 exactly. An n-point Gauss rule is therefore about twice as accurate, order for order, as a Newton-Cotes rule using the same number of function values.

Then we turn to the practical side: adaptive quadrature, which spends effort only where the integrand is difficult, and SciPy's quad, which does this for you.

Gauss-Legendre rules

On the standard interval [-1, 1] the nodes of the n-point Gauss-Legendre rule are the roots of the Legendre polynomial P_n, and the weights are positive. For example:

  • n = 1: node 0, weight 2 (the midpoint rule).
  • n = 2: nodes +-1/sqrt(3), weights 1, 1. Exact for cubics with just two evaluations.
  • n = 3: nodes 0, +-sqrt(3/5), weights 8/9, 5/9, 5/9.

For any interval [a, b], change variables with x = (b - a)/2 * t + (a + b)/2, so integral = (b - a)/2 * sum of wi * f(x(ti)). NumPy supplies the tables with np.polynomial.legendre.leggauss(n).

Python Gauss-Legendre in a few lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With just 5 evaluations the error is already near machine precision for exp. For smooth integrands Gaussian quadrature converges extremely quickly (exponentially in n).

Other weight functions give other families: Gauss-Laguerre for integral from 0 to infinity of exp(-x) f(x), Gauss-Hermite for exp(-x^2), Gauss-Chebyshev for 1/sqrt(1 - x^2). They are in np.polynomial too.

Composite Gauss and its limits

Gaussian rules work best when f is smooth on the whole interval. If f has a kink, sharp peak or singularity, one global Gauss rule converges slowly. Remedies: split the interval into panels and apply a low-order Gauss rule to each (a composite rule), or let an algorithm decide where to split.

Adaptive quadrature

Idea: estimate the error on a sub-interval; if it is above the tolerance, split the interval in half and treat each half the same way. Smooth stretches stop early, rough ones are refined.

Adaptive Simpson: compute S on [a, b] and S_left + S_right on the two halves. The difference D = S_left + S_right - S estimates the error (the corrected value is S_left + S_right + D / 15). If |D| <= 15 * tol, accept; otherwise recurse with tolerance tol / 2 on each half.

Python Adaptive Simpson from scratch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The recursion piles its points around x = 0.3, where the integrand spikes, and uses very few elsewhere. A fixed-step rule would need thousands of equal panels to reach the same accuracy.

SciPy's quad

scipy.integrate.quad wraps the QUADPACK library: adaptive Gauss-Kronrod (a 21-point rule that reuses nodes of a 10-point Gauss rule to estimate its own error). It returns the value and an error estimate, handles infinite limits and has options for singular points and weights.

Python quad, infinite limits and singularities

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Plot a hard integrand before integrating it. Use points=[...] for known kinks and singularities, and substitute variables to remove endless tails or singular ends.

Which method should I use?

  • Smooth integrand and speed matters: Gauss-Legendre with a modest n.
  • Unknown behaviour, one-off job: scipy.integrate.quad.
  • Only a table of values: trapezoid or Simpson on the samples.
  • Several dimensions: dblquad, nquad, or Monte Carlo for many dimensions.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What degree of polynomial does an n-point Gauss-Legendre rule integrate exactly?

    Choosing both nodes and weights gives 2n free parameters, matching a polynomial space of degree 2n - 1.
  2. 2. The nodes of the Gauss-Legendre rule are the roots of which polynomial?

    Nodes are the roots of the degree-n Legendre polynomial; weights follow from them.
  3. 3. How does adaptive quadrature save work?

    Smooth parts stop early, while difficult regions are subdivided.
  4. 4. How do you help quad with a kink at x = 0 inside the interval?

    The points argument tells quad where to split the interval so that each piece is smooth.

Summary

  • Gaussian quadrature chooses nodes and weights together: n points are exact up to degree 2n - 1 and converge extremely fast for smooth integrands.
  • Adaptive methods subdivide where an error estimate is large; adaptive Simpson is easy to code.
  • scipy.integrate.quad (adaptive Gauss-Kronrod) handles infinite limits, singularities and returns an error estimate.
  • Plot first, split at known trouble spots, and choose the tool to match the data.

Next: Euler's method for ODEs