%
%  Linear 2D ODE system example
%  Demonstrates use of phase space and vector field
%  Victor Matveev, Apr 1 2010
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

f = @(t,y) [ y(1)-2*y(2); y(1) ]   % Computes rates of change as a vector
[T, Y] = ode45(f, [0 6], [0 1]);   % Initial condition is (0,1)

subplot(211); hold on;
plot(T,Y(:,1),'k-');
plot(T,Y(:,2),'b-');
xlabel('time'); ylabel('x(t) and y(t)');
legend('x(t)', 'y(t)');
title('Solution as a function of time');

subplot(212); hold on;
plot(Y(:,1), Y(:,2),'r-');
title('Phase-plane plot; unstable focus at (0, 0)');
xlabel('x(t)'); ylabel('y(t)');

x = linspace(-20, 10, 12);
y = linspace(-10, 15, 12);
[XX, YY] = meshgrid(x,y);
U = XX - 2*YY;
V = XX;
quiver(XX,YY,U,V);
axis([-20 12 -10 15]);
