What is the scipy.integrate.fixed_quad method?
Overview
SciPy is a package in Python that provides functions that can be used for numerical and mathematical computation. The integrate sub-package in SciPy implements general-purpose integration methods in Python. The scipy.integrate.fixed_quad method in SciPy computes a definite integral using a simple Gaussian quadrature of the fixed order n.
Syntax
scipy.integrate.fixed_quad(func, a, b, args=(), n=5)
Parameters
-
func: This is a Python function or method representing the function to be integrated using the Gaussian quadrature. The function should accept vector inputs. When integrating a vector-valued function, the returned array must have the shape(..., len(x)). -
a: It is the lower limit of integration. It has thefloattype. -
b: It is the upper limit of integration. It has thefloattype. -
args: This is an optional parameter which is a sequence of extra arguments passed tofunc. -
n: This is the Gaussian quadrature’s order. The default value is .
Return value
-
val: This is the computed Gaussian quadrature approximation to the integral. It has thefloattype. -
none: This is the statistically returned value ofNone.
Code
Let’s see how to compute the Gaussian quadrature approximation of order of the function using the scipy.integrate.fixed_quad method over the fixed interval .
import numpy as npfrom scipy import integrateprint(integrate.fixed_quad(np.sin, 0.0, np.pi/4, n=5))
Explanation
- Line 1 and 2: We import the
numpylibrary and theintegratesub-package from thescipylibrary. - Line 4: We use the
np.sinmethod in the NumPy library as our function to be integrated. We passnp.sinas the function and0andnp.pi/4as integration limits. is the order of the quadrature integration to thescipy.integrate.fixed_quad. We then print the result.