% Example of using multilayer feedforward network
% for classification

clear all
figure(1)
clf

% Create training data
% input data
randn('state',1); % initialize random sequence for training data
x = randn([2 600]);
%Output data
o = ((x(1,:)/2).^2+(x(2,:)).^2)<1;



% plot input pattern space
n0 = 0;
n1 = 1;
for i=1:size(x,2)
   if ((x(1,i)/2)^2+(x(2,i))^2)<1
      % output class 1
      n1 = n1+1;
      class1(:,n1) = x(:,i);
   else
      % output class 0
      n0 = n0+1;
      class0(:,n0) = x(:,i);
   end
end

plot(class1(1,:),class1(2,:),'rx')
hold on
plot(class0(1,:),class0(2,:),'bo')
axis equal
v = axis;
legend('Class 1','Class 0')
xlabel('X1')
ylabel('X2')
pause

% Building the Network 
% Range of input data
PR = [min(x(1,:)) max(x(1,:));
   min(x(2,:)) max(x(2,:))]; 

% No. of nodes in each layer
S1 = 10; %layer 1
S2 = 1;  %layer 2

TF1 = 'logsig';
TF2 = 'logsig';

%Back propagation network learning function
BTF = 'trainlm';

%Back propagation weight/bias learning function
BLF = 'learngd';

%Cost function or performance function
PF = 'mse';

% initialize random sequence for initialize weight
rand('state',8);

% Create the network
net = newff(PR,[S1 S2],{TF1 TF2},BTF,BLF,PF);

% Before learning
m = net.IW{1}; b = net.b{1};  

t=min(x(1,:)):0.1:max(x(1,:));
for i=1:size(m,1)
   % plot line of w1x1+w2x2+b=0
   if (m(i,2)~=0)
      plot(t,-m(i,1)/m(i,2).*t-b(i)/m(i,2)) 
   else
      plot(-b(i)/m(i,1)*ones(size(t)),t)
   end
   hold on
end
axis(v)
legend('Class 1','Class 0')
title('Boundary lines before training')
xlabel('X1')
ylabel('X2')
pause

%Set no. of training rounds
net.trainParam.epochs = 20000;

%Set minimum error criterion
net.trainParam.goal = 0.002;

% Training the network
net = train(net,x,o);


%compute the network output after training
y = sim(net,x);

% Hard Thresholding to get binary output
netout = y>0.5;

% Compute final error
err = sum(abs(o-netout))

% plot input pattern space
figure(1)
clf
plot(class1(1,:),class1(2,:),'rx')
hold on
plot(class0(1,:),class0(2,:),'bo')
axis equal


% After learning
m = net.IW{1}; 
b = net.b{1};  

t=min(x(1,:)):0.1:max(x(1,:));
for i=1:size(m,1)
   % plot line of w1x1+w2x2+b=0
   if (m(i,2)~=0)
      plot(t,-m(i,1)/m(i,2).*t-b(i)/m(i,2)) 
   else
      plot(-b(i)/m(i,1)*ones(size(t)),t)
   end
   hold on
end
axis(v)
legend('Class 1','Class 0')
xlabel('X1')
ylabel('X2')
title('Boundary lines after training')

