clear all; clc;

t0 = 0;
y0 = 1;
T = 15;

f = @(t,y) -2*y;

h = 1e-3;
t = t0:h:T;
y_true = exp(-2*t);
y = TwoStep(h, f, t0, y0, T);

figure(1); clf
plot(t, y_true,'-ok', t, y,'--r', 'LineWidth',1)

clear all; clc;
t0 = 0;
y0 = 1;
T = 5;
f = @(t,y) -2*y;

for i = 1:10,
	h = 2^(-i);
	t = t0:h:T;
	y_true = exp(-2*t);

	y = TwoStep(h, f, t0, y0, T);
	err(i) = norm(y-y_true,Inf);
	hspace(i) = h;
end
figure(2); clf
loglog(2.^(-(1:10)), err, '-ob',...
2.^(-(1:10)), 2.^(-2*(1:10)), '--k','LineWidth',2)
legend('error of two step method','1/h^2')
xlabel('stepsize h')
set(gca,'FontSize',14)

% Estimate order of convergence
fprintf('Estimated order of convergence is %5.2f\n',eoc(hspace', err'))
