from pylab import *
from scipy.integrate import newton_cotes
The Trapezoidal and Simpson rules were based on integrating a polynomial approximation. Such methods are called Newton-Cotes integration methods. If we choose n + 1 n+1 n + 1 distinct points in [ a , b ] [a,b] [ a , b ] and approximate the function f : [ a , b ] → R f : [a,b] \to \re f : [ a , b ] → R by interpolation
p n ( x ) = ∑ j = 0 n f ( x j , n ) ℓ j , n ( x ) , x ∈ [ a , b ] p_n(x) = \sum_{j=0}^n f(x_{j,n}) \ell_{j,n}(x), \qquad x \in [a,b] p n ( x ) = j = 0 ∑ n f ( x j , n ) ℓ j , n ( x ) , x ∈ [ a , b ] the integral can be approximated by
I n ( f ) = ∫ a b p n ( x ) d x = ∑ j = 0 n f ( x j , n ) ∫ a b ℓ j , n ( x ) d x = ∑ j = 0 n w j , n f ( x j , n ) I_n(f) = \int_a^b p_n(x) \ud x = \sum_{j=0}^n f(x_{j,n}) \clr{red}{ \int_a^b \ell_{j,n}(x) \ud x } = \sum_{j=0}^n \clr{red}{w_{j,n}} f(x_{j,n}) I n ( f ) = ∫ a b p n ( x ) d x = j = 0 ∑ n f ( x j , n ) ∫ a b ℓ j , n ( x ) d x = j = 0 ∑ n w j , n f ( x j , n ) where the weights are given by
w j , n = ∫ a b ℓ j , n ( x ) d x w_{j,n} = \int_a^b \ell_{j,n}(x) \ud x w j , n = ∫ a b ℓ j , n ( x ) d x As an example for n = 3 n=3 n = 3 and using uniformly spaced points, we interpolate by a cubic polynomial and the integral is approximated as
I 3 ( f ) = 3 h 8 [ f 0 + 3 f 1 + 3 f 2 + f 3 ] I_3(f) = \frac{3h}{8}[f_0 + 3 f_1 + 3 f_2 + f_3] I 3 ( f ) = 8 3 h [ f 0 + 3 f 1 + 3 f 2 + f 3 ] For general n n n , we can state the following error estimates.
(a) For n n n even, f ∈ C n + 2 [ a , b ] f \in \cts^{n+2}[a,b] f ∈ C n + 2 [ a , b ]
I ( f ) − I n ( f ) = C n h n + 3 f ( n + 2 ) ( η ) , η ∈ [ a , b ] I(f) - I_n(f) = C_n h^{n+3} f^{(n+2)}(\eta), \qquad \eta \in [a,b] I ( f ) − I n ( f ) = C n h n + 3 f ( n + 2 ) ( η ) , η ∈ [ a , b ] C n = 1 ( n + 2 ) ! ∫ 0 n μ 2 ( μ − 1 ) … ( μ − n ) d μ C_n = \frac{1}{(n+2)!} \int_0^n \mu^2 (\mu-1) \ldots (\mu-n) \ud \mu C n = ( n + 2 )! 1 ∫ 0 n μ 2 ( μ − 1 ) … ( μ − n ) d μ (b) For n n n odd and f ∈ C n + 1 [ a , b ] f \in \cts^{n+1}[a,b] f ∈ C n + 1 [ a , b ]
I ( f ) − I n ( f ) = C n h n + 2 f ( n + 1 ) ( η ) , η ∈ [ a , b ] I(f) - I_n(f) = C_n h^{n+2} f^{(n+1)}(\eta), \qquad \eta \in [a,b] I ( f ) − I n ( f ) = C n h n + 2 f ( n + 1 ) ( η ) , η ∈ [ a , b ] C n = 1 ( n + 1 ) ! ∫ 0 n μ ( μ − 1 ) … ( μ − n ) d μ C_n = \frac{1}{(n+1)!} \int_0^n \mu (\mu-1) \ldots (\mu-n) \ud \mu C n = ( n + 1 )! 1 ∫ 0 n μ ( μ − 1 ) … ( μ − n ) d μ The accuracy of a quadrature formula can be characterized by the largest set of polynomials which it can integrate exactly.
Trapezoidal rule (n = 1 n=1 n = 1 ) has degree of precision one and Simpson rule (n = 2 n=2 n = 2 ) has degree of precision three. For n n n odd, Newton-Cotes has degree of precision of n n n while for n n n even, it has degree of precision n + 1 n+1 n + 1 . On uniformly spaced nodes, the interpolating polynomial p n ( x ) p_n(x) p n ( x ) does not always converge. Hence the Newton-Cotes formulae may not converge also. Consider
I = ∫ − 4 4 d x 1 + x 2 = 2 tan − 1 ( 4 ) ≈ 2.6516 I = \int_{-4}^4 \frac{\ud x}{1 + x^2} = 2 \tan^{-1}(4) \approx 2.6516 I = ∫ − 4 4 1 + x 2 d x = 2 tan − 1 ( 4 ) ≈ 2.6516 xmin, xmax = -4.0, 4.0
f = lambda x: 1/(1 + x**2)
exact = 2.0 * arctan(4.0)
for n in arange(2,12,2):
w,B = newton_cotes(n, equal=1)
x = linspace(xmin,xmax,n+1)
dx = (xmax-xmin)/n
quad = dx * sum(w * f(x))
error = abs(quad - exact)
print('{:4d} {:12.9f} {:12.5e}'.format(n, quad, error))
2 5.490196078 2.83856e+00
4 2.277647059 3.73988e-01
6 3.328798127 6.77163e-01
8 1.941094304 7.10541e-01
10 3.595560400 9.43925e-01
The error shown in last column is increasing.
Hence it is not advisable to use Newton-Cotes rule for large degree n n n . However, we can use moderate but fixed degree of the interpolating polynomial and build a composite rule. The convergence here is obtained as the length of the sub-intervals tends to zero but the polynomial degree in each sub-interval remains fixed.
We now look at necessary and sufficient conditions for a numerical integration formula to converge.
Let F \mathcal{F} F be a set of continuous functions on a given interval [ a , b ] [a,b] [ a , b ] . We say that F \mathcal{F} F is dense in C [ a , b ] \cts[a,b] C [ a , b ] if for every f ∈ C [ a , b ] f \in \cts[a,b] f ∈ C [ a , b ] and every ϵ > 0 \epsilon > 0 ϵ > 0 , ∃ f ϵ ∈ F \exists f_\epsilon \in \mathcal{F} ∃ f ϵ ∈ F such that
∥ f − f ϵ ∥ ∞ ≤ ϵ \norm{f - f_\epsilon}_\infty \le \epsilon ∥ f − f ϵ ∥ ∞ ≤ ϵ (1) By Weirstrass theorem, the set of all polynomials is dense in C [ a , b ] \cts[a,b] C [ a , b ] .
(2) Define the set of continuous, piecewise affine functions
F = { f ∈ C [ a , b ] : ∃ a partition { x j } of [ a , b ] s.t. f ∣ [ x j − 1 , x j ] ∈ P 1 } \mathcal{F} = \{ f \in \cts[a,b] : \exists \textrm{ a partition $\{x_j\}$ of $[a,b]$ s.t. } f|_{[x_{j-1},x_j]} \in \poly_1 \} F = { f ∈ C [ a , b ] : ∃ a partition { x j } of [ a , b ] s.t. f ∣ [ x j − 1 , x j ] ∈ P 1 } Then F \mathcal{F} F is dense in C [ a , b ] \cts[a,b] C [ a , b ] .
Let
I n ( f ) = ∑ j = 0 n w j , n f ( x j , n ) , n ≥ 1 I_n(f) = \sum_{j=0}^n w_{j,n} f(x_{j,n}), \qquad n \ge 1 I n ( f ) = j = 0 ∑ n w j , n f ( x j , n ) , n ≥ 1 be a sequence of numerical integration formulae that approximate
I ( f ) = ∫ a b f ( x ) d x I(f) = \int_a^b f(x) \ud x I ( f ) = ∫ a b f ( x ) d x Let F \mathcal{F} F be a dense subset of C [ a , b ] \cts[a,b] C [ a , b ] . Then
I n ( f ) → I ( f ) , ∀ f ∈ C [ a , b ] I_n(f) \to I(f), \qquad \forall f \in \cts[a,b] I n ( f ) → I ( f ) , ∀ f ∈ C [ a , b ] if and only if
I n ( f ) → I ( f ) I_n(f) \to I(f) I n ( f ) → I ( f ) for all f ∈ F f \in \mathcal{F} f ∈ F
B = sup n ∑ j = 0 n ∣ w j , n ∣ < ∞ B = \sup_n \sum_{j=0}^n |w_{j,n}| < \infty B = sup n ∑ j = 0 n ∣ w j , n ∣ < ∞
Note that for an integration formula
∑ j = 0 n w j , n = b − a \sum_{j=0}^n w_{j,n} = b - a j = 0 ∑ n w j , n = b − a (a) Condition (14) implies (1) is obvious since F \mathcal{F} F is a subset of C [ a , b ] \cts[a,b] C [ a , b ] . That it implies (2) also is an application of the uniform boundedness principle.
(b) Now we prove the reverse implication. We have to use the density of F \mathcal{F} F and the given two conditions. Let ϵ > 0 \epsilon > 0 ϵ > 0 be arbitrary and decompose the integration error into three parts;
I ( f ) − I n ( f ) = I ( f ) − I ( f ϵ ) + I ( f ϵ ) − I n ( f ϵ ) + I n ( f ϵ ) − I n ( f ) ∣ I ( f ) − I n ( f ) ∣ ≤ ∣ I ( f ) − I ( f ϵ ) ∣ + ∣ I ( f ϵ ) − I n ( f ϵ ) ∣ + ∣ I n ( f ϵ ) − I n ( f ) ∣ ≤ ( b − a ) ∥ f − f ϵ ∥ ∞ + ∣ I ( f ϵ ) − I n ( f ϵ ) ∣ + ∥ f − f ϵ ∥ ∞ ∑ j = 0 n ∣ w j , n ∣ ⏟ ≤ B \begin{align*}
I(f) - I_n(f) &= \clr{red}{I(f) - I(f_\epsilon)} + \clr{blue}{I(f_\epsilon) - I_n(f_\epsilon) } + \clr{magenta}{ I_n(f_\epsilon) - I_n(f) } \\
|I(f) - I_n(f)|
&\le |I(f) - I(f_\epsilon)| + |I(f_\epsilon) - I_n(f_\epsilon)| + |I_n(f_\epsilon) - I_n(f)| \\
&\le (b-a)\norm{f - f_\epsilon}_\infty + |I(f_\epsilon) - I_n(f_\epsilon)| + \norm{f - f_\epsilon}_\infty \underbrace{\sum_{j=0}^n |w_{j,n}|}_{\le B}
\end{align*} I ( f ) − I n ( f ) ∣ I ( f ) − I n ( f ) ∣ = I ( f ) − I ( f ϵ ) + I ( f ϵ ) − I n ( f ϵ ) + I n ( f ϵ ) − I n ( f ) ≤ ∣ I ( f ) − I ( f ϵ ) ∣ + ∣ I ( f ϵ ) − I n ( f ϵ ) ∣ + ∣ I n ( f ϵ ) − I n ( f ) ∣ ≤ ( b − a ) ∥ f − f ϵ ∥ ∞ + ∣ I ( f ϵ ) − I n ( f ϵ ) ∣ + ∥ f − f ϵ ∥ ∞ ≤ B j = 0 ∑ n ∣ w j , n ∣ Using density, choose f ϵ f_\epsilon f ϵ such that
∥ f − f ϵ ∥ ∞ ≤ ϵ 2 ( b − a + B ) \norm{f - f_\epsilon}_\infty \le \frac{\epsilon}{2(b-a + B)} ∥ f − f ϵ ∥ ∞ ≤ 2 ( b − a + B ) ϵ Using property (1), we can choose n n n large enough so that
∣ I ( f ϵ ) − I n ( f ϵ ) ∣ ≤ ϵ 2 |I(f_\epsilon) - I_n(f_\epsilon)| \le \frac{\epsilon}{2} ∣ I ( f ϵ ) − I n ( f ϵ ) ∣ ≤ 2 ϵ Then
∣ I ( f ) − I n ( f ) ∣ ≤ ( b − a ) ϵ 2 ( b − a + B ) + ϵ 2 + B ϵ 2 ( b − a + B ) = ϵ \begin{align}
|I(f) - I_n(f)|
&\le \frac{(b-a) \epsilon}{2(b - a + B)} + \frac{\epsilon}{2} + \frac{B \epsilon}{2 (b - a + B)} \\
&= \epsilon
\end{align} ∣ I ( f ) − I n ( f ) ∣ ≤ 2 ( b − a + B ) ( b − a ) ϵ + 2 ϵ + 2 ( b − a + B ) B ϵ = ϵ Since ε was arbitrary, we conclude that I n ( f ) → I ( f ) I_n(f) \to I(f) I n ( f ) → I ( f ) .
For the function f ( x ) = 1 / ( 1 + x 2 ) f(x) = 1/(1+x^2) f ( x ) = 1/ ( 1 + x 2 ) , x ∈ [ − 4 , 4 ] x \in [-4,4] x ∈ [ − 4 , 4 ] , the Newton-Cotes on uniform points does not converge.
Take F \mathcal{F} F to be set of all polynomials. Then given any f ∈ F f \in \mathcal{F} f ∈ F , clearly I n ( f ) = I ( f ) I_n(f) = I(f) I n ( f ) = I ( f ) when n ≥ n \ge n ≥ degree of f f f , so condition (1) of theorem is satisfied.
However, property (2) does not hold for uniform points. The weights
w j , n = ∫ a b ℓ j , n ( x ) d x w_{j,n} = \int_a^b \ell_{j,n}(x) \ud x w j , n = ∫ a b ℓ j , n ( x ) d x increase with n n n taking both positive and negative values, so that B = ∞ B = \infty B = ∞ .
Here are the weights for n = 20 n=20 n = 20 intervals in [ 0 , 1 ] [0,1] [ 0 , 1 ] .
n = 20
w,B = newton_cotes(n, equal=1)
w = w / n
for w1 in w:
print("{:18.12f}".format(w1))
print("sum(w) = ", sum(w))
print("sum(|w|) = ", sum(abs(w)))
0.011825273249
0.114137717645
-0.236478370511
1.206186893482
-3.771031726716
10.336798219898
-22.708815844147
41.828057422070
-64.075279490324
82.797283471795
-90.005367135629
82.797283472028
-64.075279489974
41.828057422070
-22.708815843856
10.336798219942
-3.771031726694
1.206186893480
-0.236478370511
0.114137717645
0.011825273249
sum(w) = 0.9999999981875201
sum(|w|) = 544.1771559949144
Because the weights are large in size, there is also more roundoff error when we add them, the sum is not exactly one.