clear all
clc

n=12;
x=rand(n,n,1);

for j=1:n;
    for k=1:n
        if x(j,k,1)<=0.5
            x(j,k,1) = -1;
        else
            x(j,k,1) = 1;
        end;
    end;
end;

figure(1)
imagesc(x(2:n-1,2:n-1,1));
colormap('gray');
title('initial image');
saveas(figure(1),['Initial Image prob3.png']);

n1=200;
for i=2:n1;
    x(:,:,i)= x(:,:,i-1);
    for j=2:n-1;
        for k=2:n-1
            alpha1=x(j+1,k,i-1) + x(j-1,k,i-1)+x(j,k+1,i-1)+x(j,k-1,i-1);
            p=exp(-x(j,k,i-1)*alpha1)/(exp(alpha1)+exp(-alpha1));
            if p<=rand;
                x(j,k,i)=x(j,k,i-1);
            else
                x(j,k,i)=-x(j,k,i-1);
            end
        end;
    end
end;


figure(2)
imagesc(x(2:n-1,2:n-1,(floor(n1/5))*2));
colormap('gray');
title(['iteration ' int2str(floor(n1/5)*2) ' of the image']);
saveas(figure(2),['iteration ' int2str(floor(n1/5)*2) ' prob3.png']);

figure(3)
imagesc(x(2:n-1,2:n-1,floor(n1/5)*3));
colormap('gray');
title(['iteration ' int2str(floor(n1/5)*3) ' of the image']);
saveas(figure(3),['iteration ' int2str(floor(n1/5)*3) ' prob3.png']);

figure(4)
imagesc(x(2:n-1,2:n-1,floor(n1/5)*4));
colormap('gray');
title(['iteration ' int2str(floor(n1/5)*4) ' of the image']);
saveas(figure(4),['iteration ' int2str(floor(n1/5)*4) ' prob3.png']);

figure(5)
imagesc(x(2:n-1,2:n-1,floor(n1/5)*5));
colormap('gray');
title(['iteration ' int2str(floor(n1/5)*5) ' of the image']);
saveas(figure(5),['iteration ' int2str(floor(n1/5)*5) ' prob3.png']);
