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 xx and yy, it takes yy as the first argument, and xx as the second argument.

  • a,b: These are the integration limits in xx where a<ba<b. Both have the type float.

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

  • hfun: This is a Python function or float that represents the higher boundary curve in yy, which is a function of xx. 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.49e81.49e-8.

  • epsrel: This is an optional parameter that is the relative tolerance of the inner 1-D integrals. Its default is 1.49e81.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)=xy4f(y,x) = xy^4 using the scipy.integrate.dblquad method, where xx ranges from 00 to 22, and yy ranges from 00 to 11.

from scipy import integrate
func = lambda y, x: x*y**4
print(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 xx is from 00 to 22 and yy is from 00 to 11.

Note: We write the constant boundary curve for yy as a lambda function of xx

Free Resources