# A simulation algorithm for the Ising model # A Gibbs sampler first--I've fudged a little since I use random # selection of pixels rather than moving through them systematically ising.gs=function(nx = 50, ny=20, beta = .2, istop = 100) { x <- matrix(0,nrow=nx+2,ncol=ny+2) x[2:(nx+1),2:(ny+1)]=matrix(sample(c(-1,1),nx*ny,replace=T),nrow=nx,ncol=ny) x.init=x[2:(nx+1),2:(ny+1)] iestop=istop*nx*ny for(i1 in 1:iestop) { i=sample(1:nx,1)+1 j=sample(1:ny,1)+1 r=exp(2*beta*(x[i,j-1]+x[i,j+1]+x[i-1,j]+x[i+1,j])) r=r/(1+r) if(runif(1)