%Neural network program for classifying points inside/outside circle.
clear;a=7;mu=0.01;K=1000;L=10;N=100;
%K=#iterations. L=#neurons in hidden layer. N=#training.
X=20*rand(2,N)-10;D(X(1,:).^2+X(2,:).^2>64)=0;D(X(1,:).^2+X(2,:).^2<64)=1;
%Half of X inside circle of radius 8, half outside, since pi*8^2=201=(20)^2/2.
figure,plot(X(1,D==0),X(2,D==0),'+')
hold on,plot(X(1,D==1),X(2,D==1),'o')
hold on,I=[1:100];plot((8*(cos(2*pi*I/100))),(8*sin(2*pi*I/100)),'.g')
%Training signals created. Now start neural network.
W(1:L,1:3)=0.5*rand(L,3);Wo(1:L+1)=0.5*rand(1,L+1);%Initialize weights.
%W(i,j) is weight from input j=1,2 and constant input to hidden layer input i=1...L.
%Wo(i) is weight from hidden layer output Y(i) i=1...L and constant input to output yo.
for k=1:K;%Iteration number
%Run NN forward:
for I=1:N;V=W*[1;X(:,I)];Y(:,I)=1./(1+exp(-a*V));
vo=Wo*[1;Y(:,I)];yo(I)=1/(1+exp(-a*vo));
%Backpropagation: Output layer: delta0.
deltaL(I)=-a*(D(I)-yo(I))*yo(I)*(1-yo(I));%scalar
%deltaI(:,I)=a*[1;Y(:,I)].*(1-[1;Y(:,I)])*deltaL(I).*Wo';%11X1
deltaI(:,I)=a*Y(:,I).*(1-Y(:,I))*deltaL(I).*Wo(2:11)';%10X1
%Update weights using steepest descent:
W=W-mu*deltaI(:,I)*[1;X(:,I)]';Wo=Wo-mu*deltaL(I)*[1;Y(:,I)]';end;end;
%Check neural network on given training:
figure,plot(X(1,yo<0.5),X(2,yo<0.5),'+')
hold on,plot(X(1,yo>0.5),X(2,yo>0.5),'or')
hold on,I=[1:100];plot((8*(cos(2*pi*I/100))),(8*sin(2*pi*I/100)),'.g')
%Test neural network on new points:
Z=20*rand(2,N)-10;for I=1:N;
V=W*[1;Z(:,I)];Y(:,I)=1./(1+exp(-a*V));vo=Wo*[1;Y(:,I)];yo(I)=1/(1+exp(-a*vo));end;
figure,plot(Z(1,yo<0.5),Z(2,yo<0.5),'+')
hold on,plot(Z(1,yo>0.5),Z(2,yo>0.5),'or')
hold on,I=[1:100];plot((8*(cos(2*pi*I/100))),(8*sin(2*pi*I/100)),'.g')

%Wo=[-2.3487,0.8790,1.1477,0.6194,-1.0793,0.3065,0.7130,0.6564,-0.8504,0.3006,-0.6792

%W=[0.8979    0.5901   -0.3065
%   2.2884    0.2527   -0.2603
%   1.2294    0.0221    0.2993
%  -1.8089    0.2253    0.2024
%   1.1969   -0.2629    0.1935
%   0.9640    0.5880    0.4538
%   1.8906    0.0745    0.2663
%  -1.3807    0.5270    0.0109
%   1.1689   -0.1188    0.1365
%   0.1329    0.8196    0.6333