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 the float type.

  • b: It is the upper limit of integration. It has the float type.

  • args: This is an optional parameter which is a sequence of extra arguments passed to func.

  • n: This is the Gaussian quadrature’s order. The default value is 55.

Return value

  • val: This is the computed Gaussian quadrature approximation to the integral. It has the float type.

  • none: This is the statistically returned value of None.

Code

Let’s see how to compute the Gaussian quadrature approximation of order 55 of the function f(x)=sin(x)f(x) = sin(x) using the scipy.integrate.fixed_quad method over the fixed interval 0π/40- \pi/4.

import numpy as np
from scipy import integrate
print(integrate.fixed_quad(np.sin, 0.0, np.pi/4, n=5))

Explanation

  • Line 1 and 2: We import the numpy library and the integrate sub-package from the scipy library.
  • Line 4: We use the np.sin method in the NumPy library as our function to be integrated. We pass np.sin as the function and 0 and np.pi/4 as integration limits. 55 is the order of the quadrature integration to the scipy.integrate.fixed_quad. We then print the result.

Free Resources