%% Matrix Exponentials
% This example shows 3 of the 19 ways to compute the exponential of a matrix.
% For background on the computation of matrix exponentials, see:
% Moler, C. and C. Van Loan. "Nineteen dubious ways to compute the exponential of a matrix, twenty-five years later." SIAM Review. Vol. 45, Number 1, 2003, pp. 3-49.
%
% Start by creating a matrix A.

A = [0 1 2; 0.5 0 1; 2 1 0]
Asave = A;

%
%% Method 1: Scaling and Squaring
% expmdemo1 is an implementation of algorithm 11.3.1 in the book:
% Golub, Gene H. and Charles Van Loan. Matrix Computations, 3rd edition. Baltimore, MD: Johns Hopkins University Press, 1996.

% Scale A by power of 2 so that its norm is < 1/2 .
[f,e] = log2(norm(A,'inf'));
s = max(0,e+1);
A = A/2^s;

% Pade approximation for exp(A)
X = A;
c = 1/2;
E = eye(size(A)) + c*A;
D = eye(size(A)) - c*A;
q = 6;
p = 1;
for k = 2:q
   c = c * (q-k+1) / (k*(2*q-k+1));
   X = A*X;
   cX = c*X;
   E = E + cX;
   if p
     D = D + cX;
   else
     D = D - cX;
   end
   p = ~p;
end
E = D\E;

% Undo scaling by repeated squaring
for k = 1:s
    E = E*E;
end

E1 = E

%
%% Method 2: Taylor Series
% expmdemo2 uses the classic definition for the matrix exponential given by the power series
% e^A = \sum_{k=0}^\infty \frac{1}{k!}A^k .
% A^0 is the identity matrix with the same dimensions as A. As a practical numerical method, this approach is slow and inaccurate if norm(A) is too large.

A = Asave;

% Taylor series for exp(A)
E = zeros(size(A));
F = eye(size(A));
k = 1;

while norm(E+F-E,1) > 0
   E = E + F;
   F = A*F/k;
   k = k+1;
end

E2 = E

%
%% Method 3: Eigenvalues and Eigenvectors
% expmdemo3 assumes that the matrix has a full set of eigenvectors V such that A=\textrm{VD}V^{-1}. The matrix exponential can be calculated by exponentiating the diagonal matrix of eigenvalues:
% e^A = Ve^D V^{-1} .
% As a practical numerical method, the accuracy is determined by the condition of the eigenvector matrix.

A = Asave;

[V,D] = eig(A);
E = V * diag(exp(diag(D))) / V;

E3 = E

%
%% Compare Results
% For the matrix in this example, all three methods work equally well.

E = expm(Asave);
err1 = E - E1
err2 = E - E2
err3 = E - E3

%
%% Taylor Series Failure
% For some matrices the terms in the Taylor series become very large before they go to zero. Consequently, expmdemo2 fails.

A = [-147 72; -192 93];
E1 = expmdemo1(A)
E2 = expmdemo2(A)
E3 = expmdemo3(A)

%
%% Eigenvalues and Eigenvectors Failure
% Here is a matrix that does not have a full set of eigenvectors. Consequently, expmdemo3 fails.

A = [-1 1; 0 -1];
E1 = expmdemo1(A)
E2 = expmdemo2(A)
E3 = expmdemo3(A)

%
% Copyright 2004-2012 The MathWorks, Inc.