clear

% define a matrix that has complex eigenvalues
A = [0 1; -4 -1];

% use this if I want to define a random matrix.  The following will often
% give a matrix that has complex eigenvalues...
% A = [0 1; -rand rand-1/2]; 

% compute the eigenvalues and eigenvectors of A
[v,d] = eig(A)
v1 = v(:,1);
% extract the real & imaginary parts of the first eigenvalue
mu = real(d(1,1));
nu = imag(d(2,2));
% extract the real and imaginary parts of the first eigenvector
a = real(v1);
b = imag(v1);
% build the matrix T
T = [a,b];

% I want to plot the circle and ellipse for t in [0, 2*pi/nu] so first I
% define a time array
N = 100;
dt = (2*pi/nu)/N;
t = 0:dt:2*pi/nu;
% I define the circle
C = [cos(nu*t);-sin(nu*t)];
% I define the ellipse which is T acting on the circle
E = T*C;

% let's plot the circle and ellipse!
figure(1)
clf
plot(C(1,:),C(2,:))
hold on
plot(E(1,:),E(2,:),'r')
axis('equal')

% now I want to find the major and minor axes of the ellipse.  So I compute
% the SVD of the matrix T.
[U,S,V] = svd(T);
% the first vector is the first column of U times the first singular value
U1 = U(:,1)*S(1,1);
% the second vector is the second column of U times the second singular value
U2 = U(:,2)*S(2,2);
% mark these points on the ellipse.  (I'm happy to see that they're on the
% ellipse and appear in the right location!)
plot(U1(1),U1(2),'bo')
plot(U2(1),U2(2),'bx')

% the following is just aesthetics to make it clearer that I really did
% find the closest and furthest points from the origin...
x = -1:.01:1;
plot(U1(1)*x,U1(2)*x,':')
plot(U2(1)*x,U2(2)*x,':')
xx = 0:.01:1;
plot(U1(1)*xx,U1(2)*xx,'-')
plot(U2(1)*xx,U2(2)*xx,'-')







