%* routine to solve dy/dx=x+y using a range of step sizes
% and a function for the Euler single-step method 

%* independent variable
xi=0;				% initial x
xf=5;				% maximum x

%  series of step sizes, h
hv=[1 0.5 0.05 0.01];			    %step sizes to try
hps=['b--'; 'b-.'; 'b. '; 'b: '];	%line format strings

%* dependent variable
y0=1; 			    % initial condition on y

%* exact solution for x0=0, y0=1
x=[xi:0.05:xf];		% make an x for the exact solution
y=zeros(size(x));		% dependent variable vector for exact solution
y(1)=y0;			% insert initial condition
g=2*exp(x) - x -1;


%* compare results 
figure(1)
clf
plot(x, g, 'r-'), hold on

for n=1:length(hv)
    
  %new step size
  x=[xi:hv(n):xf];
  y=zeros(size(x));
  y(1)=y0;
  
  %the model
  for m=1:length(x)-1
      y(m+1)=y(m) + hv(n)*f_dydx(x(m), y(m));
  end
  
  %plot the result
  figure(1)
  plot(x, y, hps(n,:));			
end

figure(1)
title('Euler method solutions to dy/dx = x + y  with y0=1 and x0=0')
legend('exact',num2str(hv(1)),num2str(hv(2)),num2str(hv(3)),num2str(hv(4)))

