function demoAndOr(flag) %function demoAndOr(flag) % flag: 1 - and, 2- or 3 - xor %rand('state',0) % Define input patterns two_inputs = [0 0; 0 1; 1 0; 1 1]; % Define target vectors and = [0 0 0 1]; or = [0 1 1 1]; xor = [0 1 1 0]; % Define eta value and maximum number of iterations eta = 0.1; maxIters = 200; % Train network to learn each pattern if nargin == 0, flag = 1; end if (flag == 1) disp('AND:') [w,w0] = learn2D(two_inputs,and,eta,maxIters); elseif flag == 2 disp('OR:') [w,w0] = learn2D(two_inputs,or,eta,maxIters); elseif flag == 3 disp('XOR:') [w,w0] = learn2D(two_inputs,xor,eta,maxIters); end end %function function [weights,w0] = learn2D(inputs,target,eta,cycles) % [w,w0] = learn2D(x,y,eta,cycles) % where % x is a vector of input vectors % y is a vector of corresponding expected responses % eta is the fraction step size for perceptron learning rule % cycles is the max number of iteration without convergence before giving up % and % w is a vector of the weights % w0 is the threshold value N = length(target); weights = rand(1,length(inputs(1,:))); w0 = rand; disp('Initial Weights:'); str = sprintf('\tw0 =%g',w0); disp(str); for i=1:length(weights), str = sprintf('\tw%g = %g',i,weights(i)); disp(str); end counter = 1; % Plot Initial figure x1 = inputs(:,1); x2 = inputs(:,2); x = [-2:0.1:2]; hold on for i = 1:N, if (target(i)==1), plot(x1(i),x2(i),'.'); else, plot(x1(i),x2(i),'o'); end end hold off while counter <= cycles, % Use Perceptron Learning Rule to set weights for i = 1:N, output = perceptron(weights,inputs(i,:),w0); delta(i) = target(i)-output; weights = weights+eta*delta(i).*inputs(i,:); w0 = w0+eta*delta(i); end if (sum(abs(delta)) == 0), str = sprintf('\nConvergence after %d iterations.\n',counter-1); disp(str); counter = cycles; % Plot Final values hold on; y = -((x*weights(1)+w0)/weights(2)); plot(x,y,'r'); axis([-1 2 -1 2]); hold off; title(str); break; end % Plot incremental values if counter<10 hold on; y = -((x*weights(1)+w0)/weights(2)); plot(x,y,':'); axis([-1 2 -1 2]); hold off; pause(1) end counter = counter + 1; end if (counter > cycles), str = sprintf('\nFAILURE to converge after %d iterations.\n',counter-1); disp(str); title(str); end % Display results disp('Final Weights:'); str = sprintf('\tw0 =%g',w0); disp(str); for i=1:length(weights), str = sprintf('\tw%g = %g',i,weights(i)); disp(str); end testResults(weights,inputs,w0); end % function function [v] = perceptron(w,u,w0) % Perceptron that returns an output for a given input vector,u, with % weights w and threshold value w0. The length of vectors u and w must be the % same. if (length(w) ~= length(u)) error('length of w and u must match'); end if ((w*u'+w0) >= 0) v = 1; else v = 0; end end % function function testResults(w,u,w0) % Displays inputs and outputs for 2 node network str=sprintf('\nResults for given weight and inputs:\nx1 | x2 | y'); disp(str); disp('----|-------|----'); for i=1:length(u(:,1)), str = sprintf('%g | %g | %g',u(i,1),u(i,2),perceptron(w,u(i,:),w0)); disp(str); end str = sprintf('\n'); disp(str); end