% Simulation of Hopfield with
clear all
close all
clc

N = 1000;  % number of neurons
P = 40; % load factor

prob=0.5; % 

patrones = 2*binornd(1,prob,N,P)-1; % generation of patterns

J=(1/N)*patrones*(patrones');
D=diag(diag(J));
W=J-D; % matrix of weights with wii=0

T=[0.1:0.2:2]; % valules of temperature
beta=1./T;

for t=1:1:length(T)  % for all values of T
    
    overlap=zeros(1,P);
    
    for u=1:1:P  % for all patterns
    
        s_ini = patrones(:,u);
        s=s_ini;

        for n=1:1:10    % 10 iterations
            
            for i=1:1:N     % for all neurons
        
                h=s'*W(:,i);
                prob_1= 1/(1+exp(-2*beta(t)*h));
                if (rand([1,1]) < prob_1)
                    s(i)=1; 
                else
                    s(i)=-1;
                end
           
            end
        
        end
        overlap(u)=(1/N)*((s'*patrones(:,u)));
    
    end

    overlap_m(t)=sum(overlap)/P;
    disp(1/beta(t))
    disp(overlap_m(t))
end

stem(T,overlap_m); grid

