 clear; clf; hold on;
 dt=0.2; tend=5;
%% Euler method
 x=1; xEuler=x;
 for t=dt:dt:tend
     x=x+dt*(1-x+t); xEuler=[xEuler,x];
 end
%% Second order
 x=1; xSecond=x;
 for t=dt:dt:tend
     x=x+dt*(1-x+t)+dt^2*(1-x)/2; xSecond=[xSecond,x];
 end
%% Runge-Kutta
 ode_function= @(t,x,flag) 1-x+t;
 [t,xRK]=ode45(ode_function,[0 tend],1,[]);
%% Plotting results
 x=t+exp(-t); plot(t,(xRK-x)./x,'r');
 t=0:dt:tend; x=t+exp(-t);
 plot(t,(xEuler-x)./x);
 plot(t,(xSecond-x)./x,'g');
%% Plot dt error
figure; xDIFF=[];
 for dt=0.01:0.01:1
     x=1; for t=dt:dt:2; x=x+dt*(1-x+t); end
     xDIFF=[xDIFF,(x-2-exp(-2))/(2+exp(-2))];
 end
 dt=0.01:0.01:1; plot(dt,xDIFF);
