

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A matlab code that simulates the ising model using the monte carlo method and the metropolis algorithm. The ising model is a mathematical model of ferromagnetism in statistical mechanics, which is used to study phase transitions and critical phenomena. The code generates random spins, calculates the energy and magnetization, and applies the metropolis algorithm to update the spins based on the metropolis criterion. The code also calculates the specific heat and the magnetic susceptibility as a function of temperature.
Typology: Exercises
1 / 3
This page cannot be seen from the preview
Don't miss anything!


clear all; n =10; % number of atoms in a row n_temp = 20; % number of steps for temperature loop nbin = 5; % number of bins to average energyies etc nmc = 500; % number of monte carlo steps for one bin T = 0.9; % initial temperature JJ = -1.0; %%%%%% generate random spins for the first time for ip= 2: n+ for jp= 2: n+ rr = 2.rand - 1.0 ; s(ip,jp) = sign(rr); s(ip,jp) = 1; end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% rand('state', 1) for mm=1: n_temp % loop over temperature T = T + 0.1; T beta = 1/T; temper(mm)= T; for kub=1:nbin % nbin loop to reduce variance % kub % fix the boundary conditions as periodic for ja=2: n+ s(1,ja) = s(n+1,ja); s(n+2,ja) = s(2, ja); end for ia=2: n+ s(ia,1)= s(ia,n+1); s(ia,n+2) = s(ia,2); end for kkk=1: nmc % loop over changes in spins sum_en = 0.0; sum_mg = 0.0; for j = 2: n+1 % loop over all mesh points for i = 2: n+1 % loop over all mesh points % Start: Metropolis Algorithm for spin flip ----- en_initial = JJs(i,j)(s(i+1,j)+s(i-1,j)+s(i,j+1)+s(i,j-1)); s_old = s(i,j); % save this for possiblereverting if(s(i,j)== +1) snew = -1; end if(s(i,j)== -1) snew = +1; end s(i,j) = snew; %(spin flip has been done) % refresh the boundary values for the flip for jj=2: n+ s(1,jj) = s(n+1,jj); s(n+2,jj) = s(2, jj); end for ii=2: n+ s(ii,1) = s(ii,n+1); s(ii,n+2) = s(ii,2); end en_final = JJs(i,j)*(s(i+1,j)+s(i-1,j)+s(i,j+1)+s(i,j-1)); delta_e = en_final - en_initial; if(delta_e<=0) s(i,j) = snew; % keep the flip %'spin is flipped; as delta_e<=0 '
end if(delta_e>0) rr = rand; if(rr<= exp(-betadelta_e)) s(i,j)= snew; % again keep the flip %'spin is flipped as delta_e>0' elseif(rr>exp(-betadelta_e)) s(i,j) = s_old; % remove the flip %'spin is not flipped'; end end % refresh the boundary values for the flip for jk=2: n+ s(1,jk) = s(n+1,jk); s(n+2,jk) = s(2, jk); end for ik=2: n+ s(ik,1) = s(ik,n+1); s(ik,n+2) = s(ik,2); end % end of the Matropolis algo .................... end % end of i-loop end % end of j-loop % calculate energy for each mesh point (or atom) for jy=2:n+ for ix=2:n+ e(ix,jy) = 0.5JJs(ix,jy)(s(ix+1,jy)+s(ix-1,jy)... +s(ix,jy+1)+s(ix,jy-1)); % Calculate the total energy and total magnetization sum_en = sum_en + e(ix,jy); sum_mg = sum_mg + s(ix,jy); end end mc_step(kkk) = kkk; energy(kkk) = sum_en/(nn); % energy per atom magn(kkk) = (sum_mg)/(nn); % magnitization per atom %kkk % axis([0 1000 -2 2]) % plot(kkk, sum_mg/9,'-.r') % hold on % F(kkk) = getframe; end % end of kkk loop sum00 = 0.0; sum01 = 0.0; for k1=1:nmc sum00 = sum00 + energy(k1); sum01 = sum01 + magn(k1); end mean_en(kub) = sum00/nmc; mean_mg(kub) = abs(sum01)/nmc; end sumff = 0.0; sumgg = 0.0; sumff2 = 0.0; sumgg2 = 0.0; for kd=2:nbin