function chybametody(x,h)
% program pro demonstraci chyby (radu) metody aproximace derivace
% volani chybametody(x,h)
% x = bod v nemz pocitame derivaci
% h - delka kroku
der1 = [];
der2 = [];
err1 = [];
err2 = [];
hh = [];
dera = df(x);
i = 1;
while h > 1e-15
    hh(i) = h;
    der1(i) = df1ord(x,h);
    err1(i) = (dera-der1(i))/dera;
    der2(i) = df2ord(x,h);
    err2(i) = (dera-der2(i))/dera;
    h = h/1.1;
    i = i+1;
end


semilogx(hh,err1,'b.-');
hold on;
semilogx(hh,err2,'r.-');
hold off;
grid on;
end

function f = f(x)
% funkcni predpis
    f = sin(x);
end

function df = df(x)
% analyticka derivace f
    df = cos(x);
end

function df = df1ord(x,h)
% aproximace derivace prvniho radu
    df = (f(x+h) - f(x))/h;
end

function df = df2ord(x,h)
% aproximace derivace druheho radu
    df = (f(x+h/2) - f(x-h/2))/h;
end