function [randomstate, freqstates] = SimulationComplete(P,qzero,nsteps) % Simulation of nsteps steps of a Markov Chain, % with transition probability matrix P, and with % initial probability vector qzero. % Returns % - the final state (in randomstate) % - the relative frequence of each of the three states, as they were generated along the path (in freqstates) q = qzero; freqstates = zeros(size(qzero)); for i = 1 : nsteps randomstate = Generate(q); freqstates(randomstate) = freqstates(randomstate) + 1; q = P(:,randomstate); end freqstates = freqstates/nsteps; end