% ex1

xs1 = [0; 0];
[X1,L1] = eig([0 1;1 -1]);
xs2 = [1; 0];
[X2,L2] = eig([0 1; -2 -1]);
xs3 = [-1; 0];
[X3,L3] = eig([0 1; -2 -1]);

dx1 = [-2 2];
dx2 = [-1 1];
figure(1);
axis([dx1 dx2]);
hold on
c='gr';

for i=1:150
  x0 = rand(2,1).*([dx1(2);dx2(2)]-[dx1(1);dx2(1)])+[dx1(1);dx2(1)];
  [t,x]=ode15s('ex2', [0, 10], x0);
  plot(x(:,1),x(:,2));
end

if isreal(L1)
  [z,i]=sort(abs(diag(L1)));
  plot([-X1(1,1) X1(1,1)]+xs1(1),[-X1(2,1) X1(2,1)]+xs1(2),c(i(1)),[-X1(1,2) X1(1,2)]+xs1(1),[-X1(2,2) X1(2,2)]+xs1(2),c(i(2)));
end

if isreal(L2)
  [z,i]=sort(abs(diag(L2)));
  plot([-X2(1,1) X2(1,1)]+xs2(1),[-X2(2,1) X2(2,1)]+xs2(2),c(i(1)),[-X2(1,2) X2(1,2)]+xs2(1),[-X2(2,2) X2(2,2)]+xs2(2),c(i(2)));
end

if isreal(L3)
  [z,i]=sort(abs(diag(L3)));
  plot([-X3(1,1) X3(1,1)]+xs3(1),[-X3(2,1) X3(2,1)]+xs3(2),c(i(1)),[-X3(1,2) X3(1,2)]+xs3(1),[-X3(2,2) X3(2,2)]+xs3(2),c(i(2)));
end

plot(dx1,[0 0],'k',[0 0],dx2,'k');
plot(xs1(1),xs1(2),'ro',xs2(1),xs2(2),'ro',xs3(1),xs3(2),'ro');
xlabel('x_1'); ylabel('x_2');
