🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

integrate.dblquad()

AI & DATA SCIENCE // integrate-dblquad

scipy.integrate.dblquad() numerically computes a double integral of a two-variable function over a rectangular (or variable-bounded) region.

Syntax

scipy.integrate.dblquad(func, a, b, gfun, hfun)

Deep Dive Course

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.

editor.html
from scipy import integrate

result, error = integrate.dblquad(lambda y, x: x * y, 0, 2, 0, 1)
print(result)
localhost:3000

2Practical Example

Here is a real-world application of integrate.dblquad() showing how it is used in production SciPy code.

editor.html
from scipy import integrate

result, error = integrate.dblquad(lambda y, x: 1, 0, 1, lambda x: 0, lambda x: x)
print(result)
localhost:3000

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.

editor.html
from scipy import integrate

result, error = integrate.dblquad(lambda y, x: x * y, 0, 2, 0, 1)
print(result)
localhost:3000

Examples

Example 01Basic Usage
from scipy import integrate

result, error = integrate.dblquad(lambda y, x: x * y, 0, 2, 0, 1)
print(result)
Example 02Advanced Example
from scipy import integrate

result, error = integrate.dblquad(lambda y, x: 1, 0, 1, lambda x: 0, lambda x: x)
print(result)

Best Practices

  • Double check the argument order of your integrand function — dblquad() expects func(y, x), y first, not the more intuitive func(x, y)
  • Use constant lambda functions for gfun/hfun when integrating over a simple rectangular region
  • 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

Interview Question

Why does dblquad() expect the integrand function to be defined as func(y, x), with y listed first, rather than the more intuitive func(x, y)?

Hint: This is a specific, documented quirk of dblquad()'s API design, worth simply knowing rather than deriving.

This ordering is a specific, historical convention in dblquad()'s API design — internally, the function performs the innermost integral over y first, for each fixed value of x, before performing the outer integral over x, and the function signature's argument order was defined to match that internal evaluation order, innermost variable first. It's not something you can derive from first principles; it's simply a documented quirk of the function's interface that's easy to get backwards if you assume the more common mathematical convention of listing x before y.

Exercises

MediumPractice using integrate.dblquad() in a real scenario.
View Solution
from scipy import integrate

result, error = integrate.dblquad(lambda y, x: x * y, 0, 2, 0, 1)
print(result)

Frequently Asked Questions

Why does dblquad() expect the integrand function to be defined as func(y, x), with y listed first, rather than the more intuitive func(x, y)?

This ordering is a specific, historical convention in dblquad()'s API design — internally, the function performs the innermost integral over y first, for each fixed value of x, before performing the outer integral over x, and the function signature's argument order was defined to match that internal evaluation order, innermost variable first. It's not something you can derive from first principles; it's simply a documented quirk of the function's interface that's easy to get backwards if you assume the more common mathematical convention of listing x before y.

Related Functions

integrate-quadintegrate-odeintnp-multiply