close all
clear
clc
Xa=[];
Ya=[];
Xb=[];
Yb=[];
input=[];
num_ip = 4;
bias = -1;
learning_rate = 0.1;
weights = rand(3,1);
z=0:0.1:10
iterations = 1000
while(1)
if(length(Xa)==4)
break;
end
axis([0 10 0 10]); % open an axis from 0__>10 in each axis
grid on
[x,y]=ginput(1); % to get input from user .the programm Continue to get input from user
% until you press enter .
plot(x,y,'ro'); % plot the point
hold on
if(isempty(x)) % if you press enter the programm break
break;
end
Xa(end+1) = x; % to save the x data in a vector
Ya(end+1) = y; % to save the y data in a vector
end
tempx=[Xa ]'
tempy= [Ya]';
input=[tempx tempy];
desired_out = [0;0;0;1];
for i = 1:iterations
out = zeros(4,1);
for j = 1:num_ip
y = weights(1,1)*input(j,1)+ weights(2,1)*input(j,2)+weights(3,1)*bias;
if(sign(y)==0 || sign(y)==1 )
out(j) = 1 ;
end
if(sign(y)==-1)
out(j) = 0 ;
end
delta = desired_out(j)-out(j);
weights(1,1) = weights(1,1)+learning_rate*input(j,1)*delta;
weights(2,1) = weights(2,1)+learning_rate*input(j,2)*delta;
weights(3,1) = weights(3,1)+learning_rate*bias*delta;
hold on
end
end
classifier= -(weights(2,1)/weights(1,1))*z - bias*(weights(3,1)/weights(1,1));
plot(z,classifier)