odefun=@(t,y)[t-y];
tspan=[-1,5];
y0=1;
% we define the exact solution
yex=@(t)[t-1+3*exp(-t-1)];
figure(1)
tt=linspace(tspan(1),tspan(2),1000);
y=yex(tt);
plot(tt,y,'r'); hold on
xlabel('t')
ylabel('y(t)')
color=['ro';'b+';'g.';'ys';'m*';'ch';'k.'];
ERR=[];
H=[1, .8, .5, .1, .05, .01, .001];
k=1;
for h=H
Nh=round((tspan(2)-tspan(1))/h);
[tn,un]=feuler(odefun,tspan,y0,Nh);
plot(tn,un,color(k,:))
y=yex(tn);
err=max(abs(y-un));
ERR=[ERR;err];
k=k+1;
title(['h=',num2str(h)])
pause
end
legend('numerical sol','exact sol')
hold off

%%
figure(2)
loglog(H,ERR,H,H)
xlabel('h');
ylabel('err_h')
grid on












