clear

% DERIVATA DELL'ESPONENZIALE.
x=1;
fx=exp(x);
f1xesatta=exp(x);

m=20;

% METODO 1: RAPPORTO INCREMENTALE.
for index=1:m
    h=2^(-index);
    x0=x-h;
    f1x(index)=(fx-exp(x0))/(x-x0);
    relerr1(index)=abs(f1xesatta-f1x(index))/abs(f1xesatta);
    %fprintf('\n \t [x]: %5.5f [h]: %2.2e [rel.err.]: %2.2e',x,h,relerr1(index));
end


% METODO 2.
for index=1:m
    h=2^(-index);
    xplus=x+h; xminus=x-h;
    f1x(index)=(exp(xplus)-exp(xminus))/(2*h);
    relerr2(index)=abs(f1xesatta-f1x(index))/abs(f1xesatta);
    %fprintf('\n \t [x]: %5.5f [h]: %2.2e [rel.err.]: %2.2e',x,h,relerr2(index));
end

% PLOT ERRORI
semilogy(1:m,relerr1,'r-+'); hold on
semilogy(1:m,relerr2,'k-o');

pause(3); hold off;

h=2.^(-(1:m)); h=h;
stima1=h*exp(1)/2;
stima2=(h.^2)*exp(1)/6;

format short e
[relerr1 stima1]
pause
[relerr2 stima2]
