{ } Raw JSON

bundles / scipy 1.17.1 / scipy / integrate / _quadrature / newton_cotes

function

scipy.integrate._quadrature:newton_cotes

source: /scipy/integrate/_quadrature.py :986

Signature

def   newton_cotes ( rn equal = 0 )

Summary

Return weights and error coefficient for Newton-Cotes integration.

Extended Summary

Suppose we have samples of at the positions . Then an -point Newton-Cotes formula for the integral between and is:

where and is the average samples spacing.

If the samples are equally-spaced and N is even, then the error term is .

Parameters

rn : int

The integer order for equally-spaced data or the relative positions of the samples with the first sample at 0 and the last at N, where N+1 is the length of rn. N is the order of the Newton-Cotes integration.

equal : int, optional

Set to 1 to enforce equally spaced data.

Returns

an : ndarray

1-D array of weights to apply to the function at the provided sample positions.

B : float

Error coefficient.

Notes

Normally, the Newton-Cotes rules are used on smaller integration regions and a composite rule is used to return the total integral.

Array API Standard Support

newton_cotes has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.

====================  ====================  ====================
Library               CPU                   GPU
====================  ====================  ====================
NumPy                 ✅                     n/a                 
CuPy                  n/a                   ⛔                   
PyTorch               ⛔                     ⛔                   
JAX                   ⛔                     ⛔                   
Dask                  ⛔                     n/a                 
====================  ====================  ====================

See dev-arrayapi for more information.

Examples

Compute the integral of sin(x) in [0, :math:`\pi`]:
from scipy.integrate import newton_cotes
import numpy as np
def f(x):
    return np.sin(x)
a = 0
b = np.pi
exact = 2
for N in [2, 4, 6, 8, 10]:
    x = np.linspace(a, b, N + 1)
    an, B = newton_cotes(N, 1)
    dx = (b - a) / N
    quad = dx * np.sum(an * f(x))
    error = abs(quad - exact)
    print('{:2d}  {:10.9f}  {:.5e}'.format(N, quad, error))

Aliases

  • scipy.integrate.newton_cotes