function [resultFine,error,finestLevel]=Automatic(a,b,nInitial,F,tolerance,topLevel)

% this implements simple automatic integration

n=nInitial/2;

resultCoarse=Simpson(a,b,n,F);

error=1.e10;

result = 0.;

presentLevel=topLevel;

while ( (error > tolerance) & (presentLevel > 0 ) )
   n=n*2;
   resultFine = Simpson(a,b,n,F);    
   error = abs( ( resultFine - resultCoarse ) /15.);

   resultCoarse = resultFine;
   presentLevel=presentLevel-1;   
end
finestLevel=presentLevel+1;