What is the scipy.integrate.dblquad method in Python?
Overview
SciPy is an open-source library provided by Python that is dedicated to scientific computation.
scipy.integrate is a sub-package in SciPy that provides the functionality to solve several integration methods, including Ordinary Differential Equations (ODEs).
We can use the scipy.integrate.dblquad method in SciPy to solve general-purpose double integration.
Syntax
scipy.integrate.dblquad(func, a, b, gfun, hfun, args=(), epsabs=1.49e-08, epsrel=1.49e-08)
Parameters
-
func: This is a Python function or method that represents a double integral function. It takes at least two inputs that acts as variables to the double integral. Given the variables and , it takes as the first argument, and as the second argument. -
a,b: These are the integration limits in where . Both have the typefloat. -
gfun: This is a Python function orfloatthat represents the lower boundary curve in , which is a function of . It returns a single floating-point value. -
hfun: This is a Python function orfloatthat represents the higher boundary curve in , which is a function of . It returns a single floating-point value. -
args: This is an optional parameter that is a sequence of extra arguments passed tofunc. -
epsabs: This is an optional parameter that is the absolute tolerance passed directly to the inner 1-D quadrature integration. Its default is . -
epsrel: This is an optional parameter that is the relative tolerance of the inner 1-D integrals. Its default is .
Return value
y: It is the computed double definite integral of func(y,x). It has the type float.
abserr: It is an estimate of the error. It has the type float.
Example
Let’s see how to compute the double intergal of using the scipy.integrate.dblquad method, where ranges from to , and ranges from to .
from scipy import integratefunc = lambda y, x: x*y**4print(integrate.dblquad(func, 0, 2, lambda x: 0, lambda x: 1))
Explanation
- Line 1: We import
integratefromscipy.
-
Line 2: We write the lambda function
func. It takes two inputs whereyis the first argument andxis the second argument. -
Line 3: We use the
scipy.integrate.dblquadmethod. It returns the double (definite) integral offuncover the region, where is from to and is from to .
Note: We write the constant boundary curve for as a lambda function of