NumPy trapz()

The trapz() function computes the definite integral of a given array using the trapezoidal rule. It approximates the area under the curve defined by the input array using a series of trapezoids.

Example

import numpy as np

# create an array of y-coordinates
y = np.array([1, 2, 3, 4, 5])

# compute the definite integral using numpy.trapz()
area = np.trapz(y)

print(area)

# Output: 12

trapz() Syntax

The syntax of trapz() is:

numpy.trapz(y, x = None, dx = 1.0, axis = -1)

trapz() Arguments

The trapz() function takes following arguments:

  • y - input array containing the y-coordinates of the curve
  • x (optional) - input array containing the x-coordinates of the curve
  • dx (optional) - the spacing between the x-coordinates
  • axis (optional) - the axis along which the integration is performed

trapz() Return Value

The numpy.trapz() function returns the approximate definite integral of the input array using the trapezoidal rule.


Example 1: Compute Definite Integral Using np.trapz()

import numpy as np

# create an array of y-coordinates
y = np.array([2, 5, 7, 3, 6, 9, 1])

# compute the definite integral using numpy.trapz() area = np.trapz(y)
print("Area under the curve:", area)

Output

Area under the curve: 31.5

In the above example, we have the y array representing the y-coordinates of a curve.

The np.trapz() function is used to calculate the definite integral of the curve, approximating the area under the curve using the trapezoidal rule.

The resulting area is stored in the area variable and then printed, which in this case is 31.5.


Example 2: Use of x and dx Argument in trapz()

import numpy as np

# create an array of y-coordinates
y = np.array([1, 2, 3, 4, 5])

# create an array of x-coordinates
x = np.array([0, 1, 2, 3, 4])

# specify the spacing between x-coordinates
dx = 0.5

# compute the definite integral using numpy.trapz() with optional arguments
area = np.trapz(y, x=x, dx=dx)

print("Area under the curve:", area)

Output

Area under the curve: 12.0

Here, the x array is provided to specify the x-coordinates, and the dx argument is used to specify the spacing between the x-coordinates.

By providing x and dx argument, we can compute the definite integral using non-equally spaced x-coordinates and a specific spacing between the x-coordinates.


Example 3: trapz() With 2-D Array

The axis argument defines how we can compute definite integrals of elements in a 2-D array.

  • If axis = None, the array is flattened and the definite integral of the flattened array is computed.
  • If axis = 0, the definite integral is calculated column-wise.
  • If axis = 1, the definite integral is calculated row-wise.

Let's see an example.

import numpy as np

# create a 2-D array
array1 = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

# calculate the definite integral of the flattened array result1 = np.trapz(array1.flatten())
print('Definite integral of the flattened array:', result1)
# calculate the definite integral column-wise (axis=0) result2 = np.trapz(array1, axis=0)
print('\nDefinite integrals column-wise (axis=0):') print(result2)
# calculate the definite integral row-wise (axis=1) result3 = np.trapz(array1, axis=1)
print('\nDefinite integrals row-wise (axis=1):') print(result3)

Output

Definite integral of the flattened array: 40.0

Definite integrals column-wise (axis=0):
[ 8. 10. 12.]

Definite integrals row-wise (axis=1):
[ 4. 10. 16.]

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges