function hopfield(C, R, I)
% HOPFIELD -   Hopfield network with two neurons
%
% CALL ROUTINE
%   hopfield(C, R, I)
%
% INPUT
%   C      -   Capacitance vector
%              [C1 C2]
%   R      -   Resistance matrix
%              [R11 R12]
%              [R12 R22]
%   I      -   External input vector
%              [I1 I2]
%
% OUTPUT
%   This function will produce the phase portrait of the Hopfield neural
%   nets
%
% Andy Byerly
% Ricky Chan
% ECE 595C
% October 9, 2004

close all;
clc

% If there's no input specified, these parameters will be utilized
if nargin ~= 3
    C = [1 1];
    R = [12.8272 -12.8375;
         -12.8375 12.7193];
    r = [34.8009 35.3497];
    I = [0 0];
end

global R11 R12 R21 R22 C1 C2 r1 r2 I1 I2 theta
theta = 1;
R11 = R(1,1);
R12 = R(1,2);
R21 = R(2,1);
R22 = R(2,2);
C1 = C(1);
C2 = C(2);
r1 = r(1);
r2 = r(2);
I1 = I(1);
I2 = I(2);
tspan=[0 40];
numIter = 300;
max = 1-eps;
min = eps;
rand('state',sum(100*clock));

figure
for counter = 1:numIter
    x0 = rand(2,1)*(max-min) + min;
    x0 = x0';
    [t,x]=ode23s('plant',tspan,x0);    
    plot(x(:,1),x(:,2));
    hold on
end
axis([0 1 0 1]);
title('Phase Portrait of a Hopfield net with two neurons');
xlabel('{\it x}_1');
ylabel('{\it x}_2');

function u = sigInv(x, theta)
% SIGINV    - Inverse of Sigmoid function
%
% CALL ROUTINE
%   u = sigInv(x, theta)
%
% INPUT
%   x       -   vector output of the nonlinear amplifier  
%   theta   -   slope of the Sigmoid function
%
% OUTPUT
%   u       -   input to the nonlinear amplifier

u = theta.*(log(x) - log(1-x));

function y = dsigInvdx(x, theta)
% DSIGINVDX    - derivative of the Inverse of Sigmoid function
%
% CALL ROUTINE
%   u = dsigInvdx(x, theta)
%
% INPUT
%   x       -   vector output of the nonlinear amplifier  
%   theta   -   slope of the Sigmoid function
%
% OUTPUT
%   u       -   input to the nonlinear amplifier
y = theta.*(1./x + 1./(1-x));
