function GLvsNC()
% Compares the m-point Newton-Cotes and Gauss-Legendre rules
% applied to the integral of sin(x) from 0 to pi/2.

clc
a = 0;
b = 1;
% Assume that I is exact where
I = quad(@MyF,a,b,.000000000001);
fprintf('Approximating the integral of exp(-x/5)sin(x^2) from %6.3f to %6.3f\n\n',a,b)
fprintf(' m | I - NC(m) | |I - GL(m) |\n ')
fprintf('------------------------------------------------\n')
for m=2:6
% Compare GL(m) and NC(m)
NC = QNC(@MyF,a,b,m);
GL = QGL(@MyF,a,b,m);
fprintf(' %1.0f %20.16f %20.16f\n',m,abs(NC-I),abs(GL-I))
end
fprintf('\n Roughly, QNC(2m) has the same accuracy as GL(m)\n\n')

function y = MyF(x)
y = exp(-x/5).*sin(x.^2);