%% Matrix Trace Properties
% Verify several properties of the trace of a matrix (up to round-off error).
%  Create two matrices. Verify that  \textrm{tr}\left(A+B\right)=\textrm{tr}\left(A\right)+\textrm{tr}\left(B\right).

A = magic(3);
B = rand(3);
trace(A+B) 
trace(A) + trace(B)

%
% Verify that \textrm{tr}\left(A\right)=\textrm{tr}\left(A^{\left.T\right)} \right).

trace(A)
trace(A')

%
% Verify that \textrm{tr}\left(A^T B\right)=\textrm{tr}\left({\textrm{AB}}^T \right).

trace(A'*B) 
trace(A*B')

%
% Verify that \textrm{tr}\left(\textrm{cA}\right)=c\;\textrm{tr}\left(A\right) for a scalar c.

c = 5;
trace(c*A) 
c*trace(A)

%
% Verify that the trace equals the sum of the eigenvalues \textrm{tr}\left(A\right)=\sum_i \lambda_i.

trace(A)
sum(eig(A))

%
% Copyright 2018 The MathWorks, Inc.