What is the scipy.integrate.trapz method?

The function f(x) (in blue) is approximated by a linear function (in red) using the trapezoidal rule

Overview

SciPy is an open-source library provided by Python dedicated to scientific computation. The scipy.integrate is a sub-package that provides the functionality to solve several integration methods, including Ordinary Differential Equations (ODEs). The scipy.integrate.trapz method in SciPy is used to compute a definite integral using a simple Gaussian quadrature of fixed order n.

Syntax

scipy.integrate.fixed_quad(func, a, b, args=(), n=5)

Paramters

  • y: The input array that needs to be integrated. It should be a Python array-like object.

  • x: The sample points corresponding to the yy values. It should be a Python array-like object. The default value is None. If x is not given, the sample points are assumed to be evenly spaced apart by dx.

  • dx: An optional scaler representing the spacing between sample points when x is None. The default value is 1.

  • axis: The axis along which to integrate is an optional number.

Return Value

**trapz**: The computed definite integral as approximated by the trapezoidal rule. It is a float.

Example

from scipy import integrate
import numpy as np
print(integrate.trapz([0,1,2]))

Explanation

We pass the array [0,1,2] to the scipy.integrate.trapz method, which integrates this array using the trapezoidal rule.

Free Resources