% Matlab Fundamentals (Ed's random selection :-)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab = "Matrix Laboratory"
% Matlab is an interpreted language (like Basic, 
%  if that means anything to this generation :)
%   That means that you can type in an instruction and execute, or
%   you can run a bunch of instructions in a text file.
%   Unlike in FORTRAN and C++, you don't need to _COMPILE_ your code
%   first, to make a binary machine-language executable file.
%
%  Why might this be an advantage?
%  -------------------------------
%
%  How do I make a line of text that Matlab will not try to execute?
%  -----------------------------------------------------------------

   keyboard

  '''keyboard'' command puts us into debug mode.'
  
  'Now we can step through one line at a time, using Step.' 

%  How do I write text to the screen?
%  ----------------------------------
'How do I write text to the screen?'

 'Put it in single quotes'
%
%
  disp(sprintf('alternatively, use disp(sprintf('' ...''))'))
%
%
%  What sorts of entities can it handle?
%  -------------------------------------
  'What sorts of entities can Matlab handle?'


%  row vector
%  -----------
   'row vector'
  'COMMAND: x = [ 1 2 3 4 ]'
   x = [ 1 2 3 4 ]
   
%  column vector
%  -------------
  'column vector'
  'COMMAND: y = [ 3 4 4 1 ]'' '
   y = [ 3 4 4 1 ]'

   
%  matrix
%  ------
   'matrix'
   x
   y
  'COMMAND: M = [ x''  y  y ]'
   M = [ x'  y  y]
   
%
% Transpose of a general matrix
% -----------------------------
   'Transpose of a general matrix'
  
   'COMMAND:  M_transpose = M'' '
    M
    M_transpose = M'
%



%  Special matrices - all ones or all zeros
%  ----------------------------------------
   'Special matrices - all ones or all zeros'
   
   'COMMAND:  ALL_ONES = ones(3,4)'
     ALL_ONES = ones(3,4)
   
   'COMMAND:  ALL_ZEROS = zeros(4,2)'
     ALL_ZEROS = zeros(4,2)

     
%  How do I know what matrices I have in play?
%  -------------------------------------------
  'How do I know what matrices I have in play?'
    'COMMAND: who'
     who
  'COMMAND: clear  M_transpose  ALL_ZEROS'
     clear  M_transpose  ALL_ZEROS
  'Repeat COMMAND: who'  
     who
     
%  Can I get more information about my variables?
%  ----------------------------------------------
   'Can I get more information about my variables?'
   
   'COMMAND: whos'
     whos


%  What sorts of operations can Matlab do for me?
%  ----------------------------------------------
 'What sorts of operations can Matlab do for me?'
%

%  Vector dot product
%  ------------------
  'Vector dot product'
  x
  y
 'COMMAND: dot_product = x * y'
  dot_product = x * y
  
% Vector outer product
% --------------------
   'Vector outer product'
   y
   x
   'COMMAND: outer_product = y * x '
   outer_product = y * x
%

%  Pointwise product
%  -----------------
   'Pointwise product'
   xx = [ x' y ]'
   yy = [ y  x' ]'
   'COMMAND: pointwise_product = xx .* yy'
   pointwise_product = xx .* yy
%


%  Matrix product
%  --------------
   'Matrix product'
    MATRIX_1 = M
    MATRIX_2 = M' - 2*ones( size(M') )

    'MATRIX_PRODUCT = MATRIX_1 * MATRIX_2'
     MATRIX_PRODUCT = MATRIX_1 * MATRIX_2
 
 'What happens if I multiply 2 matrices in reversed order?'
    'MATRIX_PRODUCT = MATRIX_2 * MATRIX_1'
     MATRIX_PRODUCT = MATRIX_2 * MATRIX_1
    
     
    
%  Inverse of a square 3x3 matrix
%  ------------------------------
   'Inverse of a square nonsingular matrix'
   
   'COMMAND:  Sq_matrix = reshape([1 1 1   2 1 -1   2 4 3 ], 3, 3)'
   
    Sq_matrix = reshape( [1 1 1   2 1 -1   2 4 3 ], 3, 3)
%  
   'COMMAND:  Sq_matrix_inverse = inv( Sq_matrix )'

    Sq_matrix_inverse = inv( Sq_matrix ) 
    
    
%  Is Sq_matrix_inverse really the inverse matrix?
%  -----------------------------------------------
    'Is Sq_matrix_inverse really the inverse matrix?'
    
    'COMMAND: I = Sq_matrix * Sq_matrix_inverse'
    I = Sq_matrix * Sq_matrix_inverse
  
    'COMMAND: I = Sq_matrix_inverse * Sq_matrix'
    I = Sq_matrix_inverse * Sq_matrix


%  Graphics
%  --------
  'Graphics'
  
  'Simple x-y plots'
  'Choose figure to plot on.'
  'COMMAND:  figure(1)'
   figure(1)
  
  'COMMAND:  plot(x,y, ''r+'')'
  plot(x,y, 'r+-')
  
  'Put on grid lines, axis labels'
  'COMMAND: grid on'
   grid on
   
  'COMMAND:  xlabel(''Distance  (m)'')'
   xlabel('Distance  (m)')
   
  'COMMAND:  ylabel(''Temperature  (^oC)'')'
   ylabel('Temperature  (^oC)')
  
  'Adjust plot-axis limits'
  'COMMAND:  set(gca, ''ylim'', [0,5] )'
   set(gca, 'ylim', [0,5] )
   
   
%  Contour plot
%  ------------
   'Contour plot'
   
  
  'COMMAND:  figure(2)'
   figure(2)
  
   M
   
  'COMMAND:  contour(M)'
   contour(M)
  
   
  'Put on a colorbar to give scale'
  'COMMAND:  colorbar'
   colorbar
  
   'Let''s check whether it looks right'
   M
   'hmmm ...  what''s wrong? ...'
   'M(1,1) is in upper-left corner of matrix.'
   'Origin (0,0) is in lower-left corner of a plot.'
    
   'Let''s see whether we plotted  flipped version of M'
   'COMMAND:  M_flipped = flipud(M)'
   M_flipped = flipud(M)
   
  'Bring back Figure 2 to check'
  'COMMAND:  figure(2)'
  figure(2)
  
  
   'Looking for more information?  Just type ''help'''
   help
%
%
%