% Programming and Numerical Methods
% MSc in Computational and Applied Physics

% Code 21: Explicit Modified Euler ODE solver
% A. Meseguer (2007)

% On input: fut (vector field to be called f(z))
%               z = [u_1 ; u_2 ; ... u_N ; t]
%           u0 (initial condition u0(N,1))
%           k  (time step)
%           T  (integration time) 
% On output: t (array of time grid)
%            u (array of v_k's)


  function [t,u] = modeuler(fut,u0,k,T)

  t = [0] ; u = [u0] ; time = 0 ;

  while time < T
   z = [u(:,end) ; t(end)] ;
   a = k*feval(fut,z) ; 
   z = [u(:,end)+a/2 ; t(end)+k/2] ;
   b = k*feval(fut,z) ; 
   u = [u , u(:,end) + b] ;


   time = time + k ; t = [t time] ;
  end  

  
  
