Andof 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) asindexes into the S0 array. It looks like S0 better be an array or you won't get it to work.
S0 is an initial state row vector of a Markov chain. In the example it is [1,0]. T is a corresponding transitional matrix. In the example it is [0.8,0.2;0.6,0.4]. When S0 is multiplied by increasing powers of T the states eventually become equal. I accumulate the states into the matrix M. My problem is that M does not stop accumulating the states when they become equal.
Add these lines before the if and see what gets reported to the command line:
S0*T^i
S0*T^(i-1)
Don't use semicolons so you can see what they give. If you think they're equal, then you really need to read the FAQ like I already gave you the link for. You'd need to do something like
Just because m1 and m2look the same using the default display format doesn't mean theycontain the same values. You can see this more clearly using a different display format.
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.
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.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
1 Comment
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/309846-test-based-on-inequality-of-two-vectors-does-not-succeed#comment_402803
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/309846-test-based-on-inequality-of-two-vectors-does-not-succeed#comment_402803
Sign in to comment.