Monte Carlo simulation of the 2-d Ising model..

I just finished my code for the 2d Ising model using Monte Carlo simulation. If you are a graduate student in the Chemical Engineering Department at Stanford University then:

  1. Your T.A. for the Statistical Thermodynamics course will suck. And confuse the hell out of you.
  2. This will be just a week before your final exam. And on the day of your Fall Quarter Poster presentation.
  3. You will be given a week to work on this. Thanksgiving break week. Yes, the only time you thought you would chill-out.

Here’s the code: ———————Ising2d.m————————

clear all;
close all;
clc;
S=-1+2*round(rand(31,31));
pcolor(S);
[t1,E,M]=energy(S);
[S,E,M]=mcarlo(S,1e5,E); % Initialization
mstep=1000;
mrun=5000;
for i=1:mstep
[S,E,M]=mcarlo(S,mrun,E);
Eavg(i)=E;
Mavg(i)=M;
M2_avg(i)=M^2;
end
E_final=mean(Eavg)
M_final=mean(Mavg)
M2_final=mean(M2_avg)
figure:pcolor(S);
———————energy.m—————————-
function [t1,E,M]=energy(S)
S(:,end)=S(:,1); %  S(:,end)-S(:,1) \\Check!
S(1,:)=S(end,:); % S(1,:)-S(end,:)
x=30;y=30;t1=0;
for i=1:x
for j=1:y
t1=t1+ (S(i,j)*S(i+1,j) + S(i,j)*S(i,j+1));
end
end
% J=0.9;
T_Tc = 1 + 0.1;
J=0.4407/T_Tc;
E= -J*t1;
smatrix=S(1:30,1:30);
M=mean(mean(smatrix)); %Magnetization
end
——————mcarlo.m———————————
function [S,Enew,M]=mcarlo(S,runs,E)
for count=1:runs
X=ceil(30*rand);
Y=ceil(30*rand);
S(X,Y)=-S(X,Y);
[t1,Enew,M]=energy(S);
if(Enewrand)
E=Enew;
else
S(X,Y)=-S(X,Y);
end
end
—————————THE END————————–

And some interesting results: monte_carlo_ising _2d.jpg

I know. You’re welcome 🙂

Comments are closed.