function new=geman_update(old,n,L,T);
%new=geman_update(old,n,L,T)
%
%Metropolis type of update of a pixel value of a site
%
%'old' is the present pixel value of the site
%'n' is the neighbours to the site
%'L' is the number of possible states in the Potts model
%'T' is the temperature
%
%Mats K 010206
%cand_vec consists of the possible states except for
%the present on:
cand_vec=[1:old-1,old+1:L];
%Draw a random candidates from these L-1 states:
new=cand_vec(ceil((L-1)*rand));
%Count the number of neighbours with the state as 'new' and 'old':
same_new=length(find(n==new));
same_old=length(find(n==old));
%Calculate the difference in 'potential energy':
Udelta=-2/3*(same_new-same_old);
%and the corresponding 'probability' of not changing state to 'new':
q=exp(-Udelta/T);
%(this 'probability' can be >1, so it's not a probability in the
%strict sense)
if q<1
if rand>q
new=old;
end
end