function [result,error,finestLevel]=Adaptive(a,b,nInitial,F,tolerance,presentLevel)

% this implements the most simple adaptive strategy (always refine both
% left and right)

n=nInitial;

resultCoarse=Simpson(a,b,n/2,F);
result=Simpson(a,b,n,F);

error = ( result - resultCoarse ) /15.;
finestLevel=presentLevel;

if ( (abs(error) > tolerance) & ( presentLevel > 1 ) )
   presentLevel=presentLevel-1;
   [resultLeft, errorLeft, finestLevelLeft]   = Adaptive(a,(a+b)/2,n,F,tolerance/2,presentLevel);
   [resultRight,errorRight, finestLevelRight] = Adaptive((a+b)/2,b,n,F,tolerance/2,presentLevel);
   error=errorLeft+errorRight;
   result=resultLeft+resultRight;
   finestLevel=min([finestLevelLeft,finestLevelRight]);
end
a;
b;
error;
finestLevel;
