% alex gerbessiots Exam1 Spring 2015
% from the MATLAB command window run
% clear;clc
% e1101s15
%
% follow instructions
%
function e1101s15()
 global X Y R S W ;
 X = [ 2 1 2 1; 2 3 3 2 ; 3 4 4 3];
 Y = [ 1 1 2 ; 2 1 1 ; 1 1 1 ];
 Z = [ 1 2 1 2];
 R = [ 1 ; 2 ; 1 ];
 S = [ 1 2 3 ];

fprintf('1.  *******************  Problem 1. (48 points= 4x12)\n')
      ex1p1;
%      input('Press ENTER to Continue');
%
%
fprintf('2.  *******************  Problem 2. (30 points)\n')
      ex1p2;
%      input('Press ENTER to Continue');
%
%
fprintf('3.  *******************  Problem 3. (60 points)\n')
      ex1p3;
      %input('Press ENTER to Continue');
%
%
fprintf('4.  *******************  Problem 4. (16 points) \n')
      ex1p4;
      %input('Press ENTER to Continue');
%
%
fprintf('5.  *******************  Problem 5. (24 points)\n')
      ex1p5;
      %input('Press ENTER to Continue');
%
%
fprintf('6.  *******************  Problem 6. (24 points)\n')
      ex1p6;
      %input('Press ENTER to Continue');
%
%
fprintf('7.  *******************  Problem 7. (24 points)\n')
      ex1p7;
      %input('Press ENTER to Continue');
%
%
fprintf('8.  *******************  Problem 8. (24 points)\n')
      ex1p8;
      %input('Press ENTER to Continue');
%
%
end

%%%%%
%%%%%
  function ex1p1
   global X Y Z R  S;

    fprintf('1.1 *********  1KiB     = %d\n', 1024)
    fprintf('1.2 *********  1Kb      = %s\n', 'nonsense')
    ans=double (1); zorro=whos('ans');
    fprintf('1.3 *********  1double  = %d\n', zorro(1).bytes)
    ans=uint16(1); zorro=whos('ans');
    fprintf('1.4 *********  1uint16  = %d\n', zorro(1).bytes)
    ans=logical(1);zorro=whos('ans');
    fprintf('1.5 *********  1logical = %d\n', zorro(1).bytes)
    fprintf('1.6 *********  int8      :  min =  %d , max= %d values = %d \n', int8(-128), int8(127),256);
    fprintf('1.7 *********  Y(end-1,end-1) = %d\n', Y(end-1,end-1));
    fprintf('1.8 *********  X(end-3)       = %d\n', X(end-3)     );
    fprintf('1.9 *********  49 in hexadecimal = %s\n', dec2base(49,16));
    fprintf('1.10*********  8 bits    \n');
    clear j;
    fprintf('1.11*********  j*j = %d\n',  j*j );
    fprintf('1.12*********  49 in binary      = %s\n', dec2base(49,2 ));
    %

  end


  function ex1p2
   global X Y Z R  S;
  
   p2a=   5<5 <5 ;             ;zorro=whos('p2a');
   fprintf('2a          p2a  = %2d  %2d x %2d  %2d    %s\n', ...
          p2a, zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);

   p2b=   int8(5<5 ) ;         ;zorro=whos('p2b');
   fprintf('2b          p2b  = %2d  %2d x %2d  %2d    %s\n', ...
          p2b, zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
  
   p2c=   5== 5+ 5 ; zorro=whos('p2c');
   fprintf('2c          p2c  = %2d  %2d x %2d  %2d    %s\n', ...
          p2c, zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);

   p2d=   int32(5*5-10/5) ; zorro=whos('p2d');
   fprintf('2d          p2d  = %2d  %2d x %2d  %2d    %s\n', ...
          p2d, zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
  
   p2e=   11:-2:1 ;zorro=whos('p2e');
   fprintf('2e          p2e  = %2s  %2d x %2d  %2d    %s\n', ...
          mat2str(p2e), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);

   p2f=   transpose(R)*R ;zorro=whos('p2f');
   fprintf('2f          p2f  = %s  %2d x %2d  %2d    %s\n', ...
          mat2str(p2f), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);


  end


  function ex1p3
   global X Y Z R  S;
  
   p3a= 2: 3 :10;
   zorro=whos('p3a');
   fprintf('3a  p3a  = %-19s  \n', mat2str(p3a));
%   fprintf('3a  p3a  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3a), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3b= size(X);
   zorro=whos('p3b');
   fprintf('3b  p3b  = %-19s  \n', mat2str(p3b));
%   fprintf('3b  p3b  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3b), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3c=length(X);
   zorro=whos('p3c');
   fprintf('3c  p3c  = %-19s  \n', mat2str(p3c));
%   fprintf('3c  p3c  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3c), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3d= 2*eye(3)+ones(3)+1;
   zorro=whos('p3d');
   fprintf('3d  p3d  = %-19s  \n', mat2str(p3d));
%   fprintf('3d  p3d  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3d), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3e=sum(X);
   zorro=whos('p3e');
   fprintf('3e  p3e  = %-19s  \n', mat2str(p3e));
%   fprintf('3e  p3e  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3e), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3f=diag(X);
   zorro=whos('p3f');
   fprintf('3f  p3f  = %-19s  \n', mat2str(p3f));
%   fprintf('3f  p3f  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3f), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3g=X(end, 2:end);
   zorro=whos('p3g');
   fprintf('3g  p3g  = %-19s  \n', mat2str(p3g));
%   fprintf('3g  p3g  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3g), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3h=Y .* transpose(Y);
   zorro=whos('p3h');
   fprintf('3h  p3h  = %-19s  \n', mat2str(p3h));
%   fprintf('3h  p3h  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3h), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3i=diag(diag(X));
   zorro=whos('p3i');
   fprintf('3i  p3i  = %-19s  \n', mat2str(p3i));
   %
   p3j=sum(X(:));
   zorro=whos('p3j');
   fprintf('3j  p3j  = %-19s  \n', mat2str(p3j));
   %
   p3k=transpose(S) * S;
   zorro=whos('p3k');
   fprintf('3k  p3k  = %-19s  \n', mat2str(p3k));
   %
   p3l= S*R ;
   zorro=whos('p3l');
   fprintf('3l  p3l  = %-19s  \n', mat2str(p3l));
   %
   p3m= S * transpose(S);
   zorro=whos('p3m');
   fprintf('3m  p3m  = %-19s  \n', mat2str(p3m));
   %
   p3n= 2:-1:2;
   zorro=whos('p3n');
   fprintf('3n  p3n  = %-19s  \n', mat2str(p3n));
   %
   p3o=length('1234');
   zorro=whos('p3o');
   fprintf('3o  p3o  = %-19s  \n', mat2str(p3o));
   %

   end

  function ex1p4
   global X Y Z R  S;
  
   p3k=transpose(S) * S;
   zorro=whos('p3k');
   fprintf('4a Variable p3k  : Size  %2d x %2d  %2d    %s\n', ...
           zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   %
   p3m= S * transpose(S);
   zorro=whos('p3m');
   fprintf('4b Variable p3m  : Size  %2d x %2d  %2d    %s\n', ...
           zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %

  end


  function ex1p5
   global X Y Z R  S;
   fprintf('5a Column Major order of Y is   %30s\n', mat2str(Y(:)));
   p5=Y';
   fprintf('5b Row    Major order of Y is   %30s\n', mat2str(p5(:)));
  end

  function ex1p6
   global X Y Z R  S;
   B=X;disp(B);
   fprintf('6a  Copy X into B: B=X  %19s\n',mat2str(B));
   fprintf('6b  Second ColofB: B(:,2) = %19s\n',mat2str(B(:,2)));
   fprintf('6c  Second RowofB: B(2,:) = %19s\n',mat2str(B(2,:)));
   B(2,:)=[];
   fprintf('6d  Del 2ndRowofB: B(2,:) =[]; B= %19s\n',mat2str(B));
   fprintf('6e  X(:) is column major X as vector  %19s\n',mat2str(X(:)));
   fprintf('6f  (2:16).^2 is %19s\n',mat2str((2:16).^ 2));
  end


  function ex1p7
    p7a=10;
    p7b=20;
    p7c=0;
    fprintf('7i  Initially  p7a = %2d p7b= %2d p7c = %2d;',p7a,p7b,p7c);
    p7c=p7a;
    p7a=p7b;
    p7b=p7c;
    fprintf('At the end p7a = %2d p7b= %2d p7c = %2d \n',p7a,p7b,p7c);
    p7d=10;p7e=5; p7f=5;
    fprintf('7ii Initially  p7d = %2d p7e= %2d p7f = %2d;',p7d,p7e,p7f);
    p7d= p7d+p7e+p7f;
    p7e= p7d+p7f;
    p7f= p7d+p7e+p7f;
    fprintf('At the end p7d = %2d p7e= %2d p7f = %2d \n',p7d,p7e,p7f);
  end


  function ex1p8
    n=input('Order of approximation ');
    a= [ 1 1:n];
    a= cumprod(a);
    b= 1./ a;
    fprintf('p8: b= 1./a\n');
    c= sum(b);
    fprintf('p8: c= sum(b) = %20.18f\n',c);
  end
