% This program uses Modified Euler's method to solve the D.E.
%    du/dt = u + t - 1
% where u(0)=2.
clear;

h = 0.1;
U(1) = 2;

k = 1;

% Modified Euler is a two-step procedure.  We first calculate
% utemp, which is the "u(k+1/2) tilde".  Then, we use that to get u(k+1). 

for t = 0:h:2-h

   Uprime1 = U(k) + t - 1;
   Utemp = U(k) + 0.5 * h * Uprime1;

   Uprime2 = Utemp + (t + 0.5 * h) - 1;
   U(k+1) = U(k) + h * Uprime2;

   k = k+1;

end

t = 0:h:2;
Uexact = 2*exp(t) - t;

plot(t,U,t,Uexact,':');
