% Interval ne kterem resime
x0 =  0.0;
xn = 10.0;
% Pocet kroku (subintervalu)
n  = 100;    

% Delka intervalu a polohy uzlu
h = 10/n;
x = (0:h:10);

% Presne reseni
yex = exp(-x);
   
% Pole pro kazdou metodu
yeul = zeros(size(x));
ynes = zeros(size(x));

% Pocatecni podminka
yeul(1) = 1;
ynes(1) = 1;
% Prvni krok
yeul(2) = exp(-x(2));
ynes(2) = exp(-x(2));

% Dalsi kroky
for i=3:n+1
    yeul(i) = yeul(i-1) * (1-h);             % eulerova metoda
    ynes(i) = ynes(i-2) - 2 * h * ynes(i-1); % nestabilno metoda
end;


figure;

% Nakreslime reseni
subplot(2,1,1)
pexa = plot(x,yex,'k-');
hold on;
peul = plot(x,yeul,'bo');
pnes = plot(x,ynes,'rx');
hold off;
legend([pexa peul pnes],{'presne','Euler','nestab'},'location','southwest');
title('Reseni')

% Nakreslime chybu
subplot(2,1,2)
erreul = abs((yeul-yex)./(yex));
errnes = abs((ynes-yex)./(yex));
erreul = plot(x,erreul,'bo');
hold on;
errnes = plot(x,errnes,'rx');
hold off;
legend([erreul errnes],{'Euler','nestab'},'location','northwest');
title('Relativni L1 Chyba')
