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

% lesson12.m (Introduction to Matlab)

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

% 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

% FUNCTIONS and M-FILES

% As with any other programming environment, is is possible to encapsulate
% groups of instructions into functions. However, unlike most other
% languages, Matlab was designed with the idea that each function would
% go into it's own file and that the name of the file would be the
% name of the function with ".m" attached. For example, a function
% called ff() would be place in file ff.m.

% When you type in a function name, such as ff(), Matlab first looks
% in your "current" directory which is the one from which you started
% Matlab from.
pause

% SCRIPT FILES

% If you put a set of Matlab instructions into a file with a ".m"
% extension (e.g. lesson9.m), then typing "lesson9" at the Matlab
% prompt will execute the commands. Such files are called "script
% files" in the Matlab documentation. All of your online Matlab
% lessons are script files.
pause

% FUNCTION FILES

% If a ".m" file begins with the word "function" in the first line,
% the file is taken to be a Matlab function. To illustrate the format
% of such files, put the following commands into mm.m (with the %'s
% removed of course).

% function y = mm(x)
% [m,n] = size(x);
% if m == 1 m = n; end
% 
% y = sum(x)  / m;
pause

% 1) Putting in all of the semi-colon's prevents Matlab from printing
%    all of the intermediate values (m, n, etc.)

% 2) Notice that the name of the file and function name "mm()" agree

% 3) The mean of the vector x is computed, and returned in the variable
%    labeled y (from the first line)
pause

% Let's test this out:

x1 = [1,2,3]
x2 = [1;2;3]
pause

mm(x1)
mm(x2)
pause

% The if statement in the function mm() was put in to make the function
% correctly compute the mean of both column and row vectors. What happens
% with matrices? 

A = [1,2,3; 4,5,6; 7,8,9]
mean(A)
pause

% If you read the help for sum ("help sum") you'll find that sum()
% returns the row of column sums of a matrix, so we get the mean
% of the columns.

% We can also define functions like size() which return two (or more)
% variables which can be assigned like:

[m,n] = size(A)
pause

% As an example, let's compute both the mean and standard deviation:

% function [mean,stdev] = stat(x)
%  [m,n] = size(x);
%  if m==1 m = n; end
%  
%  mean  = sum(x) / m;
%  stdev = sqrt(sum(x.^2)/m - mean.^2);
pause

stat(x1)
[m,s] = stat(x2)
pause

[m,n] = stat(A)

% Notice that stat() used the variable names mean and stdev. These names
% are private to the stat() will not cause conflicts with like named
% variables in other functions or your own working environment.
pause

% CASE STUDY - MARKOV SEQUENCES (Cont.)
%
% 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 T is the transition matrix introduced last week.
pause

% We studied this problem last week. Let us write a function state(pp,n)
% that computes the probable state of the gambler after n tosses of the
% coin.
% function pp = state(p0,n)
%
%  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];
% 
%  [k,l] = size(p0);
% 
%  if     ((k==1) & (l==5)) pp = p0 * T^n;
%  elseif ((k==5) & (l==1)) pp = (T')^n * p0;
%  else error('Vector must be 5x1 or 1x5');
%  end
pause

pp = [0 0 1 0 0]
state(pp,1)
pause

pp = [0; 0; 1; 0; 0;]
state(pp,1)
pause

% Let us now encapsulate the animation commands that we developed into
% a function
%
% function pp = stplt(p0,n)
%
% xx = 0:4;
% pp = p0;
%
% bar(xx,pp);
% ax = axis;		% Get the axis limits
%
% for i=1:n
%   pp = state(pp,i);  % use the state function compute the state
%   bar(xx,pp);        % Plot the state
%   axis(ax);  	       % Set the axis limits so the axes don't jump about
%   pause(1.0);        % Pause to display
% end;
pause

pp = [0 0 1 0 0]
stplt(pp,10)

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

