xrscipy.integrate.simpson

xrscipy.integrate.simpson(obj, coord, even='avg')

Integrate y(x) using samples along the given coordinate and the composite

Simpson’s rule. If x is None, spacing of dx is assumed.

If there are an even number of samples, N, then there are an odd number of intervals (N-1), but Simpson’s rule requires an even number of intervals. The parameter ‘even’ controls how this is handled.

Parameters:
  • obj (xarray object) – Array to be integrated.

  • coord (string) – The coordinate along which to integrate.

  • even ({None, 'simpson', 'avg', 'first', 'last'}, optional) –

    ‘avg’Average two results:
    1. use the first N-2 intervals with a trapezoidal rule on the last interval and

    2. use the last N-2 intervals with a trapezoidal rule on the first interval.

    ’first’Use Simpson’s rule for the first N-2 intervals with

    a trapezoidal rule on the last interval.

    ’last’Use Simpson’s rule for the last N-2 intervals with a

    trapezoidal rule on the first interval.

    None : equivalent to ‘simpson’ (default)

    ’simpson’Use Simpson’s rule for the first N-2 intervals with the

    addition of a 3-point parabolic segment for the last interval using equations outlined by Cartwright [1]. If the axis to be integrated over only has two points then the integration falls back to a trapezoidal integration.

    New in version 1.11.0.

    Changed in version 1.11.0: The newly added ‘simpson’ option is now the default as it is more accurate in most situations.

    Deprecated since version 1.11.0: Parameter even is deprecated and will be removed in SciPy 1.13.0. After this time the behaviour for an even number of points will follow that of even=’simpson’.

Returns:

The estimated integral computed with the composite Simpson’s rule.

Return type:

float

See also

quad

adaptive quadrature using QUADPACK

romberg

adaptive Romberg quadrature

quadrature

adaptive Gaussian quadrature

fixed_quad

fixed-order Gaussian quadrature

dblquad

double integrals

tplquad

triple integrals

romb

integrators for sampled data

cumulative_trapezoid

cumulative integration for sampled data

ode

ODE integrators

odeint

ODE integrators

scipy.integrate.simpson

scipy.integrate.simpson : Original scipy implementation

Notes

For an odd number of samples that are equally spaced the result is exact if the function is a polynomial of order 3 or less. If the samples are not equally spaced, then the result is exact only if the function is a polynomial of order 2 or less.

References

Examples

>>> from scipy import integrate
>>> import numpy as np
>>> x = np.arange(0, 10)
>>> y = np.arange(0, 10)

Examples

>>> integrate.simpson(y, x)
40.5

Examples

>>> y = np.power(x, 3)
>>> integrate.simpson(y, x)
1640.5

Examples

>>> integrate.quad(lambda x: x**3, 0, 9)[0]
1640.25

Examples

>>> integrate.simpson(y, x, even='first')
1644.5