Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Problem-3

We need to Compute and plot the energy, specific heat, magnetization and susceptibility as a function of temperature
for 2-D ising model.

Solution
tic
clc
clear all
k=0;
n=32;
n_run=10000;
eqpt=5000;
n_en=10;
H1=zeros(n_en, n_run);
mags=zeros(n_en,n_run);
E_savg=[];
E_svar=[];
M_savg=[];
M_svar=[];
T=[];
for t=1.5:0.05:3 %tempertaute loop
beta=1 / t;
T=[T,t];
for k=1:n_en %ensemble average
x=2*randi([0,1],n)-1;

for j=1:n_run
y=randi([1,n],1,n^2);
z=randi([1,n],1,n^2);
for i=1:n^2
r=y(i);
c=z(i);
s=x(r,c);
if r==n
e1=s*x(1,c);
else
e1=s*x(r+1,c);

end

if r==1
e2=s*x(n,c);
else
e2=s*x(r-1,c);
end

if c==n
e3=s*x(r,1);
else
e3=s*x(r,c+1);

1
Problem-3

end

if c==1
e4=s*x(r,n);
else
e4=s*x(r,c-1);
end
e=e1+e2+e3+e4;
if e<0
x(r,c)=-x(r,c);
else
p=exp(-2*beta*e);
if p>rand()
x(r,c)=-x(r,c);
end
end

end

E=Hamiltonian(x);
H1(k, j) = E;
mags(k,j)=sum(sum(x))/numel(x);

end

end
H2 = mean(H1);
H2=H2(eqpt:end);
H3=mean(H2);
E_savg=[E_savg,H3]; %equil
H1_vas=var(H2);
E_svar=[E_svar,H1_vas];
M2=mean(mags);
M2=M2(eqpt:end);
M3=mean(M2);
M_savg=[M_savg,M3];
M1_vas=var(M2);
M_svar=[M_svar,M1_vas];

end
specefic_heat=E_svar.*T.^2;
suscepti = M_svar.*T.^2;
% subplot(1,2,1)
% plot(T,E_savg)
% xline(2.4,'-',{'Transition Temperature'});
% xlabel('Temperature')
% ylabel('Average energy')
% title('Average energy function of temperature')
% subplot(1,2,2)
% plot(T,M_savg, 'bx')
% hold on

2
Problem-3

% plot(2.4,0,'r*') %red dot will be transition temperature


% xlabel('Temperature')
% ylabel('Magnetization')
% title('Magnetization function of temperature')
subplot(1,2,1)
plot(T,suscepti)
xline(2.35,'-',{'Transition Temperature'});
xlabel('Tempertautre')
ylabel('susceptibility')
title('susceptibility as a function of temperature')
subplot(1,2,2)
plot(T,specefic_heat)
xline(2.35,'-',{'Transition Temperature'});
xlabel('Temperature')
ylabel('Specefic heat')
title('Specefic heat a function of time')
toc

%function file for hamiltonian per spin


function E = Hamiltonian(grid)
E=0;
E=E-sum(sum(grid.*circshift(grid,[0,-1])));
E=E-sum(sum(grid.*circshift(grid,[1,0])));
E=E/numel(grid);
end

Elapsed time is 306.621386 seconds.

3
Problem-3

Published with MATLAB® R2022a

You might also like