
function demo_derivazione2

example=2;

do_table=1;
do_plot=1;
tol=1*10^(-6);

switch example
    case 1
        f=@(x) exp(x);
        df=@(x) exp(x);
        x0=0;
    case 2
        f=@(x) 1./(1+x.^2);
        df=@(x) -2*x./(1+x.^2).^2;
        x0=5;
end

% risultato esatto
df0=feval(df,x0);
fprintf('\n \t %1.25e \n',df0);

% esperimento
eev=-(0:1:15);
h=10.^eev;
x=x0+h; fx=feval(f,x)+tol*(2*rand(size(x))-1);
y=x0-h; fy=feval(f,y)+tol*(2*rand(size(x))-1);



diffv=(fx-fy)./(x-y);

ea=abs(diffv-df0);

if do_table == 1
    dd=length(x)/2;
    
    for ii=1:dd
        fprintf('\n \t $%1.1e$ & $%1.3e$ & $%1.1e$ & $%1.3e$\\\\',h(ii),ea(ii),h(ii+dd),ea(ii+dd));
    end
end


if do_plot == 1
    loglog(h,ea,'r+','LineWidth',2,...
        'MarkerEdgeColor','k',...
        'MarkerFaceColor','g',...
        'MarkerSize',2);
end