% A small exmaple demonstrating the concept of eigenvectors
%
% Written by Alexander Stoytchev

% Start with these three samples
X1=[-1, 3,  1]'
X2=[ 2, 1, -1]'
X3=[ 2, 2,  3]'

% Calculate the mean
M = 1/3 * ( X1 + X2 + X3)

% Subtract the mean
X1_m = X1 - M
X2_m = X2 - M
X3_m = X3 - M


% Find the covariance matrix
C = 1/3 * (X1_m * X1_m' + X2_m * X2_m' + X3_m * X3_m') 

% Alternative way to calculate C
% A  = [X1_m, X2_m, X3_m]
% C = 1/3* A * A'

% Find the eigenvalues and the eigenvectors of the covariance matrix
[V, D] = eigs(C)



%% Plot the original points
figure;
plot3(X1(1), X1(2), X1(3), 'r.') % X1 (in red)
hold on;
plot3(X2(1), X2(2), X2(3), 'g.') % X2 (in green)
plot3(X3(1), X3(2), X3(3), 'b.') % X3 (in blue)
plot3( M(1),  M(2),  M(3), 'k.') % mean (in black)
P=[X1, X2, X3]';
fill3(P(:,1), P(:,2), P(:,3), 'y') % their common plane as a triangle (in yellow)

% Draw the Eigenvectors (they must lie in the same plane as the points)
EV1 = V(:, 1) + M; 
EV2 = V(:, 2) + M;
EV3 = V(:, 3) + M;

L1=[M'; EV1'];
L2=[M'; EV2'];
L3=[M'; EV3'];

line(L1(:,1), L1(:,2), L1(:,3)); % draw first eigenvector relative to the mean
line(L2(:,1), L2(:,2), L2(:,3)); % draw seond eigenvector relative to the mean
line(L3(:,1), L3(:,2), L3(:,3)); % draw third eigenvector relative to the mean

xlabel('X')
ylabel('Y')
zlabel('Z')
title('X1, X2, X3, and their Mean')
axis([-3, 3, -3, 3, -3, 3])


