Section 8.5 Simpson rule (n=2)
Take x0=a, x1=(a+b)/2 and x2=b. Then
w2=∫bax−a+b2b−a+b2x−ab−adx=b−a6=w0
and
w1=∫bax−aa+b2−ax−ba+b2−bdx=2b−a3.
Hence the method formula is
Q2(f,[a,b])=b−a6[f(a)+4f(a+b2)+f(b)].
Simpson rule error. From the Newton-Cotes error formula (recall that n is even and so we need to use the remainder for n=3), the error bound is given by
(b−a)524max[a,b]|f(4)(x)|∫20s(s−1)(s−2)(s−3)ds.
Hence,
|∫baf(x)dx−Q2(f,[a,b])|≤(b−a)590max[a,b]|f(4)(x)|.
After the usual decomposition procedure, we get finally that
|∫baf(x)dx−Q2,h(f,[a,b])|≤h4b−a90max[a,b]|f(4)(x)|.
Convergence Speed. The formula above shows that the truncation error of the Riemann sum is O(h4). Below we visualize this scaling law by evaluating the error in the computation of ∫20sin(x)dx with h=2−j,1≤j≤10. Due to the high convergence speed of this method, the round-off error shows already before h−3 but the error, before the round-off error kicks-off, goes down to about 10−15 (for a much larger h than it was possible with the previous methods). xxxxxxxxxx
from numpy import linspace,array,frompyfunc,sin,zeros
from matplotlib import pyplot as plt
n=15; a=0.; b=2.; N=1
s = zeros(n); err = zeros(n)
​
for i in range(n):
h = (b-a)/N
x = linspace(a,b,N+1) # these are the x_i
sim = ( sin(x[:-1]) + 4*sin((x[:-1]+x[1:])/2) + sin(x[1:]) ) / 6; # Simpson rule term
s[i] = sum(h*sim) # this is the integral
err[i] = abs(1-cos(2)-s[i]) # and this is the actual error
N *= 2
​
I = range(n)
h = array([(b-a)/2**i for i in I])
plt.clf()
plt.loglog(h,err[I],label=r'$y=error(h)$')
plt.loglog(h,h**4,label=r'$y=h^4$')
plt.xlabel('log h')
plt.ylabel('log y')
plt.legend()
plt.show()