%-----------------------------------------------------%
% First order differential eq: 2nd order Runge-Kutta  %
%-----------------------------------------------------%
%INPUT: t1 - initial time
%       tN - final time
%       x1 - initial condition
%       N  - number of points on the grid
%       f  - derivative function (anonymous f(x,t))
%OUTPUT: xt_RK and t
 function [xt_RK, t] = RK_method(t1, x1, tN, N, f)

t=linspace(t1,tN,N);
dt=t(2)-t(1);

xt_RK(1)=x1;
for i=1:N-1
    xmid(i) = xt_RK(i) + (dt/2)*f(xt_RK(i),t(i));
    xt_RK(i+1) = xt_RK(i) + dt*f(xmid(i),t(i)+dt/2);
end

end