Test based on inequality of two vectors does not succeed.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Oddur Bjarnason
am 30 Okt. 2016
Kommentiert: Oddur Bjarnason
am 31 Okt. 2016
([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
1 Kommentar
Akzeptierte Antwort
Image Analyst
am 31 Okt. 2016
Bearbeitet: Image Analyst
am 31 Okt. 2016
Are S0 and T integers? Otherwise see the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
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 Kommentare
Steven Lord
am 31 Okt. 2016
>> 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
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Markov Chain Models finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!