Composite Simpson's rule vs Trapezoidal on integrating $\int_0^{2\pi}\sin^2x dx$ A simple question comparing both methods for numerical integration for a very specific case. We expect the Simpsons rule to have a smaller error than the trapezoidal method, but if we want to calculate
$
\int_0^{2\pi}\sin^2x dx
$
with $n=5$ equidistant points, we have for the trapezoidal rule (not an efficient code, didactic purposes only):
% MATLAB code
x = linspace(0,2*pi,5); % domain discretization
y = sin(x).^2; % function values
h = x(2)-x(1); % step
w_trapz = [1 2 2 2 1]; % weights for composite trapezoidal rule
w_simps = [1 4 2 4 1]; % weights for composite simpson rule
I_trapz = sum(y.*w_trapz)*h/2; % numerical integration trapezoidal
I_simps = sum(y.*w_simps)*h/3; % numerical integration simpsons

The exact answer for this integral is $\pi$ and we check that:
I_trapz =

    3.1416

I_simp =

    4.1888

So, for this particular case, the trapezoidal rule was better. What is reason for that?
Note that the error term in the Composite Simpson's rule is
$
\varepsilon=-\frac{b-a}{180}h^4f^{(4)}(\mu)
$
for some $\mu\in(a,b)$
while the error term for the Composite Trapezoidal rule is
$
\varepsilon=-\frac{b-a}{12}h^2f^{(2)}(\mu)
$
Evaluating the second and forth derivatives of $f(x)=\sin^2(x)$, and noticing $b-a=2\pi$ and $h=\pi/2$, the error term for each of the numerical techniques is:
$
\varepsilon_{Simpson}=-\frac{2\pi}{180}\left(\frac{\pi}{2}\right)^4\left(-8\cos2\mu\right)\\
\varepsilon_{Trapz}=-\frac{2\pi}{12}\left(\frac{\pi}{2}\right)^2\left(2\cos2\mu\right)
$
We estimate the maximum error in each approximation by finding the maximum absolute value the error term can obtain. Since in both approximations we have $\cos(2\mu)$ and $\mu\in(0,2\pi)$, then $\max{|\cos(2\mu)|}=1$, and we have
$
\max{\left|\varepsilon_{Simpson}\right|}=\frac{2\pi}{180}\left(\frac{\pi}{2}\right)^4\left(8\right)=\frac{\pi^5}{180}\approx1.70\\
\max{\left|\varepsilon_{Trapz}\right|}=\frac{2\pi}{12}\left(\frac{\pi}{2}\right)^2\left(2\right)=\frac{\pi^3}{12}\approx2.58
$
We see the error term is smaller for the Simpson method than that for the Trapezoidal method.
However, in this case, the trapezoidal rule gave the exact result of the integral, while the Simpson rule was off by $\approx1.047$ (about 33% wrong).
Why is that? Does it have to do with the number of points in the discretization, with the function being integrated or is it just a coincidence for this particular case?
We observe that increasing the number of points utilized, both methods perform nearly equal. Can we say that for a small number of points, the trapezoidal method will perform better than the Simpson method?
 A: Another point of view is the sampling theorem, as the integrated function is periodic and integrated over 2 periods. The limit frequency of $\sin^2x =\frac12(1-\cos2x)$ is $2$, so with 4 sub-intervals you are at the minimal sampling frequency. If you write $S(h)=\frac{4T(h)-T(2h)}3$ as per Richardson extrapolation, then the term $T(2h)$ is under-sampled with only 2 sub-intervals, inviting substantial aliasing errors. The Simpson method just "does not see" the correct function.
A more regular error behavior should, by this logic, be visible in the next refinements with 8 or 12 sub-intervals in the subdivision of the integration interval.
A: In the last line of your code, you have h/2.  It should be h/3.  You also are using the trapezoid weights instead of the simpson's weights.  In fact, I can't figure out why your two results are different at all, since the calculations in the last two lines are identical.
A: For this value of $h$, the terms $f''(\xi)$ or $f^{(4)}(\xi)$ in the error formula  can become dominant. If for the trapezoidal rule $f''(\xi)$ is small in comparison with $f^{(4)}(\xi)$ for Simpson's rule, you can have this effect. Also, if the integrand function is not regular enough this can happen (not the case here).
Regarding your error estimates, remember that they are upper bounds for the error. Just because the maximum error is larger for the trapezoidal rule, it does not mean that the same will happen with the actual error.
