%caltrap
clear;clf;
format long;
oldval=1e10;kmax=100;
a=0;b=1;
tol=1e-8;
%f=inline('x.^(.5)','x');ex=2/3;tit=[' Successive Trapezoidal Approximations to \int_0^1 x^{1/2} dx '];
f=inline('sin(pi*x)','x');ex=2/pi;tit=[' Successive Trapezoidal Approximations to \int_0^1 sin(\pi x) dx '];
%f=inline('(sin(pi*x)).^2','x');ex=1/2;tit=[' Successive Trapezoidal Approximations to \int_0^1 sin^2(\pi x) dx '];
for k=1:kmax
   [hs val]=trapez(f,a,b,oldval,k);
   if abs(val-oldval)<tol*abs(oldval);break;end
   oldval=val
   h(k,1)=hs
   I(k,1)=val;
   Ex(k,1)=ex;
   figure(1);plot(h,I,'k-*','linewidth',1)
   xlabel('h')
   ylabel('I(h)')
  title(tit);pause(.5)
end
ER=(I-Ex);
[h, I, Ex,ER]
figure(1);plot(h,I,'k-*','linewidth',1)
xlabel('h')
ylabel('I(h)')
title(tit);pause(0.5)
figure(2);loglog(h,abs((I-Ex)),'k--*',h,h.^2,'k:',h,h.^(3/2),'k','linewidth',2)
xlabel('h')
ylabel('abs|I(h)-Exact|')
title(tit);
legend(' Trapezium Rule Error',' h^2 ',' h^{3/2} ',2)