假设有9个状态,其状态转移图如下所示:
问题一:从状态1在1000次内转移到状态9的概率为多少?
clear all
clc
%一步转移概率矩阵
P=[0.7 0.3 0 0 0 0 0 0 0
0.7 0 0.3 0 0 0 0 0 0
0 0.7 0 0.3 0 0 0 0 0
0 0 0.7 0 0.3 0 0 0 0
0 0 0 0.7 0 0.3 0 0 0
0 0 0 0 0.7 0 0.3 0 0
0 0 0 0 0 0.7 0 0.3 0
0 0 0 0 0 0 0.7 0 0.3
0 0 0 0 0 0 0 0 1];
p=P^1000;
p(1,9)%理论值
num=0;%记录到达状态9的次数
for i=1:100000%实验次数
state=1;%初始状态为1
for j=1:1000%1000步
if state==1
if rand()<0.3
state=2;
end
elseif state==9
state=9;
else
if rand()<0.3
state=state+1;
else
state=state-1;
end
end
if state==9
num=num+1;
break;
end
end
end
num/100000 %仿真值clear all
clc
P=[0.7 0.3 0 0 0 0 0 0 0
0.7 0 0.3 0 0 0 0 0 0
0 0.7 0 0.3 0 0 0 0 0
0 0 0.7 0 0.3 0 0 0 0
0 0 0 0.7 0 0.3 0 0 0
0 0 0 0 0.7 0 0.3 0 0
0 0 0 0 0 0.7 0 0.3 0
0 0 0 0 0 0 0.7 0 0.3
0 0 0 0 0 0 0 0 1];
sum=0;
p=P^1000;
x=1;
y=9;
P_size=9;%状态个数
Ix=zeros(1,P_size);%行向量
Ix(x)=1;
Iy=zeros(P_size,1);%列向量
Iy(y)=1;
I=eye(P_size);
Ey=I;
Ey(y,y)=0;
average=Ix*(I-P*Ey)^-2*P*Iy%理论值
state=1;%初始状态
num=0;%达到次数
result=0;%需要经过的步数
for k=1:100000%仿真10000次
state=1;
for i=1:100000000%设定最大步数(越大越好)
if state==1
if rand()<0.3
state=2;
end
elseif state==9
state=9;
else
if rand()<0.3
state=state+1;
else
state=state-1;
end
end
if state==9
num=num+1;
result=result+i;%记录总的步数
break;
end
end
end
result/num%计算平均步数原文:http://blog.csdn.net/tengweitw/article/details/43160553
作者:nineheadedbird
原文地址:http://blog.csdn.net/tengweitw/article/details/43160553