%% Exercises on Eigenvalues & Eigenvectors, Matrix Exponential and Characteristic Polynomial of a Matrix
%
%% Exercise 1
%         A = \left[ \begin{array}{cc}
% -2 & 6 \\
% -2 & 5
% \end{array} \right]
% \quad \Longrightarrow \quad p_{A}(s) = 0\,,\; s_{1\,,\,2} = ? \qquad e^{At} = ?
% Let's determine the eigenvalues (and later the eigenvectors),  and the matrix exponential by exploiting the Symbolic Math Toolbox .
%

syms s

A = sym([ -2,  6; ...
          -2,  5])
% the characteristic polynomial
p_A(s) = charpoly(A, s)

% ALTERNATIVE way
ALPp_A(s) = det(s*eye(size(A))-A)

%


% let's determine the eigenvalues of A
% standard approach: 
% using the MATLAB function "eig" [Symbolic Math
% Toolbox]
sA = eig(A)
s1 = sA(1)
s2 = sA(2)
% Alternative way: eigenvalues as roots of the characteristic polynomial
ALT_sA = solve(p_A,s)

%

% let's determine the eigenvectors, using the Symbolic Math Toolbox
help sym/eig
[V, D, p] = eig(A)

% The length of the row-array p is the total number of linearly independent eigenvectors of A.
% Moreover:
% p(1) = 1: the first eigenvector (the first column of V, i.e., V(:,1)  ) corresponds to the first element on the main diagonal of D, i.e., to D(1,1).
% p(2) = 2: the second eigenvector (the second column of V, i.e., V(:,2)  ) corresponds to the second element on the main diagonal of D, i.e., to D(2,2).
%

% let's compute the diagonal form of the matrix A
v1 = V(:,1);
v2 = V(:,2);

matT = V; % the full-rank transformation matrix
matT1 = inv(matT); % and the corresponding inverse matrix

%                 \tilde{A} = T^{-1}\,A\,T\,,\qquad T =\big[ \begin{array}{c|c} v_1 & v_2 \end{array}\big]


diagA = matT1 * A *matT

%

% computation of the matrix exponential
help sym/expm
expA = expm(A*t)

% ALTERNATIVE way
help sym/diag
exp_diagA = diag([exp(s1*t), exp(s2*t)])
% let's apply the formula on Part 3 - p.16, p. 20
ALPexpA = matT*exp_diagA*matT1

%
%% Exercise 2
%
%             A = \left[ \begin{array}{ccc}
% -2 & 4 & -1 \\
% -1 & -2& 3\\
% 0 & 0 & -5
% \end{array} \right]  \quad \Longrightarrow \quad p_{A}(s) = 0\,,\; s_{1\,,\,2\,,\, 3} = ? \qquad e^{A\,t} = ?
% Let's determine the eigenvalues (and later the eigenvectors),  and the matrix exponential by exploiting the Symbolic Math Toolbox .
%
% How do you determine the trace of the A matrix?

clear variables
syms s t

A = sym([ -2,  4,  -1; ...
          -1,  -2,  3; ...
          0,  0,  -5])

sA = eig(A)
pA(s) = charpoly(A,s)

[V, D, p] = eig(A)

% The length of the row-array p is the total number of linearly independent eigenvectors of A.
% Moreover:
% p(1) = 1: the first eigenvector (the first column of V, i.e., V(:,1)  ) corresponds to the first element on the main diagonal of D, i.e., to D(1,1).
% p(2) = 2: the second eigenvector (the second column of V, i.e., V(:,2)  ) corresponds to the second element on the main diagonal of D, i.e., to D(2,2).
% p(3) = 3
% : the second eigenvector (the second column of V, i.e., V(:,3)  ) corresponds to the third element on the main diagonal of D, i.e., to D(3,3).

matT = V; % the full-rank transformation matrix
matT1 = inv(matT); % and the corresponding inverse matrix

diagA = matT1 * A *matT

expA = simplify(expm(A*t),500)
diagA_exp = expm(diagA*t)

%
% What about the trace of A?

trA = trace(A)
sum(sA)

%
%% Exercise 3
%  A = \left[ \begin{array}{ccccc}
% -2 & 1  &  0  &  0  & 0 \\
% 0 & -2  &  0  &  0  & 0\\
% 0 &  0  & -5  &  1  & 0\\
% 0 &  0  &  0  & -5  & 0\\
% 0  & 0  &  0  &  0  & -10
% \end{array} \right]  \quad \Longrightarrow \quad p_{A}(s) = 0\,,\; s_{1,\,2,\, 3, \, 4,\,5} = ? \qquad e^{A\,t} = ?
%
% Let's determine the eigenvalues, the eigenvectors,  and the matrix exponential by exploiting the Symbolic Math Toolbox .
%
% Does this matrix admit the diagonal form?

clear variables
syms t

A = sym([-2,  1,  0,  0,  0; ...
          0, -2,  0,  0,  0; ...
          0,  0, -5,  1,  0; ...
          0,  0,  0, -5,  0; ...
          0,  0,  0,  0, -10])

[eigV, eigVALDiag, p ] = eig(A)

% The length of the row-array p is the total number of linearly independent eigenvectors of A. This time we obtained only 3 independent eigenvectors: thus, there is no way to obtain the diagonal form of the matrix A, because we can not assembly a full-rank, square transformation matrix T such that \tilde{A} = T^{-1} \cdot A \cdot T is the diagonal form of A.
% Moreover:
% p(1) = 1: the first eigenvector (the first column of V, i.e., V(:,1)  ) corresponds to the first element on the main diagonal of D, i.e., to D(1,1).
% p(2) = 2: the second eigenvector (the second column of V, i.e., V(:,2)  ) corresponds to the second element on the main diagonal of D, i.e., to D(2,2).
% p(3) = 4
% : the second eigenvector (the third column of V, i.e., V(:,3)  ) corresponds to the fourth element on the main diagonal of D, i.e., to D(4,\,4).
% Exploiting the MATLAB function expm, we are able to determine the matrix exponential, as follows:

expAnoDiag = expm(A*t)

%