%% random real matrices often have complex eigenvalues
% note eigenvalues on diagonal of D, eigenvectors are columns of D

A = rand(3, 3);
[V, D] = eig(A)

%% $A v = \lambda v$

lam1 = D(1,1);
v1 = V(:, 1);

A*v1
lam1*v1

%% for real matrix, eigenvals and eigenvecs come in complex pairs

A = rand(5, 5);
[V, D] = eig(A)

A = rand(6, 6);
[V, D] = eig(A)


%%
% (unless we make a complex matrix, then they are just complex numbers
% without structure)

A = rand(3, 3) + 1i*rand(3, 3);
[V, D] = eig(A)

%% example from lecture
% TODO: something wrong here?

A = [0 0 3; 1 0 -1; 0 1 3]


%% by hand we found

l1 = 3;
v1 = [1; 0; 1];
l2 = 1i;
v2 = [-3i; -3 + 1i; 1];
l3 = conj(l2);
v3 = conj(v2);

%% compare to the numerical results:
% careful: don't think matlab/octave guarantee output order:

[V, D] = eig(A);

v1 ./ V(:, 3)
v2 ./ V(:, 1)
v3 ./ V(:, 2)

%% repeat calculation from lecture

x0 = [1; 0; 0]
T = [v1 v2 v3]
c = T \ x0

%% NOTE: in lecture, I incorrectly had 1 instead of 0.1 for c1

format long g

0.1 * 3^13 * v1  +  2*real(c(2) * l2^13 * v2)

A^13*x0
