Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
scipy
communitycreator
integration

# What is the scipy.integrate.dblquad method in Python? Abdul Monum

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.   ### 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 $x$ and $y$, it takes $y$ as the first argument, and $x$ as the second argument.

• a,b: These are the integration limits in $x$ where $a. Both have the type float.

• gfun: This is a Python function or float that represents the lower boundary curve in $y$, which is a function of $x$. It returns a single floating-point value.

• hfun: This is a Python function or float that represents the higher boundary curve in $y$, which is a function of $x$. It returns a single floating-point value.

• args: This is an optional parameter that is a sequence of extra arguments passed to func.

• epsabs: This is an optional parameter that is the absolute tolerance passed directly to the inner 1-D quadrature integration. Its default is $1.49e-8$.

• epsrel: This is an optional parameter that is the relative tolerance of the inner 1-D integrals. Its default is $1.49e-8$.

### 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 $f(y,x) = xy^4$ using the scipy.integrate.dblquad method, where $x$ ranges from $0$ to $2$, and $y$ ranges from $0$ to $1$.

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 integrate from scipy.
• Line 2: We write the lambda function func. It takes two inputs where y is the first argument and x is the second argument.

• Line 3: We use the scipy.integrate.dblquad method. It returns the double (definite) integral of func over the region, where $x$ is from $0$ to $2$ and $y$ is from $0$ to $1$.

Note: We write the constant boundary curve for $y$ as a lambda function of $x$

RELATED TAGS

python
scipy
communitycreator
integration

CONTRIBUTOR Abdul Monum

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.   Keep Exploring

Learn in-demand tech skills in half the time 