`numpy.trapz()`

methodThe ** numpy.trapz() method** is used to compute integration along a specified axis using the composite trapezoidal rule.

Note:A two-dimensional (2D) array can be created in Python using a list of lists.

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

: This denotes the array of inputs to be integrated.`y`

: This is array-like, but not required. It reflects the`x`

`y`

values’ sample points. The sample points are considered to be evenly spread at a distance of`dx`

if`x`

is set to None. The default is None.: This is an optional scalar variable. When`dx`

`x`

is None, it reflects the distance between samples. The default value is`1`

.: This is the`axis`

`int`

value and optional. It represents the integration axis.

The `numpy.trapz()`

method returns a definite integral estimated by the trapezoidal rule. It can be a float or `ndarray`

.

The following code shows how to use the `numpy.trapz()`

method for two-dimensional (2D) arrays.

# import numpyimport numpy as np# create listx1 = [7,3,4,8]x2 = [2,6,9,5]# convert the lists to 2D array using np.arrayy = np.array([x1,x2])# compute the integration along a specified axis# and store the result in resultresult = np.trapz(y, dx=2)print(result)

- Line 2: We import the
`numpy`

library. - Lines 4–5: We create two separate lists,
`x1`

and`x2`

. - Line 7: We utilize the
`np.array()`

method to convert the lists into a 2D array. We save in a new variable called`y`

. - Line 11: We use
`np.trapz()`

to compute the integration along a specified axis. The result is stored in a new variable called`result`

. - Line 13: We display the result.

TRENDING TOPICS