%Drunk Sailor Problem. Matrix model.
m=5;%number of steps in the bar
d=.5;
%Calculation of the matrix of transition probabilities
P=zeros(m+1,m+1);
for i=1:m;P(i,i+1)=d;P(i+1,i)=d;end;P(2,1)=1;P(m,m+1)=0;
for i=1:m+1;P(i,i)=1-sum(P(:,i));end
%The initial state vector
S0=zeros(m+1,1);S0(1)=1;
S(:,1)=S0;
N=100;%Number of steps of sailor
%Game itself
for i=1:N
    S(:,i+1)=P*S(:,i);
end
Sd=diff(S(m+1,:));%Calculation of differential output
im=[1:N]*Sd'%mean number of steps before leaving
figure(1);colormap(white);bar3(S);axis([ 1 N .5 m+1+.5 0 1]);set(gca,'DataAspectRatio',[7 1 .2]);ylabel('cell number');
xlabel('transition number');zlabel('probability')
figure(2);stairs(S(m+1,:),'k');axis([ .5 N+.5 0 1]);grid on;xlabel('transition number');ylabel('probability to leave');
figure(3);stairs(Sd,'k');hold on;line([im im],[0 max(Sd)],'linewidth',2);grid on;
xlabel('transition number');ylabel('probability to leave at the i-th step')