function compare

a = 0;
b = 2*pi;
M = 16;
for i = 1:10
H(i) = (b-a)/M;
exact1 = -.122122604618968;
exact2 = 2*pi^2;
approx1(i) = midpoint(a,b,M,@integrand1);
approx2(i) = quadc(a,b,M,@integrand1,1,1);
approx3(i) = quadc(a,b,M,@integrand1,1,2);
approx4(i) = quadc(a,b,M,@integrand1,1,4);
approx5(i) = quadc(a,b,M,@integrand1,1,3);
error1(i) = abs(approx1(i) - exact1);
error2(i) = abs(approx2(i) - exact1);
error3(i) = abs(approx3(i) - exact1);
error4(i) = abs(approx4(i) - exact1);
error5(i) = abs(approx5(i) - exact1);
M = M*2;
end
loglog(H,error1,'ro',H,error2,'m.',H,error3,'g-',H,error4,'b+',H,error5,'k*')

function f = integrand1(x)
f = x.*exp(-x).*cos(2*x);

function f = integrand2(x)
f = x;

function Imp = midpoint(a,b,M,f)
% USE: midpoint(0,1,10,'cos')
% or
% midpoint(0,1,10,@fun)
H = (b-a)/M;
x = linspace(a+H/2,b-H/2,M);
fmp = feval(f,x);
Imp=H*sum(fmp);