Home > sp > gambler.m

gambler

PURPOSE ^

gambler.m sets up the transition matrix for Gambler's wealth.

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

 gambler.m  sets up the transition matrix for Gambler's wealth.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 % gambler.m  sets up the transition matrix for Gambler's wealth.
0002 N=30;          % total wealth at the table
0003 p=0.5;         % probability of increasing wealth by 1
0004 q=1-p;
0005 
0006 P=zeros(N+1);  % set up transition matrix
0007                % rows 1..31 correspond to wealths 0..30
0008 for i=2:30,
0009    P(i,i-1)=q;
0010    P(i,i+1)=p;
0011 end
0012 P(1,1)=1;      % stay at 0 wealth
0013 P(N+1,N+1)=1;    % stay at wealth N
0014 
0015 mu=zeros(1,N+1);  % set up initial distribution
0016 mu(10)=1;         % start with wealth 10
0017 
0018 
0019 x=-0.5:1:30.5;
0020 y=x;
0021 
0022 subplot(3,2,1);
0023 
0024 pcolor(x, y, [[P zeros(N+1,1)]' zeros(N+2,1)]');
0025 axis ij;
0026 title('Graphical representation of transition matrix P');
0027 xlabel('Final state');
0028 ylabel('Initial state');
0029 %shading flat
0030 
0031 subplot(3,2,3);
0032 
0033 pcolor(x, y, [[P^2 zeros(N+1,1)]' zeros(N+2,1)]');
0034 axis ij;
0035 title('Graphical representation of P^2');
0036 xlabel('Final state');
0037 ylabel('Initial state');
0038 %shading flat
0039 
0040 subplot(3,2,5);
0041 
0042 pcolor(x, y, [[P^4 zeros(N+1,1)]' zeros(N+2,1)]');
0043 axis ij;
0044 title('Graphical representation of P^{4}');
0045 xlabel('Final state');
0046 ylabel('Initial state');
0047 %shading flat
0048 
0049 subplot(3,2,2);
0050 
0051 pcolor(x, y, [[P^20 zeros(N+1,1)]' zeros(N+2,1)]');
0052 axis ij;
0053 title('Graphical representation of P^{20}');
0054 xlabel('Final state');
0055 ylabel('Initial state');
0056 %shading flat
0057 
0058 subplot(3,2,4);
0059 
0060 pcolor(x, y, [[P^225 zeros(N+1,1)]' zeros(N+2,1)]');
0061 axis ij;
0062 title('Graphical representation of P^{225}');
0063 xlabel('Final state');
0064 ylabel('Initial state');
0065 %shading flat
0066 
0067 subplot(3,2,6);
0068 
0069 pcolor(x, y, [[P^2000 zeros(N+1,1)]' zeros(N+2,1)]');
0070 axis ij;
0071 title('Graphical representation of P^{2000}');
0072 xlabel('Final state');
0073 ylabel('Initial state');
0074 %shading flat
0075 
0076 
0077 % use black for probability 1, white for probability 0 by reversing the colormap
0078 map = colormap('gray');
0079 map = flipud(map);
0080 colormap(map);
0081 
0082 
0083 % Helpful code for displaying matrices
0084 
0085 %fprintf('%4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f\n', P')
0086 
0087 %A=P^225;
0088 %A=[(0:30)' A];
0089 
0090 %fprintf('%2d | %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f |\n', A')
0091

Generated on Mon 17-Mar-2008 10:33:42 by m2html © 2003