function ehr(d,x,n) P=diag([1/d:1/d:1],-1)+diag([1:-1/d:1/d],1) N=zeros(1,d+1) X=grand(n,"markov",P,x+1) for i=1:d+1 N(i)=mean(1*(X==i)) end xbasc() plot2d([0:d],N,-2) plot2d([0:d],binomial(.5,d),-3) endfunction function ehra(d,x,n,e) P=(1-e)*(diag([1/d:1/d:1],-1)+diag([1:-1/d:1/d],1))+diag(e*ones(1,d+1)) X=grand(n,"markov",P,x+1) xbasc(); plot2d2([1:n],X,5) endfunction function trajehr(x,d,n) X=x*ones(1,n+1) for i=1:n X(i+1)=X(i)+2*(rand(1,1)>(X(i)/d))-1 end xbasc(); xset("font size",4) plot2d2([0:n],X,5) plot2d([0,n],[d,d]/2,2) legends(['Trajectoire','Valeur moyenne'],[5,2],4) xtitle('Trajectoire de la chaîne d''Ehrenfest avec d='+string(d)) endfunction function trajehrc(x,d,n) X=x*ones(1,n+1) for i=1:n X(i+1)=X(i)+2*(rand(1,1)>(X(i)/d))-1 end xbasc(); xset("font size",4) plot2d([0:n],X,5) plot2d([0,n],[d,d]/2,2) plot2d([0,n],[d-5*sqrt(d),d-5*sqrt(d)]/2,3) plot2d([0,n],[d+5*sqrt(d),d+5*sqrt(d)]/2,3) legends(['Trajectoire','Valeur moyenne','Int de conf'],[5,2,3],4) xtitle('Trajectoire de la chaîne d''Ehrenfest avec d='+string(d)) endfunction function [P,Q]=vrac P=[0 1 0; 0 1/2 1/2; 1/2 0 1/2] Q=[1/2 1/2 0 0 0 0; 0 0 1 0 0 0; 1/3 0 0 1/3 1/3 0; 0 0 0 1/2 1/2 0; 0 0 0 0 1/2 1/2; 0 0 0 0 1/2 1/2] endfunction function couplage1 P=[0 1 0; 0 1/2 1/2; 1/2 0 1/2] pi=[.2 .4 .4] u=rand(1,1) x=1;y=1+1*(u>.2)+1*(u>.6); while x(\$)<>y(\$) x=[x grand(1,"markov",P,x(\$))] y=[y grand(1,"markov",P,y(\$))] end xbasc(); l=length(x) plot2d2([0:l-1],x,2) plot2d2([0:l-1],y,5) endfunction function couplage2 P=[0 1 0 0 0; 0 1/3 1/3 0 1/3; 1/4 0 1/4 1/4 1/4; 1/2 0 1/2 0 0; 1/3 0 1/3 0 1/3] x=1;y=5; while x(\$)<>y(\$) x=[x grand(1,"markov",P,x(\$))] y=[y grand(1,"markov",P,y(\$))] end xbasc(); l=length(x) plot2d([0:l-1],x,2) plot2d([0:l-1],y,5) endfunction