function w = prob_5a
% Problem 5a
%
% This script uses the pseudoinverse method and Least-Squares error to classify 
% a linearly-separable set of training data.  The training data is generated by 
% the script gen2classes.m.  The classifier will respond with +1 for data in 
% the set represented by X1 and will respond with -1 for data in the set 
% represented by X2.  
%
% This script will return the weight vector that discriminates between the 
% two classes.
%
% An applied bias will be represented by an additional input with a fixed value 
% of +1.  Modifying the associated weight will result in a variable bias 
% being applied to the neuron.

% Get our two sets of training data X1 and X2 and make sure we haven't been 
% given crap data
gen2classes;
tic
if size(X1,2) ~= size(X2, 2);
    disp 'X1 and X2 must represent training data of the same dimension.  Make' 
    disp 'the columns the same size'
    return;
end;

% Set up our input matrix
X = [ X1; X2 ];

% Insert a column for the bias into the training data
X = [ ones(size(X,1),1) X ];

% Set up a vector containing the desired response
d = + ones(size(X1, 1), 1);
d = [ d ; - ones(size(X2, 1), 1) ];


% Set up our weight vector (initially zero)
w = zeros(size(X, 2), 1);

% Find the error
e = d - X*w;

% Find the new weights using Least-Squares
w = inv( X.'  * X )  *  X.'  *  d;

% Plot the discriminating line
toc
drawline(w, 'r');