r""" Integrate along the given axis using the composite trapezoidal rule. If `x` is provided, the integration happens in sequence along its elements - they are not sorted. Integrate `y` (`x`) along each 1d slice on the given axis, compute :math:`\int y(x) dx`. When `x` is s
(y, x=None, dx=1.0, axis=-1)
| 4857 | |
| 4858 | @array_function_dispatch(_trapz_dispatcher) |
| 4859 | def trapz(y, x=None, dx=1.0, axis=-1): |
| 4860 | r""" |
| 4861 | Integrate along the given axis using the composite trapezoidal rule. |
| 4862 | |
| 4863 | If `x` is provided, the integration happens in sequence along its |
| 4864 | elements - they are not sorted. |
| 4865 | |
| 4866 | Integrate `y` (`x`) along each 1d slice on the given axis, compute |
| 4867 | :math:`\int y(x) dx`. |
| 4868 | When `x` is specified, this integrates along the parametric curve, |
| 4869 | computing :math:`\int_t y(t) dt = |
| 4870 | \int_t y(t) \left.\frac{dx}{dt}\right|_{x=x(t)} dt`. |
| 4871 | |
| 4872 | Parameters |
| 4873 | ---------- |
| 4874 | y : array_like |
| 4875 | Input array to integrate. |
| 4876 | x : array_like, optional |
| 4877 | The sample points corresponding to the `y` values. If `x` is None, |
| 4878 | the sample points are assumed to be evenly spaced `dx` apart. The |
| 4879 | default is None. |
| 4880 | dx : scalar, optional |
| 4881 | The spacing between sample points when `x` is None. The default is 1. |
| 4882 | axis : int, optional |
| 4883 | The axis along which to integrate. |
| 4884 | |
| 4885 | Returns |
| 4886 | ------- |
| 4887 | trapz : float or ndarray |
| 4888 | Definite integral of `y` = n-dimensional array as approximated along |
| 4889 | a single axis by the trapezoidal rule. If `y` is a 1-dimensional array, |
| 4890 | then the result is a float. If `n` is greater than 1, then the result |
| 4891 | is an `n`-1 dimensional array. |
| 4892 | |
| 4893 | See Also |
| 4894 | -------- |
| 4895 | sum, cumsum |
| 4896 | |
| 4897 | Notes |
| 4898 | ----- |
| 4899 | Image [2]_ illustrates trapezoidal rule -- y-axis locations of points |
| 4900 | will be taken from `y` array, by default x-axis distances between |
| 4901 | points will be 1.0, alternatively they can be provided with `x` array |
| 4902 | or with `dx` scalar. Return value will be equal to combined area under |
| 4903 | the red lines. |
| 4904 | |
| 4905 | |
| 4906 | References |
| 4907 | ---------- |
| 4908 | .. [1] Wikipedia page: https://en.wikipedia.org/wiki/Trapezoidal_rule |
| 4909 | |
| 4910 | .. [2] Illustration image: |
| 4911 | https://en.wikipedia.org/wiki/File:Composite_trapezoidal_rule_illustration.png |
| 4912 | |
| 4913 | Examples |
| 4914 | -------- |
| 4915 | Use the trapezoidal rule on evenly spaced points: |
| 4916 |