
function cavalieri_simpson_script

f=@(x) sin(x.^2);

a=0; b=pi/4;
tol=10^(-8);
Q = quadl(f,a,b,tol);


m(1,1)=3;
[I,h] = cavalieri_simpson (f,a,b,m);
err=tol+1;

while err >= tol
    m(end+1,1)=2*m(end,1);
    [IL,h] = cavalieri_simpson (f,a,b,m(end));
    I=[I; IL];
    err=abs(I(end)-I(end-1));
end

save_table(m,I);

ea=abs(Q-I);
semilogy(m,ea);
title('Errore Assoluto Cavalieri Simpson composta');


function save_table(m,I)


% print su file.
delete('tabella.txt');
nomefile=fopen('tabella.txt','wt') ;

fprintf(nomefile,'          m     |        I  \n ');
fprintf(nomefile,'.......................................\n ');
for ii=1:length(m)
fprintf(nomefile, '%15g | %1.15e \n',m(ii),I(ii)) ; % SCRITTO SU FILE.
end
st=fclose (nomefile) ; % CHIUSO

% print su video.
fprintf('          m     |        I  \n ');
fprintf('.......................................\n ');
for ii=1:length(m)
fprintf('%15g | %1.15e \n',m(ii),I(ii)) ; % SCRITTO SU FILE.
end

