%% Homework #13

%% Problem 1
% Take our 2D Ising model simulator and make a class.   The class should
% have propoerties of 'B','J','Beta','UpdateInterval' and 'NumJumps'. The class should have
% methods to run the Markov Chain and to show the 2D image. Demonstrate
% these capabilities.

% See HW13_Solution_2DIsing


%% Problem 2
%
% Add to the class a method that finds the correlation length.  This can be
% done by radially averaging the 2D auto-correlation image and fitting the result to
% an exponential model.  Least-squares fitting is sufficient for this purpose.

% See HW13_Solution_2DIsing.findCorLength()


%% Problem 4
%
% Run the simulation until it approaches equilibrium for values of 'J'
% between -2 and 0.  Keep 'Beta' at one and 'B' at zero.  Plot the
% correlation length as a function of 1/(Beta*J). Indicate with a dashed
% vertical line the location of the theoretically predicted phase
% transition.  Use the 'publish' capability to make a PDF showing the use
% of your class and the figure produced.
clear all
clear classes

I2D=HW13_Solution_2DIsing()
I2D.NumJumps=1e6;
I2D.NSize=100;

Jvec=linspace(-1,0,10);
L=zeros(size(Jvec));

for jj=1:length(Jvec)
    I2D.J=Jvec(jj);
    I2D.runChain();
    L(jj)=I2D.findCorLength();
end

h=figure
plot(Jvec,L);

%Transition point
Jtans=-log(1+sqrt(2))/2;
hold on
plot([Jtans, Jtans],[0 max(L)],'k--')
xlabel('J')
ylabel('Correlation Length')
saveas(h,'2DIsingPhaseTransition','png')




%% Extra Credit
%
% Make a GUI for the simulator.

I2D=HW13_Solution_2DIsing()
I2D.gui



















