function [X,T] = RK4(x0,F,t,h)
% Implementation of the classical fourth order Runge-Kutta method for
% an autonomous system of ODEs in vector form
%
% INPUT: x0 (initial value vector)
%        F  (right hand side function)
%        t  (vector with initial and final time)
%        h  (step size)
%
% OUTPUT: X (matrix of approximate solutions at the discrete times)
%         T (vector of times t_n for which the solution is approximated)
%
n = round(diff(t)/h);
X = zeros(length(x0),n+1);
T = linspace(t(1),t(2),n+1);
X(:,1) = x0;
for i = 1:n
    K1 = F(X(:,i));
    K2 = F(X(:,i)+0.5*h*K1);
    K3 = F(X(:,i)+0.5*h*K2);
    K4 = F(X(:,i)+h*K3);
    X(:,i+1) = X(:,i) + (h/6)*(K1+2*K2+2*K3+K4);
end

end

