%MATLAB SCRIPT FILE s322eg3.m  */
%
%    EXAMPLE 3 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 
'WHERE ERRORS OCCUR'


x = 1 ;
y = 2 :
z = 3 ;
          %  THIS ONE CAN BE REALLY TOUGH TO SPOT


a = ones[2,3] ;
b = a[2,2] ;
c = {3 * a + (27 - b)} ;
           %  [] AND () HAVE VERY SPECIFIC USES! 


SQRT(-1) 
          %  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


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)


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?


a = 4 ;
if a = 4 ;
  note = "got into first conditional!" 
emd ;
          %  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 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']
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 ;
end ;


CONGRATULATIONS, YOU FIXED ALL OF THE BUGS


          %  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?
