SciPy - integrate.nquad() Method



The SciPy integrate.nquad() method is used to find the integration of multiple variable. While creating the program it is necessary to mention the module as scipy.integrate.

Syntax

Following is the syntax of the SciPy integrate.nquad() method −

scipy.integrate.nquad(custom_func, [[0, 1], [0, 1]])

Parameters

This method accepts the following parameter −

  • custom_func: This parameter is defined for integration work in which it operate the task of integrals(upper limit and lower limit).
  • [0, 1]: This parameter is used to define the upper range limit.
  • [1, 0]: This parameter is used to define the lower range limit.
  • args = (a, b): This is an optional parameter if the user wants more variables to perform the integration work.

Return value

The method returns the result integration value of type float.

Example 1

Following is basic example of SciPy integrate.nquad() method that illustrate the integration over two variable.

import scipy.integrate

def integrand(x, y):
    return x**2 + y**2
    
# perform the nquad()
res, _ = scipy.integrate.nquad(integrand, [[0, 1], [0, 1]])
print("The double integral result is ", res)

Output

The above code produces the following output −

The double integral result is  0.6666666666666669

Example 2

Here, we perform the operation of integration over three variables(x, y, and z).

import scipy.integrate
def integrand(x, y, z):
    return x + y + z
    
# perform the nquad()
res, _ = scipy.integrate.nquad(integrand, [[0, 1], [0, 1], [0, 1]])
print("The triple integral result is ", res)

Output

The above code produces the following output −

The triple integral result is  1.5

Example 3

Below the program calculate the integration result using multiple variable(x, y, a, and b).

import scipy.integrate
def integrand(x, y, a, b):
    return a * x + b * y

a = 2
b = 3

# perform the nquad()
res, _ = scipy.integrate.nquad(integrand, [[0, 1], [0, 1]], args=(a, b))

# display the result
print("The double integral with parameter result is ", res)

Output

The above code produces the following output −

The double integral with parameter result is  2.5
scipy_reference.htm
Advertisements