You are on page 1of 2

Practical for week 9 Adaline

1. Open up MATLAB
2. Click on File, New to open a new M-file
3. Type in the following
function [I,W,O]=neural1(n,t)
%Adaline program for AND gate
W=2.*(rand([3,1])-0.5);
I=[1,0,0;1,0,1;1,1,0;1,1,1];
d=1;count=1;
O=[0;0;0;1];
%O=[0;1;1;0];
while ((abs(d)>0.001)&(count<t))
for loop=1:4
o1=h11(W(1).*I(loop,1)+W(2).*I(loop,2)+W(3).*I(loop,3));
d1(loop)=O(loop)-o1;
for loop2=1:3
W(loop2)=W(loop2)+(n.*d1(loop).*I(loop,loop2)');
end;
end;
d=max(abs(d1'));count=count+1
end;
4. Save it as neural1.m
5. Create a new m-file and type in the code below
function x=h11(y)

%x=1./(1+exp(-0.1.*y));
if y>=0
x=1;
else
x=0;
end;
6. save as h11.m
7. Now in the MATLAB workspace type in the following
[i1,w1,O1]=neural1(0.1,1000)

What did you see happen? You should have seen some number labelled count going up
and stopping, and then the results of i1 the input set, w1 the weights and the desired
outputs O1 should have been listed. We can now use the weights produced and the input
set to test if we can get the same output from our network as the desired output.

8. Type in the code below which tests the network and produce outputs for each input
pattern. Save the file as test_adaline
function [ol]=test_adaline(I,W)

for loop=1:4
ol(loop)=h11(W(1).*I(loop,1)+W(2).*I(loop,2)+W(3).*I(loop,3));
end;
ol=ol';
9. Now in the MATLAB workspace type in the instruction, if the network has worked o1
should give the desired output.
[ol]=test_adaline(i1,w1)

10. Now go into the function neural11.m and change the lines
O=[0;0;0;1];
%O=[0;1;1;0]; to the following
%O=[0;0;0;1];
O=[0;1;1;0];
11. Run the routines neural1.m and test_adaline.m as before. What did you see happen?
Can you explain any differences? It might look at first that it has trained, by running
test_adaline should tell you if it did.
12. for interest: Can you get the Adaline to do any other logical operations.

You might also like