newton_cotes
- scipy.integrate.newton_cotes(rn, equal=0)
Return weights and error coefficient for Newton-Cotes integration.
Suppose we have samples of at the positions . Then an -point Newton-Cotes formula for the integral between and is:
Return weights and error coefficient for Newton-Cotes integration.
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 .
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.
Set to 1 to enforce equally spaced data.
1-D array of weights to apply to the function at the provided sample positions.
Error coefficient.
Normally, the Newton-Cotes rules are used on smaller integration regions and a composite rule is used to return the total integral.
Compute the integral of sin(x) in [0, ]:
>>> 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))
...
2 2.094395102 9.43951e-02
4 1.998570732 1.42927e-03
6 2.000017814 1.78136e-05
8 1.999999835 1.64725e-07
10 2.000000001 1.14677e-09