function euler  

   % Euler numerical method for solving first order ODEs
   
   % Any line starting with a % is a "comment" and will be ignored by matlab.
   % Comments are extremely important to tell others how your method works.
   % They are even more important to you so that in a year from now you remember
   %   what the heck you were doing. 

   % Define the initial condition and stepsize.
   %    The semicolon at the end tells matlab to not show the result.
   t0 = 0;  		% Specify the initial t.    
   x0 = 0;  		% Specify the initial value of x.
   h = 0.1; 			% Specify the stepsize.
   N = 100; 		% Specify the total number ot steps.
   	
   % With the initial time, stepsize and number of steps given, we know the final time.
   tf = t0 + h*N	% The final time. No semicolon tells matlab to show result.
   
   % Let xn be the numerical solution compute by Euler.
   % Load the initial time and value into the first (n=1) element of the vectors.
   %   Matlab starts its indexing at 1 not 0. 
   t(1) = t0;        
   xn(1) = x0;		
   
   % Implement the Euler method, which requires evaluating the function f.
   % t(n)=current value of t.  xn(n)= value of xn at current time.
   for n = 1:N		% For each value of n, find the value t(n+1) and x(n+1).
     t(n+1) = t(n) + h;   % For t we can just ad another h to find the next value of t.
   	 xn(n+1) = xn(n) + h*f(t(n),xn(n)); % Numerical: Use Euler to find the next value.
   end

    figure(1);  % Plot the numerical solution.
	plot(t,xn);
	xlabel('t')
	ylabel('x')
	% axis([0 10 0 3]); % Set plot axis to [tmin tmax xmin xmax].   

end

function dxdt = f(t,x)
   % Note, the variable names in f, ie, t and x, do not need to match the names
   % used above, ie, tn and xn. It's their location in the list that is important.
   % If the function call was "xn(n+1)=xn(n)+h*f(apple(n),orange(n))", then the variable
   % apple would be used for t and organge would be used for x.

   global parameters
   dxdt = zeros(1,1); % Create a column vector.
	
    dxdt(1) = 5+2*x(1);  		% Example 1
   % dxdt(1)= 3*x(1)^2;   		% Example 2

end

