% Simulation of Hopfield model (no noise)
clear all
close all
clc

N=[ 1000 2000 3000];  % number of neurons
alpha=[0.12 0.14 0.16]; % load factor

prob=0.5; 

 % k between 1 and ng, (N)
 % j between 1 and ng, (P)
ng=3;

for k=1:1:ng
    for j=1:1:ng

P=alpha*N(k); % number of patterns
patterns = 2*binornd(1,prob,N(k),P(j))-1; % generation of patterns

J=patterns*patterns';
D=diag(diag(J));
W=(J-D); % matrix of weights with wii = 0

ptest=25;

for u=1:1:ptest % loop over patterns
    
    s_inicial = patterns(:,u);
    s=s_inicial;

    pfijo=0;
    
    while (not(pfijo))
        pfijo=1;
        for i=1:1:N(k)
           if (s(i)*s'*W(:,i)<0)
           s(i) = -s(i);
           pfijo = 0;
           
           end 
        end
    end
    overlap(u)=(1/N(k))*(s'*patterns(:,u));
end

subplot(ng,ng,(k-1)*ng+j);
disp(j)
disp(k)
[histO,valores] = hist(overlap,10);
histOp = histO/ptest;
bar(valores,histOp,'hist'); grid
xlim([0 1]);
ylim([0 1]);
xlabel('Overlap'); ylabel('PDF');
title(['N=',num2str(N(k)),'  alpha=',num2str(alpha(j))]);

    end
end
