Sometimes a function is very complicated to integrate or cannot be integrated analytically; then, it can be solved by the numerical integration method. SciPy provides some routines for performing numerical integration. The scipy.integrate library contains most of these functions.
- Single Integrals
Numerical integrate is sometimes called quadrature. The Quad function is essential for SciPy’s integration functions. The syntax of the quad() function is the following:
f – Function name to be integrate
a-It is a lower limit.
b- It is a upper limit.
Let’s consider the Gaussian function, integrated over a range from a to b. We define the functionf(x)= e-x2, this can be done using a lambda expression and apply the quad method on the given function.
In the above program, we have used the quad() function that returns the two values. The first value is the integral, and the second value is an estimate of the absolute error in the value of an integer.
Note: Since quad() function requires function as the first argument, we cannot directly pass expression as the argument. It allows positive and negative infinity as limits.
The multiple integral such as double and triple integration conclude into the functions dblquad(), tplquad(), and nquad(). Here we consider the double integral problem to solve using the scipy.integrate.dblquad(func,a,b,gfun,hfun). The first argument func is the name of the function to be integrated and a and b are the lower and upper limit of the x variable. While gfun and hfun are names of the functions that define the lower and upper limit of the y variable. Let’s consider the following example:
The scipy.integarte contains the number of other integration functions, including nquad(), which perform n-fold multiple integrations.