%pi_estimate uses quadtx to estimate the value of pi through the integral
%2/(1+x^2)

f=inline('2/(1+x^2)');
%fcountes=inline('4*x^(-.2)');

subplot(311);ezplot(f,-1.5,1.5);
Qexact=pi;
disp('  tol                 Q            fcount    error        error/tol');
for k=1:12  
        
    tol=10^(-k);
    tolvector(k)=10^(-k);
    [Q,fcount]=quadtx(f,-1,1,tol);
    error(k)=Q-Qexact;
    ratio=error(k)/tol;
    fcountvector(k)=fcount;
    fprintf('%8.0e %21.14f %7d %13.3e %9.3f\n',tol,Q,fcount,error(k),ratio);
end
end

fcountes=(4.*tolvector.^(-.2)); %Best fit for fcount vs. tolerance

subplot(312);semilogx(tolvector,fcountvector,'*');
ylabel('fcount');xlabel('tol');
hold on; semilogx(tolvector,fcountes,'r');legend('Actual fcount','Fcount fit');

errores=(10^-3.*tolvector);

subplot(313);loglog(tolvector,error,'o');
ylabel('Error (Q-Pi)');xlabel('tol');
hold on; semilogx(tolvector,errores,'g');
legend('Actual Error','Error fit',4);