What is the scipy.integrate.quadrature() method in Python?
Overview
The scipy.integrate.quadrature() method in Python computes a definite integral using the numerical integration method of the fixed-tolerance Gaussian quadrature. The method takes a Python function, limits of integration, and absolute tolerance value as arguments and calculates the definite integral using the Gaussian quadrature.
Syntax
The function prototype is as follows:
scipy.integrate.quadrature(func, a, b, args = (),tol = 1.49e-08,rtol = 1.49e-08,maxiter = 50,vec_func = True,miniter = 1)
Parameters
-
func: This is the Python function or the method that is being integrated. -
a: This is a float value that specifies the lower limit of integration. -
args: This is an optionaltupleparameter. It specifies the extra arguments to be passed to thefuncfunction. -
tol, rtol: This is an optional float value that defines the absolute tolerance.Note: Iteration stops when the error between the last two iterates is less than
tolor the relative change is less thanrtol. Its default value is1.49e-08. -
maxiter: This is an optional int value that specifies the maximum order of the Gaussian quadrature. -
miniter: This is an optional int value that specifies the minimum order of the Gaussian quadrature. -
vec_func: This is an optional bool value that specifiesTrueorFalse, iffunchandles arrays as arguments. The default value isTrue.
Return value
-
val: This float value computes the Gaussian quadrature approximation to the integral within the absolute tolerance. -
err: This float value tells the difference between the last two estimates of the integral.
Code
from scipy import integrateimport numpy as npprint(integrate.quadrature(np.sin, 0.0, np.pi/4))
Explanation
-
Line 1–2: We import the required libraries.
-
Line 4: We use the sine function,
np.sin, from the NumPy library and pass it to theintegrate.quadraturemethod. The method integrates it using the Gaussian quadrature method with integration limits from to .