Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 38 ยท Integration and Differential Equations

Numerical Integration

Compute definite integrals in SciPy with quad, dblquad and nquad, handle infinite limits and spikes, and integrate sampled data with trapezoid and simpson.

Many integrals have no closed form. The area under a bell curve, the total energy of a measured signal, the probability that a variable falls in a range: you can write each of them down, but you cannot always solve them with pencil and paper. Numerical integration (also called quadrature) approximates a definite integral by evaluating the function at well-chosen points and combining the results.

SciPy offers two families of tools in scipy.integrate. The first takes a function and decides by itself where to evaluate it (quad, dblquad, nquad). The second takes sampled data, values you already measured at fixed positions (trapezoid, simpson, cumulative_trapezoid). Which one you need depends on whether you can call the function at any point you like or only have a table of numbers. If you want a refresher on arrays first, see NumPy Refresher for SciPy.

flowchart TD
    A["I need an integral"] --> B{"Do I have a function<br/>I can call anywhere?"}
    B -->|"yes, one variable"| C["quad"]
    B -->|"yes, 2 variables"| D["dblquad"]
    B -->|"yes, 3 or more"| E["nquad"]
    B -->|"no, only samples"| F{"Need the running total?"}
    F -->|"no"| G["trapezoid or simpson"]
    F -->|"yes"| H["cumulative_trapezoid"]

Pick the tool by what you have: a callable function or an array of samples.

quad: one integral of a function

quad(func, a, b) integrates func from a to b. It returns two numbers: the estimated value and an estimate of the absolute error. Always look at both.

Python Your first quad call

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 function must take a single float (plus any args) and return a float. Unlike most NumPy code, quad calls it one point at a time, so you do not need to vectorize it. Under the hood quad uses an adaptive algorithm from the Fortran library QUADPACK: it splits the interval where the function is hard to approximate and keeps refining until the estimated error is below its tolerances.

Infinite limits

Pass np.inf or -np.inf as a limit. quad transforms the infinite range into a finite one internally. The classic check is the Gaussian integral, which equals the square root of pi:

Python Integrals over infinite ranges

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.

Reading the error estimate

The second return value is not the true error; it is the algorithm's own estimate. In practice it is usually pessimistic, which is good. Use it to decide whether to trust the answer:

  • A tiny estimate (around 1e-10 or below, relative to the value) means the result is reliable.
  • An estimate that is as large as the value means something is wrong.
  • Use epsabs and epsrel to tighten or loosen the tolerances, and limit to allow more subdivisions.
  • Pass full_output=True to get diagnostics. With it, quad returns an extra dictionary (with the number of evaluations under 'neval') and warns instead of staying silent when it struggles.

Singularities and spikes

Adaptive subdivision handles a surprising amount of trouble, as long as the function is integrable. The integral of 1/sqrt(x) from 0 to 1 equals 2 even though the function blows up at 0, and quad copes with it. A narrow spike or a sharp corner inside a wide interval is harder, because the algorithm must discover it by itself. If you know where it is, tell quad with points=[...]:

Python Singularities and breakpoints

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 hinted call reports a much smaller error estimate (here by a factor of a few hundred) because the interval is split exactly at the spike. The bigger lesson: help the integrator when you know something about the function.

Warning: quad is a one-dimensional tool. Do not call it on a function that returns an array, and do not feed it an array of limits. To integrate for many different limits, loop over them (or use cumulative_trapezoid on samples).

Double and multiple integrals

dblquad(func, a, b, gfun, hfun) integrates func(y, x) over x from a to b and, for each x, over y from gfun(x) to hfun(x). Watch the argument order: y comes first in the function, x second. The inner limits are functions (or constants wrapped in lambdas), which lets you describe non-rectangular regions.

For three or more variables, or when you prefer a more uniform interface, use nquad. It takes a function and a list of ranges, innermost variable first. A range can itself be a function of the outer variables.

Python Area of a disk and volume of a sphere

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.

Every extra dimension multiplies the work, because the inner integral is recomputed for every outer point. Three nested integrals are fine for smooth functions; beyond that, consider Monte Carlo methods (see Correlation, Regression and Bootstrap for simulation ideas).

Fixed-order Gaussian quadrature and Romberg

quad chooses its own evaluation points. Two further methods are worth knowing, because sometimes you want to decide how many times the function is called.

fixed_quad(func, a, b, n=5) uses Gaussian quadrature with exactly n function evaluations. The points and weights are chosen so that the rule is exact for every polynomial up to degree 2n - 1: with n=3, an exact answer for a degree-5 polynomial costs three calls. Gaussian quadrature needs no error estimate for smooth functions, and because func receives a whole array of points at once, it can be very fast. The function must be vectorized, and fixed_quad returns a pair (value, None), because no error estimate is made.

romb(y, dx=...) is Romberg integration for samples: it needs 2**k + 1 equally spaced samples and combines the trapezoid rule at different resolutions to cancel error terms. It is the high-accuracy sibling of trapezoid and simpson for data on a regular grid.

Two more tools for special situations: quad_vec integrates a function that returns a vector (for example the integral of many curves at once) with one adaptive run, and nquad, shown above for double integrals, handles any number of dimensions with a list of limits.

Python fixed_quad, romb, quad_vec and nquad

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 polynomial is integrated exactly with three evaluations, and for exp the error falls from about 4e-4 with two points to the limit of floating-point precision with eight. Romberg beats the plain trapezoid rule on the same 17 samples.

Old tutorials use integrate.quadrature, integrate.romberg and integrate.trapz. These functions were removed from recent SciPy versions (trapz became trapezoid; quadrature and romberg were dropped in favor of quad, fixed_quad and tanhsinh). If copied code fails with AttributeError, that is why.

Integrating sampled data

Often you do not have a function at all, only measurements: speed readings every second, power samples from a sensor. For these, integrate the array with the trapezoid rule or Simpson's rule.

  • trapezoid(y, x) joins neighbouring points with straight lines and adds the areas of the trapezoids. It works for any spacing and any number of points.
  • simpson(y, x=x) fits a parabola through groups of three points. For smooth data it is far more accurate than the trapezoid rule at the same sample count.

Note that x is a keyword argument for simpson.

Python Trapezoid versus Simpson

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.

Each time the number of intervals doubles, the trapezoid error falls by about a factor of four, while Simpson's error falls by about sixteen. That is the practical meaning of "higher order": fewer samples for the same accuracy.

Older code calls trapz, which has been removed. The current names are trapezoid and simpson; if you see trapz in an old answer, replace it.

Running totals with cumulative_trapezoid

To get the integral as a function of the upper limit (distance travelled from speed, charge from current) use cumulative_trapezoid. It returns one value per interval, so it is one element shorter than the input unless you pass initial=0, which prepends a zero:

Python Position from velocity

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 numerical curve and the dashed exact curve are indistinguishable at this scale. If your samples are noisy, integration actually smooths the noise (errors partly cancel), whereas differentiation amplifies it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does integrate.quad(f, a, b) return?

    quad returns (value, abserr). The second number is the algorithm's own error estimate, and you should look at it before trusting the result.
  2. 2. In dblquad(func, a, b, gfun, hfun), which order do the arguments of func have?

    dblquad calls func(y, x): y is the inner integration variable, with limits gfun(x) to hfun(x), and x is the outer one over [a, b].
  3. 3. You know that the integrand has a narrow spike at x = 0.3. What should you do?

    The points argument tells quad about difficult locations so it splits the interval exactly there, giving a better result and a smaller error estimate.
  4. 4. You have 33 evenly spaced measurements of a smooth signal. Which call usually gives the most accurate total?

    simpson is higher order than trapezoid for smooth data. quad needs a function, not an array; y.sum() ignores the spacing; trapz is a removed name.

Summary

  • quad integrates a one-variable function and returns (value, error_estimate); use args= for extra parameters and np.inf for infinite limits.
  • Read the error estimate, tighten it with epsabs/epsrel, and help quad with points=[...] when you know where the function has spikes or corners.
  • dblquad expects func(y, x) with limit functions for the inner variable; nquad handles any number of variables, innermost first.
  • For measured data use trapezoid or simpson (with x=), and cumulative_trapezoid(..., initial=0) for running totals.
  • Doubling the samples cuts trapezoid error by about 4 and Simpson error by about 16 on smooth data.

Next up: turn rates of change into trajectories in Solving ODEs with solve_ivp.