function [x,y] = euler_for_plot(x0,y0,x_end,h,fcn,exact)
% Solve the initial value problem
%    y' = f(x,y),  x0 <= x <= b,  y(x0)=y0
% Use Euler's method with a stepsize of h.  The user must 
% supply a program with some name, say deriv, and a first 
% line of the form
%   function ans=deriv(x,y)
% A sample call would be
%   [t,z]=eulercls(t0,z0,b,delta,'deriv')
%
% Output:
% The routine euler_for will return two vectors, x and y.
% The vector x will contain the node points
%   x(1)=x0, x(j)=x0+(j-1)*h, j=1,2,...,N
% with 
%   x(N) <= x_end-h,  x(N)+h > x_end-h
% The vector y will contain the estimates of the solution Y 
% at the node points in x.
%
%!!!!!if you don't know the exact solution, use: !!!!
% function [x,y] = euler_for(x0,y0,x_end,h,fcn)
%
% Example use:
% <Example 1.6, section 6.1 : "y'=y-t" with y(1)=1
% fcn = inline('-t+y','t','y')
% exact=inline('-exp(t-1)+1+t','t','y')
% TYPE: [x,y]=euler_for_plot(1,1,1.5,0.1,fcn,exact)
%
% <Example 1.7, section 6.1 : "y'=y+t"
% fcn = inline('t+y','t','y')
% exact=inline('2*exp(t)-1-t','t','y')
% TYPE: [x,y]=euler_for_plot(0,1,.5,0.1,fcn,exact)
%------------------
%<Exercise 4, section 6.1 : "y'=5-z"
% fcn = inline('5-z','x','z')
% exact=inline('5-5*exp(-x)','x','z')
% TYPE: [x,y]=euler_for_plot(0,0,.5,0.1,fcn,exact)
%   -----------

n = fix((x_end-x0)/h)+1;
x = linspace(x0,x_end,n)';
y = zeros(n,1);
y(1) = y0;
for i = 2:n
  y(i)=y(i-1)+h*feval(fcn,x(i-1),y(i-1));
end
time = x;
ForwardEuler = y;
length(x);
length(y);
Tabular = table(time,ForwardEuler)%,exact);
Tabular(1:length(time),:)  ;  


figure(1)
hold on
plot(x,y,'o',x,feval(exact,x,y),'*-.')
legend('forward Euler solution','exact')
end