Skip to Content

DBLQUAD

Overview

The DBLQUAD function computes the double integral of a function over a two-dimensional region defined by the limits of integration for two variables. This is useful for evaluating definite integrals in two dimensions, such as areas under surfaces or volumes. The function wraps scipy.integrate.dblquad and exposes only the most commonly used parameters, simplifying the interface for Excel use.

The double integral is calculated as:

x=ax=by=gfun(x)y=hfun(x)f(y,x)dydx\int_{x=a}^{x=b} \int_{y=gfun(x)}^{y=hfun(x)} f(y, x) \,dy \,dx

where:

  • f(y,x)f(y, x) is the function to integrate.
  • aa and bb are the limits of integration for xx.
  • gfun(x)gfun(x) and hfun(x)hfun(x) are the lower and upper boundary curves for yy, respectively.

For more details, see the official documentation.

This example function is provided as-is without any representation of accuracy.

Usage

To use the function in Excel:

=DBLQUAD(func_expr, a, b, gfun_expr, hfun_expr, [epsabs], [epsrel])
  • func_expr (str, required): Expression for the function f(y,x)f(y, x) to integrate. Use x and y as variables. For example, "x * y^2".
  • a (float, required): The lower limit of integration for xx.
  • b (float, required): The upper limit of integration for xx.
  • gfun_expr (str or float, required): Expression for the lower boundary of yy as a function of xx, or a constant. For example, "0", "x", or 0.
  • hfun_expr (str or float, required): Expression for the upper boundary of yy as a function of xx, or a constant. For example, "1", "2-x", or 1.
  • epsabs (float, optional, default=1.49e-8): Absolute tolerance for the inner 1-D integrals.
  • epsrel (float, optional, default=1.49e-8): Relative tolerance for the inner 1-D integrals.

The function returns a single value (float): the computed double integral, or an error message (string) if the input is invalid.

Examples

Example 1: Rectangular Region

This example computes the double integral of xy2x y^2 over the region xx from 0 to 2 and yy from 0 to 1.

Inputs:

func_exprabgfun_exprhfun_exprepsabsepsrel
x * y^202011.49e-81.49e-8

Excel formula:

=DBLQUAD("x * y^2", 0, 2, 0, 1)

Expected output:

Result
0.6667

This means the integral of xy2x y^2 over the region is 0.6667.

Example 2: Curved Boundaries

This example computes the double integral of 11 over the region xx from 0 to π/4\pi/4, yy from sin(x)\sin(x) to cos(x)\cos(x).

Inputs:

func_exprabgfun_exprhfun_exprepsabsepsrel
100.7853981633974483sin(x)cos(x)1.49e-81.49e-8

Excel formula:

=DBLQUAD("1", 0, PI()/4, "SIN(X)", "COS(X)")

Expected output:

Result
0.4142

This means the area of the region is 0.4142.

Example 3: Gaussian Integral

This example computes the two-dimensional Gaussian integral e(x2+y2)dydx\iint_{-\infty}^{\infty} e^{-(x^2 + y^2)} \,dy\,dx.

Inputs:

func_exprabgfun_exprhfun_exprepsabsepsrel
exp(-(x^2 + y^2))-1e101e10-1e101e101.49e-81.49e-8

Excel formula:

=DBLQUAD("EXP(-(X^2 + Y^2))", -1E10, 1E10, -1E10, 1E10)

Expected output:

Result
0.0000

This means the value of the 2D Gaussian integral is approximately 0.0000 with the given limits.

Example 4: Custom Function

This example computes the double integral of 3xy3 x y over the region xx from 0 to 1 and yy from xx to 2x2-x.

Inputs:

func_exprabgfun_exprhfun_exprepsabsepsrel
3 * x * y01x2-x1.49e-81.49e-8

Excel formula:

=DBLQUAD("3 * X * Y", 0, 1, "X", "2-X")

Expected output:

Result
1.0000

This means the integral of 3xy3 x y over the region is 1.0000.

Python Code

from scipy.integrate import dblquad as scipy_dblquad import math import re def dblquad(func_expr, a, b, gfun_expr, hfun_expr, epsabs=1.49e-8, epsrel=1.49e-8): """ Compute the double integral of a function over a two-dimensional region. Args: func_expr (str): Expression for the function f(y, x) to integrate. Use 'x' and 'y' as variables. a (float): Lower limit for x. b (float): Upper limit for x. gfun_expr (str or float): Lower boundary for y as a function of x or a constant. hfun_expr (str or float): Upper boundary for y as a function of x or a constant. epsabs (float, optional): Absolute tolerance. Default is 1.49e-8. epsrel (float, optional): Relative tolerance. Default is 1.49e-8. Returns: float: The computed double integral, or an error message (str) if invalid. This example function is provided as-is without any representation of accuracy. """ # Validate bounds if b < a: return "Invalid input: lower limit must be less than or equal to upper limit" def _parse_expr(expr, varnames): # Only allow safe names allowed_names = {k: getattr(math, k) for k in dir(math) if not k.startswith("_")} allowed_names.update({v: None for v in varnames}) # Replace ^ with ** for exponentiation expr = re.sub(r"\^", "**", expr) code = compile(expr, "<string>", "eval") def func(*args): local_dict = dict(zip(varnames, args)) return eval(code, {**allowed_names, **local_dict}) return func try: # Parse function expression f = _parse_expr(func_expr, ["y", "x"]) # Parse gfun and hfun if isinstance(gfun_expr, str): gfun = _parse_expr(gfun_expr, ["x"]) else: gfun = lambda x: float(gfun_expr) if isinstance(hfun_expr, str): hfun = _parse_expr(hfun_expr, ["x"]) else: hfun = lambda x: float(hfun_expr) result, _ = scipy_dblquad(f, a, b, gfun, hfun, epsabs=epsabs, epsrel=epsrel) except Exception as e: return f"Error: {str(e)}" return round(result, 4)

Live Notebook

Edit this function in a live notebook.

Live Demo

Last updated on