You are on page 1of 5

%% Homework #10 - Author: Noah Moreno

classdef Moreno_HW10_IsingClass < handle


%TwoDimIsing - This class runs a two dimensional ising model with
%computations for energy and variable update numbers. The gui allows
%the user to change values for interaction strength, the number of
%spins, and B field.

properties
beta % Constant value.
j % Constant value for interaction strength.
B % Magnetic field value.
N % Number of spins.
FigGUI % Property for the figure.
spins % Property for the spins array.
RunningCondition % Condition for while loop.
UpdateNumber % Number of updates.
end

methods
function obj = TwoDimIsing(obj)
obj.spins = double(randn(obj.N,obj.N)>0); %
Initializing array for spins.
obj.spins(obj.spins==0)=-1; %
Initializing first element of the array.
imshow(obj.spins,[],'InitialMagnification',400) % Plots
the spins using the imshow function.
s=sprintf('Beta=%g, J=%g, B=%g',obj.beta,obj.j,obj.B); % Title
for the figure.
title(s)

obj.RunningCondition=1; % Condition for the while


loop.
NChain=1000000; % Start of Markov Chain Monte
Carlo method.
nn=0;
while obj.RunningCondition && obj.UpdateNumber > 0 % Conditions
for loop.
nn=nn+1; % Counter.
obj.UpdateNumber = obj.UpdateNumber -1; % Decreasing
value for the number of updates.
IDX=ceil(obj.N*rand); % Picking a
value for the test jump. (x)
IDY=ceil(obj.N*rand); % Picking a
value for the test jump. (y)
SpinsTest=obj.spins; %
Initializing array for tests.
SpinsTest(IDX,IDY)=-obj.spins(IDX,IDY); % Inverting
spins.
[H,HTest] = obj.EnergyComputation(obj.spins,SpinsTest); %
Computation of energy value (total = Hamiltonian).
[Accept] = obj.EnergyAccept(obj.spins,SpinsTest); %
Evaluating acceptance.
if Accept == 1
obj.spins(IDX,IDY) = -obj.spins(IDX,IDY); %
Changing the sign of spin for accepted test values.
end
ShowIter=1000; % Number of
iterations.
if (nn/ShowIter==round(nn/ShowIter))
pause(.01);if ~obj.RunningCondition;break;end % Time
value for figure closure.
try
imshow(obj.spins,[],'InitialMagnification',400) %
Drawing on figure.

catch
end
pause(.01)
end
end

end
function[H,HTest] = EnergyComputation(obj,spins,SpinsTest)
spins = obj.spins; % Initializing spins variable
with object property value.
IDX=ceil(obj.N*rand); % Picking a test value. (x)
IDY=ceil(obj.N*rand); % Picking a test value. (y)
SpinsTest=obj.spins; % Initializing test array.
SpinsTest(IDX,IDY)=-obj.spins(IDX,IDY); % Inverting at test
values.

E_Spin=obj.j*(sum(sum(spins(1:end-1,:).*spins(2:end,:)))+... %
Energy value from neighbor interaction
sum(spins(1,:).*spins(end,:))+... %
Neighbor for Y
sum(sum(spins(:,1:end-1).*spins(:,2:end)))+... %
Neighbor for X
sum(spins(:,1).*spins(:,end))); % X

E_SpinTest=obj.j*(sum(sum(SpinsTest(1:end-
1,:).*SpinsTest(2:end,:)))+... % Neighbor for Y
sum(SpinsTest(1,:).*SpinsTest(end,:))+...
% Y
sum(sum(SpinsTest(:,1:end-1).*SpinsTest(:,2:end)))+...
%Neighbor for X
sum(SpinsTest(:,1).*SpinsTest(:,end)));
%X

E_Field=-obj.B*sum(spins(:)); % Energy from


magnetic field interaction.
E_FieldTest=-obj.B*sum(SpinsTest(:)); % Test values
inverted.

H=E_Spin+E_Field; % Total energy is the


Hamiltonian.
HTest=E_SpinTest+E_FieldTest; % Test value.
end
function[Accept] = EnergyAccept(obj,spins,SpinsTest) %
Employment of Metropolis-Hastings for acceptance.
spins = obj.spins; %
Initializing array for spins.
[H,HTest] = obj.EnergyComputation; % Calling
energy computation function.
Alpha=exp(-obj.beta*(HTest-H)); %
Acceptance probability formula.

Accept = 0; % Metropolis-Hastings rule.


if rand()< (Alpha) % Condition for acceptance.
Accept = 1; % Acceptance value.
end
end

function delete(obj)

delete(obj.FigGUI); % Closing figure.

end
function StopModel(obj)
obj.RunningCondition = 0; % Gui closes when this value is zero.
end

function gui(obj)
% Graphical User Interface function for the class to have
% modularity for the number of updates, the number of spins,the
% value for interaction strenth and magnetic field.
Z=findall(0,'Tag','TWODIMISINGGUI') % Checks for an empty
figure.
if ~isempty(Z);return;end

F = figure('Position',[100 100 300 400]); % Creates figure for


object.
F.Tag = 'TWODIMISINGGUI' % Tag for
figure.
FigH.CloseRequestFcn='Go=0;';
obj.FigGUI=F; % Saves figure to
object property.

% Setup UI CONTROLS----------------------------------------------
TextBoxA = uicontrol('Style','edit','String','Beta',... %
First Text Box (editable).
'Position',[100 300 50 50]);
ALabel = uicontrol('Style','text','String','Beta',... %
Label for the first text box.
'Position',[0 300 50 50]);
TextBoxB = uicontrol('Style','edit','String','Interaction
Strength',... % Second text box for jump size (editable).
'Position',[100 250 50 50]);
BLabel = uicontrol('Style','text','String','Interaction
Strength',... % Label for the second text box.
'Position',[0 250 50 50]);

TextBoxC = uicontrol('Style','edit','String','Magnetic Field',...


% Third Text Box (editable.
'Position',[100 200 50 50]);
CLabel = uicontrol('Style','text','String','Magnetic Field',...
% Label for the third text box.
'Position',[0 200 50 50]);

TextBoxD = uicontrol('Style','edit','String','Number of
Spins',... % Fourth Text Box (editable).
'Position',[100 150 50 50]);
DLabel = uicontrol('Style','text','String','Number of Spins',...
% Label for the fourth text box.
'Position',[0 150 50 50]);
TextBoxE = uicontrol('Style','edit','String','Number of
Updates',... % First Text Box (editable).
'Position',[100 350 50 50]);
ELabel = uicontrol('Style','text','String','Number of
Updates',... % Label for the first text box.
'Position',[0 350 50 50]);

Button = uicontrol('Style', 'pushbutton', 'String', 'Run',...


% Button to run IsingModel function.
'Position', [0 50 50 50],...
'Callback', @runTwoDimIsing);

ButtonTwo = uicontrol('Style', 'pushbutton', 'String',


'Stop',... % Button to stop IsingModel function.
'Position', [100 50 50 50],...
'Callback', @runStopModel);

property2gui() % Initialize UI controls.

function runTwoDimIsing(~,~)
gui2property() % Callback for button.
obj.TwoDimIsing() % Runs Ising function.
property2gui()
end
function runStopModel(~,~)
obj.StopModel
end

function property2gui()
TextBoxA.String=num2str(obj.beta); % Writes object
property for the constant value beta to the first uicontrol.
TextBoxB.String=num2str(obj.j); % Writes object
property for the interaction strength to the second uicontrol.
TextBoxC.String=num2str(obj.B); % Writes object property
for the magnetic field magnitude to the third uicontrol.
TextBoxD.String=num2str(obj.N); % Writes object
property for the number of spins of the data to the fourth uicontrol.
TextBoxE.String=num2str(obj.UpdateNumber); % Writes object
property for the number of updates to the fifth uicontrol.
end
function gui2property()
obj.beta=str2double(TextBoxA.String); % Value for first
object property.
obj.j=str2double(TextBoxB.String); % Value for second
object property.
obj.B=str2double(TextBoxC.String); % Value for third object
property.
obj.N=str2double(TextBoxD.String); % Value for
fourth object property.
obj.UpdateNumber=str2double(TextBoxE.String); % Value for
fifth object property.
end

end
end
end

You might also like