%
%   LINADEMO.M    Linear Algebra Applications in MATLAB 
%
%   This file simply demonstrates several linear algebra operations that
%   can be performed in Matlab.  This area is, in fact, one of Matlab's
%   greatest strengths, since the base element is a 2-d array and all 
%   operations are, by default, matrix manipulations. In addition to the 
%   basic arithmetic operations, Matlab also has m-files for just about 
%   any matrix application or operation you can imagine.  This demo just
%   illustrates a few of Matlab's capabilities.
%
%   File prepared by J. R. White, UMass-Lowell  (July 2003).
%

%
%   Getting started
      clear all, close all
%
%   Open diary file for saving solutions 
      delete linademo.out
      diary linademo.out
      format compact
      disp(' *** LINADEMO.OUT ***   Diary File for LINADEMO.M ')
      disp(' ')
%
%   Define some matrices for the sample problems which follow
      disp('Matrices for sample manipulations')
      x = [2 1 -1]',      y = [0 -4 3]'
      A = [1 0 5; 0 4 -2; 1 3 2],      B = [6 -2 2; 8 3 2]
%
%   Now let's perform several arithmetic operations
      disp('Find inner product of x and y');                    xx = x'*y
      disp('Find outer product of x and y');                    XX = x*y'
      disp('Try getting the row and column dimensions of XX');  [nr,nc] = size(XX)
      disp('Note that A*B is undefined, but we can do B*A');    C = B*A
      disp('What is the size of B transposed * B?');            size(B'*B)
      disp('How about the size of B * B transposed?');          size(B*B')
%
%   We can also extract portions of a matrix (using the repeat operator (the :) )
      disp('The first column of A is');                           a1 = A(:,1)
      disp('Or the first and third rows of A can be extracted');  a13 = A(1:2:3,:)       
%
%   Working with systems of equations is also easy
      disp('The rank of A is');            rankA = rank(A)
      disp('The determinant of A is');     detA = det(A)
      disp('The inverse of A is');         invA = inv(A)
      disp('The solution to Az = y can be found as z = invA*y');      z1 = invA*y
      disp('Or, more efficiently, with an LU decomposition scheme');  z2 = A\y  
      disp('We can see the components of the LU decomp scheme');      [L,U,P] = lu(A)
      disp('With this form, we should have L*U = P*A, or L*U - P*A = 0'); ZZ = L*U-P*A
%
      disp(['Also if you do not know how to use a command, just type ' ...
           '"help command name".']);
      disp('For example, help lu gives');  help lu   
%
%   Finding eigenvalues and eigenvectors is also straightforward
      disp('The eigenvalues & eigenvectors of A are');      [M,D] = eig(A)
      disp(['For distinct eigenvalues, M should satisfy the similarity ' ...
            'transformation, D = invM*A*M.  Let us see!']);  DD = inv(M)*A*M
%
%   Well this demo could go on and on, so let's finish by simply closing the diary file
      disp('And so on, and so on, and so on, ...');
      diary off
%   
%   end of demo
