2D Ising Model with Lattice structure

I'm trying to model the Ising model and plot magnetization and energy vs temperature, but it's not quite right. I'm sure it's something tiny like a misplaced comma or something in the wrong for loop, I just need a fresh set of eyes to see it.
clc
clear all
close all
size = 10;
%defining size for the matrix
Tnum = 5;
%temperatures to test
itnum = 10;
%time steps
for t = 1:Tnum
%temperature steps
A = ones(size);
%inital matrix
TT(t) = (t/Tnum)*5;
%temperature steps over temp
%TTT(t) = TT;
Mag(1, t) = sum(sum(A));
%magnetization
E(1,t)=-200;
%energy
for i = itnum %time steps
for j = 1:size %part of matrix
for k = 1:size %part of matrix
CS = circshift(A, [0 1]) + circshift(A, [0 -1]) + circshift(A, [1 0]) + circshift(A, [-1 0]);
%circle shifting the matrix
delE = 2*A(j,k)*CS(j,k);
%change in total energy
if delE<=0
A(j,k) = -A(j,k);
%flipped!
continue
end
jjj = -delE/TT(t);
chk = exp(jjj);
nn = rand;
if nn<chk
A(j,k) = -1*A(j,k);
%no flip, keep
end
end
end
A
Mag(i+1,t) = sum(sum(A));
%pause(0.1)
%mag at the next step
CS = circshift(A, [0 1]) + circshift(A, [0 -1]) + circshift(A, [1 0]) + circshift(A, [-1 0]);
E(i+1,t)=-0.5*sum(sum(A.*CS));
%updating energy total
end
MM(t) = sum(Mag(:,t))/(itnum+1);
%sum of magnetizations over time steps
EE(t) = sum(E(:,t))/(itnum+1);
%sum of energy over time
end
figure(1)
plot(TT, MM)
%plotting temp vs sums of magnetism
hold on
figure(2)
plot(TT, EE)
%plotting temp vs energy

Answers (0)

Categories

Find more on Genomics and Next Generation Sequencing in Help Center and File Exchange

Asked:

on 17 Apr 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!