%
%   MISC_STUFF   This demo represents a collection of short Matlab examples
%              that illustrate several programming features and general tips
%   
%   The goal here is to illustrate the proper Matlab commands and syntax needed 
%   for performing several common programming tasks.  This collection of 
%   miscellaneous tips should prove useful in subsequent programming assignments
%   within this course...
%
%   This file is the result of performing all the steps indicated in Lab #4...
%
%   File created by J. R. White, UMass-Lowell (Feb. 2007)
%

%
%   getting started 
      clear all,  close all
%
%   Note: several items will be echoed back to the screen to demonstrate how
%         Matlab responds to the various commands...
      format compact
%
%   scalar versus element by element versus matrix arithmetic 
      disp(' Scalar Arithmetic')
      x = 3,    y = 4
      z1 = 3*x*y/2,   z2 = 4*(y-5)/(3*x-6)
      disp(' paused -- hit any key to continue'), pause
%
      disp(' Element by Element Arithmetic with vectors') 
      xx = [3 1 0],    yy = [0 1 2]
      zz1 = 3*xx.*yy/2,  zz2 = 4*(yy-5)./(3*xx-6)
      disp(' paused -- hit any key to continue'), pause
%
      disp(' Matrix Arithmetic (no dots) with vectors gives an error') 
      zzz1 = 3*xx*yy/2,  zzz2 = 4*(xx-5)./(3*yy-6)
      disp(' paused -- hit any key to continue'), pause
%
      disp(' Element by Element Arithmetic with 2-D arrays') 
      X = [3 1;3 0],    Y = [0 1;3 4]
      Z1 = 3*X.*Y/2,  Z2 = 4*(Y-5)./(3*X-6)
      disp(' paused -- hit any key to continue'), pause
%
      disp(' Matrix Arithmetic (no dots) - notice that this is completely different') 
      ZZ1 = 3*X*Y/2,  ZZ2 = 4*(Y-5)/(3*X-6)
      disp(' paused -- hit any key to continue'), pause
%
%   array operations (focus on array indexing)
      disp(' Array Indexing')
      A = [1:4;5:8;9:12]
%
      B = A(:,3)                     % extract 3rd column
      C = A(:,[1 3])                 % extract 1st and 3rd columns
      D1 = A(1,:)+A(3,:)             % add 1st and 3rd rows (option #1)
      D2 = sum(A([1 3],:))           % add 1st and 3rd rows (option #2)
      AA = A;  AA(2,3) = -7          % modify single element
      BB = [A([1 3],:); AA(2,:); D1] % form a matrix from parts of other matrices
      disp(' paused -- hit any key to continue'), pause
%
%   work with 5x5 magic matrix
      disp(' 5x5 Magic Matrix -- why is this magic?')
      clear all,   N = 5;  A = magic(N)
      sum(A)                         % sum each column (these are all the same!!!)
      sum(A')                        % sum each row (sum columns after transposing A)
      B1 = diag(A);  sum(B1)         % extract main diagonal and sum (option #1)
      for j=1:N                      % extract main diagonal & sum (opt #2)
        B2(j) = A(j,j); 
      end  
      sum(B2)
      for j=1:N,                     % extract other diagonal and sum
        jj = N-(j-1);
        BB(j) = A(j,jj);
      end
      sum(BB) 
      disp(' paused -- hit any key to continue'), pause
%
%   working with logical arrays
      disp(' Working with Logical Arrays and the FIND command')
      C = A > 10                     % locations in A that are greater than 10
      whos                           % C is a logical variable
      D = A(C)                       % extract elements of A that meet logical test
%
      [I,J] = find(A > 10)           % use of find command to do same thing
      for k = 1:length(I)
        DD(k) = A(I(k),J(k));
      end
      DD                             % DD should have the same values as D
      disp(' paused -- hit any key to continue'), pause
%
%   for loops and while loops  --  when to use?
      disp(' Use a FOR Loop when there is a fixed # of loops')
      N1 = 50;    S1 = 0;
      for i = 1:N1
        S1 = S1 + 2*(i-1);           % note the standard form: ans = ans + term i
      end
      S1
      disp(' Use a WHILE Loop when the # of loops is dependent on some condition')
      S2 = 0;  j = 0;
      while S2 <= 12000
        j = j+1;  
        S2 = S2 + 2*(j-1);
      end
      N2 = j
      disp(' paused -- hit any key to continue'), pause
%
%   working with discontinuous functions
%   option #1 -- single time vector (use if statements to check for proper interval)
      Nt = 501;  t = linspace(0,5,Nt);    % set independent variable
      f = zeros(1,Nt);                    % allocate space for dependent variable
      for i = 1:Nt
        if t(i) <= 1   
          f(i) = 15*exp(-t(i)/2);
        elseif t(i) > 1 & t(i) <= 2
          f(i) = 0.8*15*exp(-t(i)/2);
        else
          f(i) = 0.8*0.8*15*exp(-t(i)/2);
        end
      end
%   option #2 -- multiple time vectors concatenated together
      t1 = linspace(0,1,21);  f1 = 15*exp(-t1/2);    % evaluate over each range
      t2 = linspace(1,2,21);  f2 = 0.8*15*exp(-t2/2);
      t3 = linspace(2,5,61);  f3 = 0.8*0.8*15*exp(-t3/2);
      tt = [t1 t2 t3];  ff = [f1 f2 f3];             % concatenate into single vector
%   plot results from both options
      plot(t,f,'r-',tt,ff,'b.','LineWidth',2),grid
      title('Working with Discontinuous Functions -- An Example')
      xlabel('Time (hours)'),ylabel('Product Amount (grams)') 
      legend('Single Time Vector','Multiple Time Vecors')
%
%   well, there is lots more, but this is already probably more than we can do in one
%   lab session.  Thus, we will end here.  There is a lot of Matlab here, so study
%   these examples carefully, since the more you understand here, the easier
%   subsequent projects will be as we move along this semester...
%
