function [t,y] = Euler1(f,tspan,y0,N)
%  [t,y] = Euler1(f,[t0,T],y0,N)
%
% Use N steps of Euler method to solve initial value problem
%  y'(t) = f(t,y) , y(t0) = y0
%
% INPUT:
%
% f(t,y)  is function on right hand side of differential equation
%         (use m-file or inline function)
%     t0  is initial time
%     y0  is initial value for y at time t0
%      N  is number of Euler steps
%
% OUTPUT:
%
%      t  is vector of t-values
%      y  is vector of y-values
%
% EXAMPLE:
%     y' = -y + t,  y(1)=2, find y(t) for t in [1,5] using 20 steps:
%
%     f = inline('-y + t', 't','y')
%     [t,y] = Euler1(f, [1,5], 2, 20);
%     [t,y]              % show table of t and y values
%     plot(t,y,'o-')     % plot solution

% NOTE: Since Matlab vectors have lowest index 1, we will use
%       t(1),...,t(N+1) for t_0,...,t_N  
%       y(1),...,y(N+1) for y_0,...,y_N 

t = zeros(N+1,1);            % initialize t and y with zero vectors
y = zeros(N+1,1);

t0 = tspan(1); T = tspan(2);
h = (T - t0)/N;
t(1) = t0;                   % initialize first rows of t,y with initial values
y(1) = y0;

for i=1:N
  t(i+1) = t(i) + h;
  s = feval(f,t(i),y(i));     %   evaluate s = f(t(i),y(i))
  y(i+1) = y(i) + h*s;
end
