%% Example of numerical solutions for a system of ODES
% by Radu C. Cascaval, written on Wed, Jan 28, 2015

%% First Step
% Define the right hand side of the system and the initial condition
clf
RHS=@(t,X) [0 1; -1 0]*X;
X0=[3; 0];

%% Second Step
% Use the built in MATLAB solver (Runge Kutta 4th order)

[t,Xplot] = ode45(RHS,[0 100],X0);

%% Third Step
% Plot each component of the solution vs time

plot(t, Xplot(:,1))
hold on
plot(t, Xplot(:,2),'r')
hold off
xlabel('time t')
legend('position', 'speed')

%% Last Step
% Plot the solution curve (in this case a circle) in the phase portrait.

figure
plot( Xplot(:,1),Xplot(:,2))
xlabel('position x')
ylabel('position y')
grid on