%
%  math495/stat490 -- 10 nov 03 -- djm
%
%  w11markov.m:  

clear

%  system size
Nsize = 8;  Nst = 2^Nsize;

%  initialize transition matrix
P = zeros(Nst,Nst);

%  loop over all possible states
for jj = 0:Nst-1
    bin_st = dec2bin(jj,Nsize);

    %  loop over all possible transitions
    for kk = 0:Nsize-1
        bin_temp       = bin_st;
        bin_temp(kk+1) = bin_temp(mod(kk+1,Nsize)+1); 
        new            = bin2dec(bin_temp);
        
        %  increment transition probability
        P(jj+1,new+1) = P(jj+1,new+1) + (1/8);
    end
end

figure(1);  clf;
image(0:Nst-1,0:Nst-1,P,'CDataMapping','scaled');
caxis([0 1]);  colorbar
axis equal;  axis image
title('\bf P matrix -- color image')

disp('pausing')
pause

[evec,eval] = eig(transpose(P));

figure(2)
plot(1:Nst,abs(diag(eval)),'x')
title(['\bf absolute value of all ' num2str(Nst) ' eigenvalues'])
ylabel('\bf |\lambda_n|')
xlabel('\bf n-axis')

figure(3)
ev = 3;  eval(ev,ev)
plot(0:Nst-1,evec(:,ev),'x')
title(['\bf elements of eigenvector #' num2str(ev)])
ylabel('\bf k = state #')
xlabel('\bf v_k')

state = [];  prob = [];
for j=1:Nst
    if (abs(evec(j,ev))>0.01)
        state = [state ; dec2bin(j-1,8)];
        prob  = [prob  ; evec(j,ev)  ];
    end
end

%  sorted info
[prob1 index] = sort(-abs(prob));  data = [];

for j=1:size(prob1,1)
    data = [data ; state(index(j),:) '  ' num2str(abs(prob1(j)),'%10.5f')];
end

data