dblquad() integrates a function of two variables, func(y, x), note the argument order is y first, then x, a commonly-missed detail, over x ranging from a to b, and for each x, y ranging between gfun(x) and hfun(x), which can be constant functions for a simple rectangular region, or functions of x for a region with curved or slanted boundaries. Like quad(), it returns a tuple of the estimated integral value and an error estimate, and internally it works by repeatedly calling quad() for the inner integral at many different x values as part of computing the outer integral.
1Understanding integrate.dblquad()
dblquad() integrates a function of two variables, func(y, x), note the argument order is y first, then x, a commonly-missed detail, over x ranging from a to b, and for each x, y ranging between gfun(x) and hfun(x), which can be constant functions for a simple rectangular region, or functions of x for a region with curved or slanted boundaries. Like quad(), it returns a tuple of the estimated integral value and an error estimate, and internally it works by repeatedly calling quad() for the inner integral at many different x values as part of computing the outer integral.
dblquad()'s function signature expects func(y, x), with y as the first argument, the reverse of how you might instinctively write it — this argument order is a common, easy-to-miss source of bugs when defining the integrand.
from scipy import integrate
result, error = integrate.dblquad(lambda y, x: x * y, 0, 2, 0, 1)
print(result)2Practical Example
Here is a real-world application of integrate.dblquad() showing how it is used in production SciPy code.
from scipy import integrate
result, error = integrate.dblquad(lambda y, x: 1, 0, 1, lambda x: 0, lambda x: x)
print(result)3Best Practices
Follow these guidelines when working with integrate.dblquad():
1. Double check the argument order of your integrand function — dblquad() expects func(y, x), y first, not the more intuitive func(x, y)
2. Use constant lambda functions for gfun/hfun when integrating over a simple rectangular region
3. Use functions of x for gfun/hfun when the integration region has boundaries that vary with x, rather than trying to force a non-rectangular region into a rectangular one
Tip: dblquad()'s function signature expects func(y, x), with y as the first argument, the reverse of how you might instinctively write it — this argument order is a common, easy-to-miss source of bugs when defining the integrand.
from scipy import integrate
result, error = integrate.dblquad(lambda y, x: x * y, 0, 2, 0, 1)
print(result)