 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % fully connected continues recurrent ANN, with s in [-1,1]
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 clear; clf; hold on;

 %%%%%%% Network parameters %%%%%%
   nn=500;     % number of nodes
   npat=50;    % number of pattern
   dt=.01 ;    % time step
   tau=1;      % time constant
   tend=0.1;   % end of simulation
 %%%%%%% random binary pattern with elements [-1;1] %%%%%%%%%%
   pat=2*floor(2*rand(nn,npat))-1;

 %%%%%%%%% weight matrix: Hebb learning %%%%%%%%%
   w=pat*pat';
   %w=w./npat-diag(1); %if we want to remove self couplings

 for dist0=0.1:0.1:.9; %loop over different initial distances
   %%%% initial pattern: pattern(1) with nflip flips %%%%%%%
   u=pat(:,1);
   nflips=floor(dist0*nn);
   flag=zeros(nn,1);
   iflip=1;
   while iflip<=nflips;
     ii=floor(nn*rand)+1;
     if flag(ii)==0; u(ii)=-u(ii); flag(ii)=1; iflip=iflip+1; end
   end
   s=tanh(u);
   x(1)=0;
   y(1)=(1-pat(:,1)'*s/(sqrt(pat(:,1)'*pat(:,1))*sqrt(s'*s)))/2;
   %%%%%%%%% Update %%%%%%%%%%
   irec=1;
   for t=dt:dt:tend;
       s=tanh(u);
       u=(1-dt/tau)*u+dt/tau*(w*s);
       irec=irec+1;
       x(irec)=t;
       y(irec)=(1-pat(:,1)'*s/(sqrt(pat(:,1)'*pat(:,1))*sqrt(s'*s)))/2;
   end  %time
   plot(x,y);
 end % dist0
 axis([0 tend 0 1]); xlabel('Time [tau]'); ylabel('Distance')
