%HW9
%% Problem #1
%creating an object for the class HW9_2DIsing, and usig the GUI

HW9_G = HW9_2DIsing;
HW9_G.HW9_gui; %calling the GUI method

%% Problem #1
% How to use class without GUI

HW9 = HW9_2DIsing; % creating an object
%assigning values to the class properties
HW9.B = 0.01;
HW9.J = 0.1;
HW9.Beta = 100;
% calling the spins-function
HW9.HW9_spins;

%% Problem #2

Data = load('HW9_NoisyData.mat'); %loading data

figure
plot(Data.X,Data.YNoise,'.') %plotting the data
xlabel('X');ylabel('Signal')

lsqlinefit = @(X0,YNoise,X) sum( (YNoise-(X0(2)+X0(1)*X)).^2 ); %the linear model for the data
X0=[1 1]; %the initial values for unknowns

OPTIONS = optimset();
ABfound = fminsearch(lsqlinefit,X0,OPTIONS,Data.YNoise,Data.X); %implementing 'fminsearch' function 

hold on %plotting the found model for the data
FoundModel=ABfound(2)+ABfound(1)*Data.X;
plot(Data.X,FoundModel,'g','linewidth',2)
legend('Noisy Data','Found Model','Location','NorthWest')
s=sprintf('A: %g  B: %g',ABfound(1),ABfound(2));
title(s)
