% Lab 6, problem 1
% you must first load in apples and oranges data

% initialize data array
data=[apples oranges];
[N K]=size(data);

% initialize teacher
teacher=[ones(1,K/2) -ones(1,K/2)];

% learning rate
eta=.001;

% number of trials - you may want to make this longer
num_trials=1000;

% initialize weights
w=randn(2,1);
w0=randn(1);

% initialize data plot
figure(1)
plot(apples(1,:),apples(2,:),'+',oranges(1,:),oranges(2,:),'o')
hold on
x1=0:4;
x2=-(w(1)*[0:4]+w0)/w(2);
h=plot(x1,x2,'EraseMode','xor');

% loop over trials
for t=1:num_trials
   
   dw=0;
   dw0=0;
   e2=0;
   
   % loop over training set
   for alpha=1:K
      
      y=w'*data(:,alpha)+w0;         % compute neuron output
      e=teacher(alpha)-y;	     % error
      e2=e2+e^2;                     % accumulate squared error
      dw=dw+2*eta*e*data(:,alpha);   % accumulate dw
      dw0=dw0+2*eta*e;               % accumulate dw0
      
   end
   
   % update weights
   w=w+dw;        
   w0=w0+dw0;
   
   % save E for this trial
   E(t)=e2;
   
   % update display of separating hyperplane
   x2=-(w(1)*[0:4]+w0)/w(2);
   set(h,'YData',x2)
   drawnow
%   pause(0.25)
   
end

hold off

figure(2)
plot(E)
