clear all
close all

tspan = [0, 10];
t = linspace(0, 10, 200);
y = y_ex(t);
y0 = .1;  % initial condition
NH = 10;  % number of equally-spaced intervals
NH2 = 2*NH;
NH4 = 4*NH;
[t1, u1] = feuler(@odefun, tspan, y0, NH);
[t2, u2] = feuler(@odefun, tspan, y0, 2*NH);
[t3, u3] = feuler(@odefun, tspan, y0, 4*NH);
figure()
plot(t1, u1, '.', "markersize", 5, t2, u2, '.',"markersize", 5, ... 
     t3, u3, '.', "markersize", 5, t, y)
title('forward euler')
legend('location', 'east')
legend(['NH = ' num2str(NH)], ['NH = ' num2str(NH2)], ['NH = ' num2str(NH4)])
[t1, u1] = beuler(@odefun, tspan, y0, NH);
[t2, u2] = beuler(@odefun, tspan, y0, 2*NH);
[t3, u3] = beuler(@odefun, tspan, y0, 4*NH);
figure()
plot(t1, u1, '.', "markersize", 5, t2, u2, '.', "markersize", 5, ...
     t3, u3, '.', "markersize", 5, t, y)
title('backward euler')
legend('location', 'east')
legend(['NH = ' num2str(NH)], ['NH = ' num2str(NH2)], ['NH = ' num2str(NH4)])
[t1, u1] = cranknic(@odefun, tspan, y0, NH);
[t2, u2] = cranknic(@odefun, tspan, y0, 2*NH);
[t3, u3] = cranknic(@odefun, tspan, y0, 4*NH);
figure()
plot(t1, u1, '.', "markersize", 5, t2, u2, '.', "markersize", 5, ...
     t3, u3, '.', "markersize", 5, t, y)
title('crank-nicolson')
legend('location', 'east')
legend(['NH = ' num2str(NH)], ['NH = ' num2str(NH2)], ['NH = ' num2str(NH4)])
