Simulate a five-state absorbing Markov chain
Show older comments
I am trying to solve this problem :

this my code so far and getting errors:
N=1; % Number of experiments
n=15; % Number of transitions to be computed
X=char(zeros(n,N)); % Each column of X represents one of the N experiments
S=char(n,1); % Initialize the state array
M=zeros(n,5); % M contains the experimental probabilities for states S & R & N
p00=1; p01=0; p02=0; p03=0; p04=0;
p10=0.3; p11=0; p12=0.7; p13=0; p14=0;
p20=0; p21=0.5; p22=0; p23=0.5; p24=0;
p30=0; p31=0; p32=0.6; p33=0; p34=0.4;
p40=0; p41=0; p42=0; p43=0; p44=1;
for j=1:N
s0=randi(4,1);
S(1)=s0;
for k=1:n-1
r=rand(); s=S(k);
if s=='1'
if r<=p12, S(k+1)='2'; end
elseif s=='2'
if r<=p21, S(k+1)='1'; elseif r>p23, S(k+1)='3'; end
elseif s=='3'
if r<=p32, S(k+1)='2'; elseif r>p34, S(k+1)='4'; end
elseif s=='4'
if r<=p44, S(k+1)='4';end
end
end
X(:,j)=S;
end
% code
%end
for j=1:n
x=X(j,:);
ma=length(find(x==1));
mb=length(find(x==2));
mc=length(find(x==3));
md=length(find(x==4));
M(j,:)=[ ma mb mc md ];
end
%
nv=0:n-1;
figure(1);
plot(nv, M,'*:');
title('Simulation results -- States A & B');
xlabel('Step number');
ylabel('State');
%
Accepted Answer
More Answers (0)
Categories
Find more on Markov Chain Models in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
