% Demonstration of diagonalization for 2x2 matrices
%
% Samuli Siltanen November 2012

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Real and distinct eigenvalues

% This is an example of stretching along a coordinate axis
A = [[1 0];[0 2.5]];
kissa(A)
pause

% We add a coordinate transformation (rotation) based on two orthogonal
% eigenvectors
fii = pi/4;
P   = [[cos(fii) -sin(fii)];[sin(fii) cos(fii)]];
A2  = P*A*inv(P);
kissa(A2)
pause

% We replace the coordinate transformation by another, this time based on
% eigenvectors that are not orthogonal
evec1 = [1;0];
evec2 = [-1;1];
evec2 = evec2/norm(evec2); % Normalize the length of the vector to 1
P     = [evec1,evec2]; % Eigenvectors are the columns of P
A3    = P*A*inv(P);
kissa(A3)
pause



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Complex eigenvalues

% Now let's take a rotation matrix as our starting point
fii = -pi/2;
B   = [[cos(fii) -sin(fii)];[sin(fii) cos(fii)]];
kissa(B)
pause

% We can add scaling
B2 = 0.8*B;
kissa(B2)
pause

% We use a coordinate transformation based on eigenvectors that are not 
% orthogonal
evec1 = [1;0];
evec2 = [-1;1];
evec2 = evec2/norm(evec2); % Normalize the length of the vector to 1
P     = [evec1,evec2]; % Eigenvectors are the columns of P
B3    = P*B*inv(P);
kissa(B3)
pause



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Jordan form

% This matrix cannot be diagonalized
C = [[2 1];[0 2]];
kissa(C)
pause

[P,D] = eig(C)
[P,D] = jordan(C)
pause

% We add a coordinate transformation (rotation) based on two orthogonal
% eigenvectors
fii = pi/4;
P   = [[cos(fii) -sin(fii)];[sin(fii) cos(fii)]];
C2  = P*C*inv(P);
kissa(C2)





