function retval = Euler(fName,t0,y0,hStep,nSteps, fEvery)
% usage: Euler(fname,t0,y0,hStep,nSteps, fEvery)
% description: Returns an approximate solution to dy/dt = fcn(t,y) at
% t = t0 + hStep*nSteps.  Here fName is the name of fcn, y(t0) = y0
% an n x 1 column vector and the method of solution is explicit Euler. 
% The optional flag fEvery is default set to false.
% User can change it via parameter list. Return value
% in this case is an nX(nsteps+1) matrix whose columns are 
% computed solutions interpolated to the grid of times 
% t = t0 + k*hStep, k = 0,...,nSteps+1. In case of an error condition, 
% an error message is displayed and the global gError is set to true. 

% programmer: t. shores
% last rev: 4/7/16

% Octave and Matlab compatible file
% local variables
% jj: index variable
% t: current time value to which y values are accepted
% y: current value of y which is accepted at time t
% h: current stepsize 

global gError; 

% initialization code
y0 = y0(:);   % ensure column
n = size(y0, 1);
if (exist('fEvery', 'var'))
  retval = zeros(n, nSteps + 1);
  retval(:,1) = y0(:);
  fEvery = true;
else
  fEvery = false;
end
t = t0;
y = y0;
h = hStep;
% main loop
for jj = 1:nSteps
  % do the Euler step
  y = y + hStep*feval(fName, t, y);
  t = t + hStep;
  %  do fill-in if requested
  if (fEvery)
     retval(:, jj + 1) =y;
  end
end
if (~fEvery)
  retval = y;
end

end
