%hebb
% THere seems to be a bug with quiver in octave ! May 04

close all;

negcor = 1; % 1 or 0  pos. or neg. input correlation
subnorm = 0; % {0,1} use subtractive normalisation ?
multnorm= 1; % {0,1} use multiplicative norm.?
wmax =1; % hard limit on weight
wmin =0;


ntr=100;
inmat = zeros(2,ntr);
ranx = randn(1,ntr);
rany = randn(1,ntr);
ranz = randn(1,ntr);
inmat(1,:)=(ranx+(1-2*negcor)*ranz)/sqrt(2);
inmat(2,:)=(rany+ranz)/sqrt(2);

plot(inmat(1,:),inmat(2,:),'x')
cov(inmat')

eta=0.01/ntr;% learning rate

%vectorplot
qx=[]; qy=[]; qu=[];  qv=[];

% pick starting values for weight
winitmin =0;
winitmax =1;
for w1init=winitmin:0.1:winitmax
    for w2init=winitmin:0.1:winitmax

        w1 = w1init;
        w2 = w2init;
        for itr=1:ntr
            x1 = inmat(1,itr);
            x2 = inmat(2,itr);
            y = w1*x1+w2*x2;
            dw1 = x1*y;
            dw2 = x2*y;

            if (subnorm==1)
                dw1 = dw1 -y*(x1+x2)/2;
                dw2 = dw2 -y*(x1+x2)/2;
            elseif (multnorm==1 & w1+w2 ~=0)
                dw1 = dw1 -y*(x1+x2)/(w1+w2)*w1;
                dw2 = dw2 -y*(x1+x2)/(w1+w2)*w2;
            end
            w1 = w1+ eta*dw1;
            w2 = w2+ eta*dw2;

            % put hard limits to maximal weight
            % -wmax < w < wmax
            w1 = min(wmax,w1);
            w2 = min(wmax,w2);
            w1 = max(wmin,w1);
            w2 = max(wmin,w2);

        end
        qx = [qx w1init];
        qy = [qy w2init];
        qu = [qu w1-w1init];
        qv = [qv w2-w2init];
    end
end
figure
scale=3
quiver(qx,qy,qu,qv,scale)
