Home > sp > chutesladders.m

chutesladders

PURPOSE ^

chutes.m sets up and displays various matrices relating to Chutes and Ladders

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

 chutes.m sets up and displays various matrices relating to Chutes and Ladders

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 % chutes.m sets up and displays various matrices relating to Chutes and Ladders
0002 
0003 A=zeros(102,102);     % blank matrix to work with
0004                       % state 102 is fictitious, to help with pcolor below
0005 
0006 for i=1:100,      
0007   for j=(i+1):(i+6),  % as if just rolling the die, no chutes or ladders
0008     if j>100 
0009        k=i;           % stay where you are; exact spin to get to 100
0010     else
0011        k=j;
0012     end
0013     A(i,k)=A(i,k)+1/6;
0014   end
0015 end    
0016 
0017 for j=1:6,           % state 101 is where you are before the game starts
0018   A(101,j)=1/6;
0019 end
0020 
0021 ladder = [[1 38]; [4 14]; [9 31]; [28 84]; [21 42]; [36 44]; [51 67]; [71 91]; [80 100]];
0022 chute  = [[98 78]; [95 75]; [93 73]; [87 24]; [64 60]; [62 19]; [56 53]; [49 11]; [47 26]; [16 6]];
0023 both = [ladder; chute];
0024 
0025 CL = eye(102);
0026 CL(both(:,1),:) = CL(both(:,2),:);         % the chutes and ladders matrix
0027 
0028 P=A*CL;              % the transition matrix
0029 
0030 i=[101 1:100 102];       % put 101 as state 0 when necessary for graphing
0031 x=-0.5:1:100.5;
0032 
0033 colormap(1-gray);
0034 
0035 Z = P;
0036 for n=1:400,
0037   F(n) = Z(101,100);
0038   Z = Z * P;
0039 end
0040 
0041 figure(1);
0042 
0043 subplot(2,2,1);
0044 pcolor(x,x,sqrt(sqrt(P(i,i))));
0045 axis ij;
0046 shading flat;
0047 title('The transition matrix P');
0048 
0049 subplot(2,2,2);
0050 pcolor(x,x,sqrt(sqrt(P(i,i)^2)));
0051 axis ij;
0052 shading flat;
0053 title('Two-step transition matrix P^{2}');
0054 
0055 subplot(2,2,3);
0056 pcolor(x,x,sqrt(sqrt(P(i,i)^7)));
0057 axis ij;
0058 shading flat;
0059 title('Seven-step transition matrix P^{7}');
0060 
0061 subplot(2,2,4);
0062 pcolor(x,x,sqrt(sqrt(P(i,i)^30)));
0063 axis ij;
0064 shading flat;
0065 title('Thirty-step transition matrix P^{30}');
0066 
0067 figure(2);
0068 
0069 subplot(2,2,1);
0070 plot(F(1:250));
0071 xlabel('Number of steps');
0072 ylabel('Probability of finishing by this number of steps');
0073 title('One player');
0074 
0075 subplot(2,2,3);
0076 f=[0 diff(F)];
0077 plot(f(1:250));
0078 xlabel('Number of steps');
0079 ylabel('Probability of finishing at exactly this number of steps');
0080 title('One player');
0081 text(80,0.02,'Most likely number is 22');
0082 text(80,0.015,'Average number of steps to finish is 39.2251');
0083 avg=1+sum(1-F)
0084 text(80,0.010,'Standard deviation is 25.2245');
0085 sqrt(sum(f.*((1:400).^2)) - avg^2)
0086 
0087 G = 1-(1-F).^2;
0088 
0089 subplot(2,2,2);
0090 plot(G(1:250));
0091 xlabel('Number of steps');
0092 ylabel('Probability of finishing by this number of steps');
0093 title('Two players');
0094 
0095 subplot(2,2,4);
0096 g=[0 diff(G)];
0097 plot(f(1:250));
0098 xlabel('Number of steps');
0099 ylabel('Probability of finishing at exactly this number of steps');
0100 title('Two players');
0101 text(80,0.02,'Most likely number is 18');
0102 text(80,0.015,'Average number of steps to finish is 26.3310');
0103 avg=1+sum(1-G)
0104 text(80,0.010,'Standard deviation is 13.4887');
0105 text(80,0.005,'First player wins 50.79% of the time');
0106 
0107 sqrt(sum(g.*((1:400).^2)) - avg^2)
0108 
0109 0.5+ 0.5*sum(f.*f)
0110  
0111 break
0112 
0113 t=[1:99, 101];       % index vector of transient states, to pull out Q
0114 Q=P(t,t);            % Pulls out columns and rows 1 to 99 of P
0115 S=inv(eye(100)-Q);   % matrix telling expected number of visits
0116 R=zeros(102,102);    % set up a blank potential matrix
0117 R(t,t)=S;            % the potential matrix, with 0 replacing infinity
0118 
0119 subplot(2,2,4);
0120 pcolor(R);          % Plot the matrix S as a grayscale image
0121 shading flat;
0122 title('Expected number of visits R');
0123 axis ij;
0124 
0125 r=[100];             % index vector of recurrent states, to pull out L
0126 L=P(t,r);            % Pulls out rows t and columns r of P
0127 G=S*L;               % Matrix of absorption probabilities
0128 
0129 break
0130 
0131 i=(0:0.01:1)';       % For fixing the gray-scaling
0132 mymap = (i.^(1/3))*[1 1 1];
0133 colormap(1-mymap);   % this changes the gray-scale levels in a useful way
0134

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