In the Numpy module, we have discussed many functions used to operate on the multidimensional array. In this tutorial, we will discuss the concept of the numpy trapz() function used to integrate along the given axis using the composite trapezoidal rule.
What is numpy trapz() function?
The numpy trapz() function integrates along the given axis using the composite trapezoidal rule.
Syntax numpy trapz()
numpy.trapz(y, x = None, dx = 1.0, axis = -1)
Parameters
- y: It is an input array that is to be integrated.
- x: it is also array-like, which is optional as an input. The sample points correspond to the y values. If x is set to None, the sample points are assumed to be evenly spaced dx apart. By default, it is None.
- dx: It is the scalar value, which is also an optional input. It is the spacing between sample points when x is None. by default. It is 1.
- axis: It is an integer value and an optional input. It is the axis along which we have to integrate.
Return value
The return value is the float value and the definite integral as approximated by the trapezoidal rule.
Examples of numpy trapz() function
Here, we will discuss how to write the trapz() function from the numpy library.
1. Taking only y as a parameter
We will take y as an array-like input parameter in this example and see the output. Let us look at the example for understanding the concept in detail.
#taking y as a parameter
#import numpy library
import numpy as np
y = [3,4,5,6]
output = np.trapz(y)
print("Output : ",output)
Output:
Output : 13.5
Explanation:
- Firstly, we will import the numpy library with an alias named np.
- Then, we will take an array as y.
- Then, we will apply the trapz() function with y as a parameter and store the output in the output variable.
- At last, we will print the output.
- Hence, you can see the output is in float value.
2. Taking x and y as parameter
We will take x and y as array-like input in this example and perform the trapz() function. Hence, we will see the output. Let us look at the example for understanding the concept in detail.
#taking x and y as a parameter
#import numpy library
import numpy as np
y = [3,4,5,6]
x = [1,2,3,4]
output = np.trapz(y,x)
print("Output : ",output)
Output:
Output : 13.5
Explanation:
- Firstly, we will import the numpy library with an alias named np.
- Then, we will take an array as y.
- After that, we will take an array as x.
- Then, we will apply the trapz() function with y and x as a parameter and store the output in the output variable.
- At last, we will print the output.
- Hence, you can see the output is in float value.
3. Taking y and dx as a parameter
In this example, we will take x as an array-like input and dx = 2 and perform the trapz() function. Hence, we will see the output. Let us look at the example for understanding the concept in detail.
#taking dx and y as a parameter
#import numpy library
import numpy as np
y = [3,4,5,6]
output = np.trapz(y,dx = 2)
print("Output : ",output)
Output:
Output : 27.0
Explanation:
- Firstly, we will import the numpy library with an alias named np.
- Then, we will take an array as y.
- Then, we will apply the trapz() function with y and dx = 2 as a parameter and store the output in the output variable.
- At last, we will print the output.
- Hence, you can see the output is in float value.
4. Taking multidimensional array and axis as a parameter
In this example, we will take input as a multidimensional array with the help of arange() function in the numpy library. Then, we will set the axes as 0 and 1 and check the output on both axes. Hence, you can see the output. Let us look at the example for understanding the concept in detail.
#taking dx and y as a parameter
#import numpy library
import numpy as np
a = np.arange(6).reshape(2, 3)
print(a)
print("\n")
output = np.trapz(a,axis = 0)
out = np.trapz(a,axis = 1)
print("Output with axis = 0 : ",output)
print("\n")
print("Output with axis = 1 : ",out)
Output:
[[0 1 2]
[3 4 5]]
Output with axis = 0 : [1.5 2.5 3.5]
Output with axis = 1 : [2. 8.]
Explanation:
- Firstly, we will import the numpy library with an alias named np.
- Then, we will apply arange() function from the numpy library to take the input in a multidimensional array.
- Then, we will print the array that we have taken as input.
- We will then apply the trapz() function with a and axis = 0 and axis =1 as the parameter and store the output in the output and out variable.
- Finally, we have printed both outputs by applying ” \n” to leave a gap of the single line between both outputs.
- Hence, you can see the output.
Faster alternative for Numpy trapz() function
The faster alternative for numpy trapz() function simps() function from scipy. simps() function is used to get the integration of y(x) using samples along the axis and composite Simpson’s rule by using scipy.integrate.simps()
method.
Let us look at the example for understanding the concept in detail.
import numpy as np
from scipy.integrate import simps
arr = np.array([5, 20, 4, 18, 20, 18, 3, 4])
output = simps(arr, dx=5)
print("output =", output)
Output:
output = 443.333333333
Conclusion
In this tutorial, we have discussed the concept of the numpy trapz() function. We have discussed what the numpy trapz() function is and when we use it. We have discussed how to use the numpy trapz() function from the numpy library, with examples explained in detail. You can use any of the functions according to your choice and your requirements in the program.
However, please let me know if you have any questions in the comment section below. I will try to help you as soon as possible.
What if you have a function, such as sin(x) that you would like to integrate? Thank you so much, I appreciate this tutorial!
np.trapz(np.sin(x), x)
This would help.