% The Euler method, (c) L. Euler 1768.
% Barnett 9/28/05
% Roughly follows algorithm on p. 104 of Boyce & DiPrima, 8th ed.

f = @(t,y) 1+t-y;                                   % set up function f(t,y)
t0 = 0; y0 = 2;                                     % IC
h = 0.1;                                            % time step
T = 4;                                              % final (stopping) time

N = (T-t0)/h;                                       % number of steps
ys(1) = y0;                                         % first y,t given by IC
ts(1) = t0;                                         % (NB indexing starts at 1)
for n=1:N
  ys(n+1) = ys(n) + h*f(ts(n),ys(n));               % Euler update for y
  ts(n+1) = ts(n) + h;                              % fill the time values too
end

% you now have ys containing a vector of y values corresponding to t values
% in the vector ts
