% N = linspace(2,200,50);
N = round(logspace(1,3,50));

e1 = zeros(size(N));
e2 = e1;
e4 = e1;

sol = @(t)(2.0*exp(t) - t - 1.0);
f = @(t,y)(t+y);

tfinal = 2;

for i=1:length(N)
    n = N(i);
    [~, y] = solutions_hw7_p4(f, [0 tfinal], 1, 1, n);
    e1(i) = abs(y(end) - sol(tfinal));
    
    [~, y] = solutions_hw7_p4(f, [0 tfinal], 1, 2, n);
    e2(i) = abs(y(end) - sol(tfinal));
    
    [~, y] = solutions_hw7_p4(f, [0 tfinal], 1, 4, n);
    e4(i) = abs(y(end) - sol(tfinal));
    
end
dt = 1./(N);
clf;
loglog(dt, e1, dt, e2, dt, e4);
hold on;
loglog(dt,50*dt,'b--',dt,50*dt.^2,'r--',dt,dt.^4,'k--');
xlabel('Time Step, $\Delta t$','interpreter','latex');
ylabel('Error');
legend('F. Euler', 'Heun''s Method', 'RK4', '$O(\Delta t)$','$O(\Delta t^2)$', '$O(\Delta t^4)$', 'interpreter','latex');
% semilogy(N, e1, N, e2, N, e4);