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:
where:
- is the function to integrate.
- and are the limits of integration for .
- and are the lower and upper boundary curves for , 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 to integrate. Usex
andy
as variables. For example,"x * y^2"
.a
(float, required): The lower limit of integration for .b
(float, required): The upper limit of integration for .gfun_expr
(str or float, required): Expression for the lower boundary of as a function of , or a constant. For example,"0"
,"x"
, or0
.hfun_expr
(str or float, required): Expression for the upper boundary of as a function of , or a constant. For example,"1"
,"2-x"
, or1
.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 over the region from 0 to 2 and from 0 to 1.
Inputs:
func_expr | a | b | gfun_expr | hfun_expr | epsabs | epsrel |
---|---|---|---|---|---|---|
x * y^2 | 0 | 2 | 0 | 1 | 1.49e-8 | 1.49e-8 |
Excel formula:
=DBLQUAD("x * y^2", 0, 2, 0, 1)
Expected output:
Result |
---|
0.6667 |
This means the integral of over the region is 0.6667.
Example 2: Curved Boundaries
This example computes the double integral of over the region from 0 to , from to .
Inputs:
func_expr | a | b | gfun_expr | hfun_expr | epsabs | epsrel |
---|---|---|---|---|---|---|
1 | 0 | 0.7853981633974483 | sin(x) | cos(x) | 1.49e-8 | 1.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 .
Inputs:
func_expr | a | b | gfun_expr | hfun_expr | epsabs | epsrel |
---|---|---|---|---|---|---|
exp(-(x^2 + y^2)) | -1e10 | 1e10 | -1e10 | 1e10 | 1.49e-8 | 1.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 over the region from 0 to 1 and from to .
Inputs:
func_expr | a | b | gfun_expr | hfun_expr | epsabs | epsrel |
---|---|---|---|---|---|---|
3 * x * y | 0 | 1 | x | 2-x | 1.49e-8 | 1.49e-8 |
Excel formula:
=DBLQUAD("3 * X * Y", 0, 1, "X", "2-X")
Expected output:
Result |
---|
1.0000 |
This means the integral of 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 .