% perceptron_demo.m
% 
% David Pan, UAH


% Class 1: [3 3 1], Class 2: [1 1 1]

a = 1;  % learning rate

% Augmented input vector
x1 = [3 3 1];
x2 = [1 1 1];

w = [0 0 0];    % initial weight vector

for epoch = 1: 20
    w_prev = w; 
    x = x1; 
    y = dot(w, x);

    if (y <= 0)
        w = w + a*x;
    end
    
    x = x2; 
    y = dot(w, x);

    if (y >= 0)
        w = w - a*x;
    end
    
    if (w == w_prev)
        break; 
    end
end

epoch

w

dot(w, x1)
dot(w, x2)
        
% Use Matlab built-in function
% Do not use the augmented input vector
x1 = [3 3];  
x2 = [1 1];

% The input matrix: (vert: features, horz: samples)
x = [x1' x2'];   

% Target has to be 0/1 values for binary classification
target = [0 1];
method = perceptron;
net = train(method, x, target);

% View the weights for the connection from the first input to the first layer
net.iw{1,1}
% View the bias values for the first layer 
net.b{1}

y = net(x);
error = y - target

% Display the training data and decision boundary
figure; 
hold on;
plotpv(x,target);
plotpc(net.iw{1},net.b{1});
axis equal
grid


    
