%Neural network program for classifying points inside/outside 4 quarter circles
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;
D11(1:N)=0;D11((X(1,:)-10).^2+(X(2,:)-10).^2<64)=1;
D12(1:N)=0;D12((X(1,:)-10).^2+(X(2,:)+10).^2<64)=1;
D21(1:N)=0;D21((X(1,:)+10).^2+(X(2,:)-10).^2<64)=1;
D22(1:N)=0;D22((X(1,:)+10).^2+(X(2,:)+10).^2<64)=1;
D(1:N)=D11+D12+D21+D22;
%figure,plot(X(1,D==0),X(2,D==0),'+')
%hold on,plot(X(1,D>0),X(2,D>0),'o')
figure,plot(X(1,:),X(2,:),'+')
M=[1:25];hold on,plot((8*cos(2*pi*M/100)-10),(8*sin(2*pi*M/100)-10),'.g')
hold on,plot((8*cos(2*pi*(M+25)/100)+10),(8*sin(2*pi*(M+25)/100)-10),'.g')
hold on,plot((8*cos(2*pi*(M-25)/100)-10),(8*sin(2*pi*(M-25)/100)+10),'.g')
hold on,plot((8*cos(2*pi*(M+50)/100)+10),(8*sin(2*pi*(M+50)/100)+10),'.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,plot((8*cos(2*pi*M/100)-10),(8*sin(2*pi*M/100)-10),'.g')
hold on,plot((8*cos(2*pi*(M+25)/100)+10),(8*sin(2*pi*(M+25)/100)-10),'.g')
hold on,plot((8*cos(2*pi*(M-25)/100)-10),(8*sin(2*pi*(M-25)/100)+10),'.g')
hold on,plot((8*cos(2*pi*(M+50)/100)+10),(8*sin(2*pi*(M+50)/100)+10),'.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,plot((8*cos(2*pi*M/100)-10),(8*sin(2*pi*M/100)-10),'.g')
hold on,plot((8*cos(2*pi*(M+25)/100)+10),(8*sin(2*pi*(M+25)/100)-10),'.g')
hold on,plot((8*cos(2*pi*(M-25)/100)-10),(8*sin(2*pi*(M-25)/100)+10),'.g')
hold on,plot((8*cos(2*pi*(M+50)/100)+10),(8*sin(2*pi*(M+50)/100)+10),'.g')

%Wo=[0.3037,0.6487,-0.0996,1.3108,-0.5643,1.5805,-1.4454,0.3744,-0.1028,-0.6078,1.1960]

%W=[-1.1899   -0.0593   -0.5779
%   -0.0214    0.3483    0.5621
%   -1.0185    0.0067    0.5025
%    0.0791    0.6671    0.3797
%   -1.4246    0.4343    0.0108
%    2.9659    0.2519    0.3384
%    0.3221    0.3637    0.6909
%   -0.1790    0.4690    0.5233
%    0.9175    0.1220    0.6306
%   -1.1660   -0.1275    0.1301
