% numerical integration of functions using gaussian quadrature

f1=@(x) (1+4.*x.^2).^(-1);
f2=@(x) x.^20;
f3=@(x) abs(x).^3;
error1=zeros(40);
error2=zeros(40);
error3=zeros(40);
sum1=0;
sum2=0;
sum3=0;

%testing for different number of nodes =n+1
for n=1:40;
   [x,w]=gauss(n); %sets up gaussian nodes and weights for [-1,1]
   sum1=w*f1(x);
   sum2=w*f2(x);
   sum3=w*f3(x);
   error1(n)=abs(atan(2)-sum1); 
   error2(n)=abs(2/21-sum2);
   error3(n)=abs(1/2-sum3);
end

figure;
semilogy(1:40,error1);
xlabel('n');
ylabel('error');
print -depsc 4a.eps;

%not sure if semilogy or loglog is better...
figure;
loglog(1:40,error2);
xlabel('n');
ylabel('error');
print -depsc 4b.eps;

figure;
loglog(1:40,error3);
xlabel('n');
ylabel('error');
print -depsc 4c.eps;