% Use RK method of order 2 to compute an approximation of the initial 
% value problem y'=f(y,t) y(t0)=y0.
%
% Usage:
%   yn=RK2(y0,t0,T,f,n)
% where
%   y0 -- initial condition
%   t0 -- initial time
%   T  -- ending time
%   f  -- forcing function
%   n  -- number of steps to take
%
% Written Feb 6, 2008 by Eric Olson for Math/CS 467/667

function yn=RK2(y0,t0,T,f,n)
    h=(T-t0)/n;
    yn=y0;
    for i=0:n-1
       tn=t0+i*h;
       k1=h*feval(f,yn,tn);
       k2=h*feval(f,yn+k1,tn+h);
       yn=yn+(1/2)*(k1+k2);
    end
end
