Tuesday 2 February 2016

Implement perceptron for AND function using bipolar inputs

%Code

clc
clear all;
close all;

x1=[-1 -1 1 1];
x2=[-1 1 -1 1];
t=[-1 -1 -1 1];
alpha=input('Enter the value of alpha=');
th=input('enter the threshold=');
yin=zeros(1,4);
y=zeros(1,4);
w1=0;
w2=0;
b=0;
c=1;
cnt=0;
while(c)
    c=0
    for i=1:4
        %yin(i)=b+(x(i,1)*w1)+(x(i,2)*w2);
       yin(i)=b+(x1(i)*w1)+(x2(i)*w2);
        if yin(i)>th
            y(i)=1;
        else if yin(i)<-th
                y(i)=-1;
            else
                y(i)=0;
            end
            if t(i)~=y(i)
                w1=w1+(alpha*x1(i)*t(i));
                w2=w2+(alpha*x2(i)*t(i));
                b=b+(alpha*t(i));
                c=1;
            else
                w1=w1;
                w2=w2;
                b=b;
            end
        end
        cnt=cnt+1;
    end
    disp('OUTPUT MATRIX:');
    disp(y);
    disp('w1=');
    disp(w1);
    disp('w2=');
    disp(w2);
    disp('bias=');
    disp(b);
    a1=x1;
    a2=x2;
    a2=(-w1/w2)*a1-(b/w2);
    plot(x1,x2,'*r',a1,a2);
    axis([-2 2 -2 2]);
end

No comments:

Post a Comment