% M21-126 Intro to Math Software
% Spring 2000
% Dr. David Handron

% lesson11.m (Matlab: Boolean Variables and Programming)

		% This next command causes all the commands below to be shown
echo on		% echo off will only print out the answers.

% To use this file, start up matlab and issue the command
% >> lesson11

% To quit Matlab, issue the command >> quit


% To continue after a pause statement, hit any key to continue, or
% hit (control) C to terminate the execution of a ".m" file like this.

pause
% RELATIONAL OPERATORS

% In Matlab, False is represented by zero and True by ANY non-zero number
% and Matlab functions will always use 1 to represent true.

A = 1:9, B = 9-A	% Construct a couple of arrays
pause 

b1 = A >  4		% This constructs a Boolean array ...
b2 = A <= B		% ... as does this
pause

% Notice that Boolean variables are a distinct type in Matlab 

whos
pause

A  + b1			% However, Booleans will behave like numbers
A .* b1
pause

% The Booleans operators are <, <=, >, >=, and == (equal) and ~= (not equal).

A = -2:2, B = 0:4	% A couple of arrays
eps			% Smallest positive floating point increment of 1
pause

(A==0)*eps		% An array with eps where A has zeros
pause

C = B ./ (A + (A==0)*eps)	% This eliminates division by zero
C .* (C < 1/eps)		% These are the "finite" values
pause

% LOGICAL OPERATIONS

% Boolean variables can be manipulated with & (and), | (or), and ~ (not)

A = (1:3)' * (2:4)	% An array
pause

b1 = (4 < A) & (A < 8)	% Boolean array where 4 < A[i] < 8
~b1
pause

any(b1)			% Which columns of b1 contain non-zero elements
all(b1)			% Which columns of b1 contain only non-zero elements
pause

% There are many functions with names of the form is...()

isprime(1:12)		% True where prime
isprime(1:12) .* (1:12)	% These are the primes
isieee			% Is the computer using IEEE standard arithmetic
pause

% FLOW CONTROL & LOOPS

% Matlab for statements have the form: for i=<Matrix> <statements> end
% During the loop, i gets assigned to the columns of the <Matrix>
% Note: when the echo is on, for statements print out lots of stuff
% so I'll switch if off
% 
% for i=-1:3 
%   fprintf('i = %d\n', i) 
% end
% echo on

echo off
for i=-1:3 
  fprintf('i = %d\n', i) 
end
echo on
pause

A			% Recall A
pause

% I'll switch the echo off for other loops
%
% for i=A			% Columns of A
%   i.^2
% end

echo off
for i=A			% Columns of A
  i.^2
end
echo on
pause

% Matlab if statements take the form:
% if <test> <statements> elseif <statements> .... else <statements> end
%
% for i=-1:5
%   if 	 i <= 0   fprintf('%d is negative\n',i)
%   elseif rem(i,2) fprintf('%d is even and positive\n',i)
%   else	 	  fprintf('%d is  odd and positive\n',i)
%   end
% end

echo off
for i=-1:5
  if 	 i <= 0   fprintf('%d is negative\n',i)
  elseif rem(i,2) fprintf('%d is even and positive\n',i)
  else	 	  fprintf('%d is  odd and positive\n',i)
  end
end
echo on
pause

% CASE STUDY - MARKOV SEQUENCES
%
% Consider a gambler playing the the following game:
% a) If the gambler is broke ($0) then she can't play so ends up with $0
% b) If the gambler has $4 she wins and keeps her fortune of $4
% c) If the gambler has $x for 0 < x < 4, then she bets, and wins
%    with probability p to get $(x+1), and looses with probability (1-p)
%    to end up with $(x-1).
%
% We can use a vector pp = [p0,p1,p2,p3,p4] to represent the
% probability that the gambler has $0, $1, $2, $3 and $4. After
% one round of the game, the expected probabilities become (pp T)
% where the transition matrix T is (for p=1/3):
pause

p = 1/3
T = [1 0 0 0 0; 1-p 0 p 0 0; 0 1-p 0 p 0; 0 0 1-p 0 p; 0 0 0 0 1]
pause

% For example, if the gambler starts with $2 then pp = [0 0 1 0 0]
% represents that with probability one she has $2. Then after one round
% of the game she has

pp = [0,0,1,0,0]
pp * T

% That is, 1/3 of the time she has $1 and 2/3 of the time she has $3.
pause

% After two rounds the probabilities become

(pp * T) * T

% The parentheses are unnecessary in the above.
pause

% We can animate the probable state of the gambler as a function of
% time using a for loop to generate the states.
%
% We first plot the initial vector and get the axis limits:

xx = 0:4
bar(xx,pp)
ax = axis		% Get the axis limits
pause

% The loop command to plot the states is:
%
% echo off
% for i=1:20
%   bar(xx,pp);    % Plot the state
%   pp = pp * T;   % Update the state
%   axis(ax);  	   % Set the axis limits so the axes don't jump about
%   pause(1.0);    % Pause to display
% end;
% echo on
pause

echo off
for i=1:20
  bar(xx,pp); 	 % Plot the state
  axis(ax);  	 % Set the axis limits so the axes don't jump about
  pp = pp * T;   % Update the state
  pause(1.0);	 % Pause to display
end;
echo on

% This shows that if the gambler starts with $2, then 80% of the time
% she goes broke, and 20% of the time doubles her money.
