%----------------------------------%
% Course 168 481 Class 2002        %
% Homework No. 1                   %
% Single layer feedforward network %
% How the network learns           %
%----------------------------------%


%Generate data for this problem
figure(1)
clf
figure(2)
clf
a = 1;
b = 0.5;
c = -0.1;
d = 1;
e = 0.5;
f = 0.1;

t = -1:0.01:1
s = (-a*t - c)/b;
figure(1)
plot(t,s,'r')
hold on
s = (-d*t - f)/e;
plot(t,s,'g')

x = [];
y = [];
rand('state',0)
% Here we have training data for the network
% if (x(1),x(2)) is above the line a*x(1) + b*x(2) + c = 0 then y = 1
% if (x(1),x(2)) is under the line d*x(1) + e*x(2) + f = 0 then y = 0
for i = 1:30
   z = (rand([2 1])-0.5)*2
   if (z(1)*a + z(2)*b + c > 0)
      y = [y 1];
      x = [x z];
   elseif (z(1)*d + z(2)*e + f < 0)
      y = [y 0];
      x = [x z];
   end
end
figure(1)
hold on
for i = 1:size(y,2)
   if (y(i) == 1)
      plot(x(1,i),x(2,i),'rx')
   else
      plot(x(1,i),x(2,i),'go')
   end
end
legend('x = class y = 1','o = class y = 0')
axis equal
axis([-1.2 1.2 -1.2 1.2])

% Problem 
% 1. Make a single layer network with 2 input nodes and one output node 
%    to classify data
%    Hint : network equation y = f(w1*x1 + w2*x2 + b)
%           where f is an activation function
% 2. Initial w1,w2,b using random values
% 3. Adjust w1,w2,b using gradient descent method
% 4. At each training step, record w1,w2,b
% 5. When the network can classify 100% correctly, stop
% 6. plot (w1,w2) and see how the weight w1 and w2 change
% 7. go to step 2 again, change initial values and repeat procedure 3-6

