numpy.trapz(): A Step-by-Step Guide to the Trapezoidal Rule

Featured Image (1)

In this article, we understand and implement numpy.trapz() which is a NumPy function. This function integrates along the given axis using the composite trapezoidal rule.

What is numpy.trapz()?

The numpy.trapz() is used to Integrate along the given axis using the composite trapezoidal rule. If x is provided, the integration happens in sequence along its elements.

What is the trapezoidal rule?

  • Area of trapezium = 1/2(sum of parallel sides)*height
  • The name trapezoidal is because when the area under the curve is evaluated, then the total area is divided into small trapezoids instead of rectangles
  • This rule is used for approximating the definite integrals where it uses the linear approximations of the functions.
  • To know more visit the trapezoidal rule.

Syntax

numpy.trapz(y, x = None, dx = 1.0, axis = -1)
ParameterDescriptionRequired/Optional
y:array_likeInput array to integrateRequired
x:array_likeThe sample points correspond to the y values. If x is None, the sample points are assumed to be evenly spaced dx apart. The default is None.Optional
dx: scalarThe spacing between sample points when x is None. The default is 1.Optional
axisThe axis along which to integrate.Optional
Numpy -trapz syntax paramter

Return value

Definite integral of y = n-dimensional array as approximated along a single axis by the trapezoidal rule. If y is a 1-dimensional array, then the result is a float. If n is greater than 1, then the result is an n-1 dimensional array.

Examples of numpy.trapz()

Importing numpy, declaring and printing x and y arrays.

import numpy as np
y = [1, 3, 5, 7]
x = [1, 5, 9, 13]
print(y)
print(x)

Output:

[1, 3, 5, 7]
[1, 5, 9, 13]

Example 1: Calculating the Area with numpy.trapz() and Default dx=1

print(np.trapz(y))

Here we have only provided argument y, therefore np.trapz() will take dx=1 as the default argument and x=None. We will get the trapezoidal area with x-axis points spaced with a difference of 1(dx=1).

  • Area of 1st trapezium = (1/2)*(1+3)*1 = 2
  • Area of 2nd trapezium = (1/2)*(3+5)*1 = 4
  • Area of 3rd trapezium = (1/2)*(5+7)*1= 6
  • Total area = 2+4+6 = 12

Output:

12.0

Example 2: numpy.trapz() with User-Defined x and dx Values

print(np.trapz(y,x))

Here we have also provided array x corresponding to array y, So here area of the trapeziums will be calculated with height as the difference between the 2 points on the x-axis.

  • Area of 1st trapezium = (1/2)*(1+3)*(5-1) = 8
  • Area of 2nd trapezium = (1/2)*(3+5)*(9-5) = 16
  • Area of 3rd trapezium = (1/2)*(5+7)*(13-9)= 24
  • Total area = 8+16+24 = 48

Output:

48.0

Example 3: Calculating the Area with numpy.trapz() and Custom dx Value

ans =np.trapz(y,dx=3)
print(ans)

Here we have defined dx=3 so every trapezium will have a height of h = 3 instead of 1(Example 1). Also x=None.

  • Area of 1st trapezium = (1/2)*(1+3)*3 = 6
  • Area of 2nd trapezium = (1/2)*(3+5)*3 = 12
  • Area of 3rd trapezium = (1/2)*(5+7)*3= 18
  • Total area = 6+12+18 = 36

Output:

36.0

Summary

In this article, We have introduced the trapezoidal rule, understood and implemented the numpy.trapz() function directly with default arguments, and also with different parameter values of x and dx parameters. numpy.trapz() is used to calculate the definite integral of the given function using the trapezoidal rule.

References

https://numpy.org/doc/stable/reference/generated/numpy.trapz.html