“Integration” is a great mathematical feature that allows us to calculate various vital quantities, such as volumes of solids, areas of surfaces, centroids of regions, and moments of inertia of bodies. The “scipy” library is a versatile tool for Python that enables various numerical and scientific computations. It has a powerful integration feature that lets users calculate the integral of functions with ease.
This Python tutorial presents a complete overview of the Scipy Nquad or “scipy.integrate.nquad()” function using numerous examples.
What is Scipy Nquad?
The “scipy.integrate.nquad()” is a function from the “scipy” library that is used to perform multiple integrations over the specified domain.
Syntax
Parameters
In the above syntax:
-
- The “func” parameter indicates the function that should be integrated. It should take a tuple of “n” arguments, where “n” is the number of dimensions, and return a single value.
- The “ranges” parameter indicates a list of “n” tuples specifying the lower and upper limits of integration for each dimension.
- The “opts” parameter (optional) specifies a dictionary of additional options.
- The “full_output” parameter returns the number of evaluations “neval” when we set the value of this parameter to “True”.
Return Value
This function retrieves a tuple containing two elements: the value of the integral and the absolute error estimate.
Example 1: Computing a Single Integral
The below code is used to compute the integral of the function “f(x)” over the range “[0, 2]”:
def example(x):
return x ** 2
print(spi.nquad(example, [[0, 2]]))
In the above code:
-
- The “scipy.integrate” module is imported.
- The user-defined function named “example()” is defined in the program. This function takes a parameter “x” and returns the square of that parameter (when passed as an argument).
- The “nquad()” function takes the “example()” function and a list representing the integration limits as its arguments, respectively, and returns the integral value along with the estimated absolute error.
Output
The integral of the given function has been calculated successfully.
Example 2: Computing a Double Integral
The following code snippet is used to determine a rectangular double integral:
def example(x, y):
return x * y
print(spi.nquad(example, [[0, 1], [0, 2]]))
According to the above code:
-
- Likewise, the “scipy.integrate” module is imported and the user-defined function named “example()” is defined.
- The user-defined function takes two parameters “x” and “y” and returns the multiplication of the passed arguments.
- The “nquad()” function takes the user-defined function and the integration limits as its arguments, respectively, and computes the double integral.
Output
The double integral of the given function has been calculated successfully.
Conclusion
The “scipy.integrate.nquad()” function of the “scipy” library is used to calculate the numerical integration of functions of any dimension. This Python post provided a detailed guide on the “scipy.integrate.nquad()” function by explaining the syntax, and parameters along with numerous examples that computed the single and double integral of the defined function.