% MATLAB SCRIPT FILE s322hw24.m  */
%    SOLUTION TO HW 2, PART 4,  ABOUT DEBUGGING SCRIPTS
%    DUE SEPTEMBER 5, 1996
note = 'Running MATLAB script file s322hw24.m' 


%***  THE FOLLOWING ARE THE ORIGINAL CODE LINES,
%***  THOSE WITH ERRORS ARE COMMENTED OUT, THEN A COPY
%***  IS MADE, FOLLOWED BY AN EXPLANATION STARTING WITH:  %***


  %MATLAB SCRIPT FILE s322eg1.m  */
  %
  %    EXAMPLE 2 FOR STATISTICS 322
  %    POSTED 8/27/96
  %    ILLUSTRATES COMMON "BUGS"

  %    THIS MATLAB SCRIPT IF FULL OF ERRORS, WHICH YOU SHOULD FIX 
  %    TO MAKE IT RUN.


%  'MATLAB USUALLY GIVES YOU THE LINE NUMBERS
   'MATLAB USUALLY GIVES YOU THE LINE NUMBERS'
%***  ADDED A SINGLE QUOTE AT THE END
  'WHERE ERRORS OCCUR'


  x = 1 ;
%  y = 2 :
   y = 2 ;
%****  CHANGED THE COLON TO A SEMICOLON
  z = 3 ;
            %  THIS ONE CAN BE REALLY TOUGH TO SPOT


%  a = ones[2,3] ;
   a = ones(2,3) ;
%***  ones IS A COMMAND, WHOSE ARGUMENTS NEED TO BE IN ()'S
%  b = a[2,2] ;
   b = a(2,2) ;
%***  SUBMATRICES ARE INDEXED BY ()'S
%  c = {3 * a + (27 - b)} ;
   c = (3 * a + (27 - b)) ;
%***  ONLY ()'S WORK LIKE PARENTHESES!
             %  [] AND () HAVE VERY SPECIFIC USES! 


%  SQRT(-1) 
   sqrt(-1) 
%***  ALL MATLAB COMMANDS WORK IN LOWER CASE
            %  RECALL "CASE SENSITIVITY"? 
            %  AFTER THIS ONE, MATLAB IS LEFT HANGING.  GET THE PROMPT
            %  BACK WITH (CTRL-BREAK), OR BY TYPING   "end"


             %  CAPITAL LETTER VARIABLES ARE OK, BUT.....
  TOP = 5 ;
  BOTTOM = 1 ;
%  RANGE = T0P - B0TT0M
   RANGE = TOP - BOTTOM
%***  THE O (OH) WAS REPLACED BY A 0 (ZERO)


  sqm1 = sqrt(-1)
  lnm1 = log(-1)
            %  NOTE MATLAB GOES IMMEDIATELY TO COMPLEX ARITHMETIC
            %  WHICH CAN BE RATHER SURPRISING, WHEN YOU DON'T EXPECT IT
            %  THIS CAN BE DEBUGGED USING COMMANDS LIKE
  x = [-1 ; 1] * (2:(-3):(-4)) 
  sqx = sqrt(x) 
  flagimag = (abs(imag(sqx)) > 0)
%***  THIS PUTS A ONE IN EACH PURELY IMAGINARY LOCATION


  note = 'any key to continue'
  pause ;
            %  NO MISTAKE HERE, JUST LOOK AT THE ABOVE


  x = [-1 ; 1] * 2:(-3):(-4) 
  x = [-1 ; 1] * (2:(-3):(-4)) 
            %  WHY ARE THESE TWO DIFFERENT?
%***  PROBLEM HERE IS "PRECEDENCE OF OPERATORS".  SOME OF THESE
%***  ARE NOT OBVIOUS AND CAN GIVE UNEXPECTED RESULTS.  THEY CAN
%***  BE AVOIDED THROUGH USE OF PARENTHESES


  a = 4 ;
%  if a = 4 ;
   if a == 4 ;
%***  PROBLEM WAS NEED DOUBLE = SIGN FOR CONDITIONAL
%    note = "got into first conditional!" 
     note = 'got into first conditional!' 
%***  NEED SINGLE QUOTES INSTEAD OF DOUBLE QUOTES
%  emd ;
   end ;
%***  end WAS MISSPELLED
            %  THIS ONE HAS A DOUBLE ERROR!
            %  NOTE THE EFFECT OF THE MISPELLING


  a = 4 ;
  b = -7 ;
  if a == 4 ;
    if b == -7 ;
      note = 'got into second conditional!' 
    end ;
%***  THIS LINE HAS BEEN ADDED, NOTE THE INDENTATION MAKES IT
%***  CLEAR THIS IS NEEDED.
  end ;
            %  THIS IS A LITTLE TRICKIER, BECAUSE YOU GET
            %  NO LINE NUMBER.  ANOTHER "end" IS NEEDED


  note = 'have you ever seen an infinite loop?'
  note = 'kill it with (CTRL-C)'
  i = 1 ;
  while i < 5 ;
    progress = ['still have i = ', num2str(i), '   (CTRL-C) to kill this']
    i = i + 1 ;
%***  ADDING THIS LINE MEANS THE COUNTER i WILL ACTUALLY 
%***  GET UP TO 5, AND QUIT
  end ;


  note = 'infinite loops with no write statements in them are'
  note = 'especially unpleasant, because you dont see that it'
  note = 'is happening.  Again kill with (CTRL-C)'
  note = 'any key to continue'
  pause ;
  i = 1 ;
  x = 1 ;
  while i < 5 ;
    x = x + 1 ;
    i = i + 1 ;
%***  ADDING THIS LINE MEANS THE COUNTER i WILL ACTUALLY 
%***  GET UP TO 5, AND QUIT
  end ;


%  CONGRATULATIONS, YOU FIXED ALL OF THE BUGS
  'CONGRATULATIONS, YOU FIXED ALL OF THE BUGS'
%***  ADDED QUOTES TO MAKE THIS APPEAR ON THE SCREEN


            %  NOW TRY THIS: HOW LARGE A MATRIX CAN YOU CREATE, AND
            %  WHAT HAPPENS WHEN IT IS TOO BIG?
  big0mat = zeros(10000,1) ;
            %  START WITH COLUMN OF 0'S
  while size(big0mat,2) < 10^10 ;
    progress = ['working on column ' num2str(size(big0mat,2))]
    big0mat = [big0mat zeros(10000,1)] ;
            %  APPEND NEW COLUMN OF ZEROS
  end ;
            %  TRY THE COMMAND  >> whos  AFTER RUNNING THIS.
            %  WHY COULD  >> clear  BE USEFUL NOW?


