function fig6
clf;

% These are graphic display commands
% The computation engine is in the functions onesidedMarkovHK and markovHK

k=10;
t=0;
p=[1 1];
n=41;

axisY=15;
textX=1;

plotting=[0 1 2 3 4 5 6 7];

subplot(length(plotting),2,1);
colormap([0 0 0])
bar(0:n,ones(size(0:n)),1);
axis([-1 n-1 0 axisY]);
text(textX,axisY*0.7,['t = 0']);
a=get(gca,'Position');
set(gca,'XTick',[1 10 20 30 40],'XTickLabel',[],'YTick',[0 10],'Layer','top')
title(['k = ' num2str(k)])

for i=2:length(plotting)
    for j=t+1:plotting(i)
        p=onesidedMarkovHK(p,k);
    end
    t=plotting(i);
    subplot(length(plotting),2,2*i-1);
    colormap([0 0 0])
    P=p;P(length(p)+1:n)=1;
    bar(0:length(P)-1,P,1);
    axis([-1 n-1 0 axisY]);
    a=get(gca,'Position');
    set(gca,'XTick',[0 10 20 30 40],'XTickLabel',[],'YTick',[0 10 ],'Layer','top')   
    text(textX,axisY*0.7,['t = ' num2str(t)])
end
set(gca,'XTickLabel',[0 10 20 30 40]);
xlabel('opinion classes')

% right hand side

k=500;
t=0;
p=[1 1];
n=2001;

axisY=15;
textX=99;

plotting=[0 1 2 3 4 5 6 7];

subplot(length(plotting),2,2);
colormap([0 0 0])
pPlot=[p ones(1,n-(length(p)))];
bar(0:length(pPlot)-1,pPlot,1);
axis([-1 n-1 0 axisY]);
text(textX,axisY*0.7,['t = 0']);
a=get(gca,'Position');
set(gca,'XTick',[0 500 1000 1500 2000],'XTickLabel',[],'YTick',[0 10],'Layer','top')
title(['k = ' num2str(k)])

for i=2:length(plotting)
    for j=t+1:plotting(i)
        p=onesidedMarkovHK(p,k);
    end
    t=plotting(i);
    subplot(length(plotting),2,2*i);
    colormap([0 0 0])
    P=p;P(length(p)+1:n)=1;
    bar(0:length(P)-1,P,1);
    axis([-1 n-1 0 axisY]);
    a=get(gca,'Position');
    set(gca,'XTick',[0 500 1000 1500 2000],'XTickLabel',[],'YTick',[0 10],'Layer','top')
    text(textX,axisY*0.7,['t = ' num2str(t)])
end
set(gca,'XTick',[0 500 1000 1500 2000],'XTickLabel',[0 500 1000 1500 2000],'YTick',[0 10],'Layer','top')
xlabel('opinion classes')

set(gcf,'Position',[232    49   600   500],'PaperPositionMode','auto')
print -dtiff -r96 fig5






function P=onesidedMarkovHK(P,k)

% P = onesidedMarkovHK(P,k)
% P : row vector of the opinion distribution
% k : discrete opinion dynamics
%
% computes one step in an HK interactive Markov chain and extracts P with
% uniform opinion classes

% calculate point of uniformity
pou = max(find(P~=1));
if isempty(pou)
    pou=0;
end

% add enough length
P(pou+1:pou+2+2*k)=1;

P=markovHK(P,k);

% flat tail to uniformity
P(pou+1+k:pou+2+2*k)=1;





function P = markovHK(P,k)

% P = markovHKsymodd(P,k)
% P : 1 x n vector of the opinion distribution
% k : discrete opinion dynamics
%
% computes one step in an HK interactive Markov chain for symmetric P

k = floor(k);
n = length(P);
%A = zeros(n);
I = [];
J = [];
S = []; % I, J, and S are to construct the transition matrix in sparse form
for i = 1:n
    minindP = max([1 i-k]);
    maxindP = min([n i+k]);
    inds = minindP:maxindP;
    mass = sum(P(inds));
    if mass > 10*eps
        mean = sum(P(inds).*inds)/mass;
    else
        mean = i;
    end
    j1 = floor(mean+10*eps);
    j2 = ceil(mean-10*eps);
    if j1 ~= j2
        I=[I;i;i];
        J=[J;j1;j2];
        S=[S;j2-mean;mean-j1];
    else
        I=[I;i];
        J=[J;j1];
        S=[S;1];
    end
end
A = sparse(I,J,S,n,n);
P = P*A;