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.

Answers Code

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))
Implementation of scipy.integrate.dblquad()

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

RELATED TAGS

python
scipy
communitycreator
integration

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.

Answers Code
Keep Exploring