필터 지우기
필터 지우기

Test based on inequality of two vectors does not succeed.

조회 수: 1 (최근 30일)
Oddur Bjarnason
Oddur Bjarnason 2016년 10월 30일
댓글: Oddur Bjarnason 2016년 10월 31일
([1,0],[0.8,0.2;0.6,0.4])
function stationarystates( S0,T )
%This function is a simple model of a Markov chain
% S0 is the initial state
% T is the transition matrix
% I want the cumulation of states to stop after state i if state i =
% state i+1. This does not happen with this code
M=S0
for i=1:1:10
if S0*T^i~=S0*T^(i-1) %Test for inequality of successive states
M((i+1),:)=S0*T^i; %M cumulates the states
else break
end
end
disp(M)
plot(M)
end

채택된 답변

Image Analyst
Image Analyst 2016년 10월 31일
편집: Image Analyst 2016년 10월 31일
And of course S0*T^i will never equal S0*T^(i-1) - the exponent is different! What you need to do is use i and (i-1) as indexes into the S0 array. It looks like S0 better be an array or you won't get it to work.
  댓글 수: 5
Steven Lord
Steven Lord 2016년 10월 31일
>> m1 = [1,0]*[0.8,0.2;0.6,0.4]^6
m1 =
0.7500 0.2500
>> m2 = [1,0]*[0.8,0.2;0.6,0.4]^7
m2 =
0.7500 0.2500
>> m1-m2
ans =
1.0e-04 *
0.1280 -0.1280
Just because m1 and m2 look the same using the default display format doesn't mean they contain the same values. You can see this more clearly using a different display format.
>> format longg
>> [m1; m2; m1-m2]
ans =
0.750016 0.249984
0.7500032 0.2499968
1.2800000000035e-05 -1.27999999999795e-05
Oddur Bjarnason
Oddur Bjarnason 2016년 10월 31일
Thank you Steven, I have come to realize this. See my answer below which follows Image Analyst's answer and comments.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Oddur Bjarnason
Oddur Bjarnason 2016년 10월 31일
function stationarystates(S0,T) %This function is a simple model of a Markov chain % S0 is the initial state % T is the transition matrix % I want the cumulation of states to stop after state i if the difference % between states is small enough.
M=S0
for i=1:1:10
m1 = S0*T^i;
m2 = S0*T^(i-1);
mDiff = abs(m2-m1);
if max(mDiff(:)) > 0.00001; % The states are essentially equal.
M((i+1),:)=S0*T^i; % M cumulates the states
else break
end
end
disp(M)
plot(M)
end
  댓글 수: 1
Oddur Bjarnason
Oddur Bjarnason 2016년 10월 31일
Thank you Image Analyst. This code is accordance with your answer and comments and yields the results I needed. I realize that the rows of the matrix approach the stationary matrix asymptotically but never becomes equal to the stationary state. So I have to be satisfied with a difference between states that is small enough. Thank you again. Oddur.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Markov Chain Models에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by