function [] = tabtestder_cos2(x, np)
% si "ripulisce" il workspace e lo schermo

clc;

%x = -0.5;

NP = np;

ek = 0:NP;

nk = 10.^(-ek);

for ik = 1:length(nk)
    h = nk(ik);
    Df(ik)=(cos2(x+h)-cos2(x-h))./(2*h);
end

Df0 = dercos2(x);

Ek = abs(Df - (Df0));

for ik = 2:length(nk)
    alpha(ik) =log(Ek(ik-1)/Ek(ik))/log(nk(ik-1)/nk(ik)) ;
end
alpha(1) = NaN;

Erk = Ek/Df0;


barra = '-----------------------------------------------------------------------------';
titolo = 'Tabella degli errori nel calcolo approssimato della derivata';

disp(titolo);
disp(barra);
fprintf('  ek  \t     h  \t  Df \t\t |E_k| \t\t E_r \t      alpha \n');
disp(barra);
fprintf('  %d \t %2.1e \t %8.7g \t %4.3e  \t %4.3g     \t%4.3g \n',...
    [ek; nk; Df; Ek; Erk; alpha]);

%%%%%%%%%%%%%%%%%%%%%%%%
function y=cos2(x)

y = cos(x.^2);

%%%%%%%%%%%%%%%%%%%%%%%%
function yd = dercos2(x)

yd = - 2 .* x .* sin(x.^2);
