
% why perceptron algorithm can converge,
% but backpropagation cannot. Example using
% a simple dataset

x = [0,0;0,1;1,0;1,1];
t = [0,1,1,0];

% error surface for perceptron
spacing = 0.02;
for i=1:101
    w1 = -1.02 + spacing*i; % assume this is the w1
    for j=1:101
        w2 = -1.02 + spacing*j; % assume this is the w2
		error = 0;
		for cases=1:4 
                    % present each case to the neuron
		    % and see how well it performs
		    error = error +  ...
		    (t(cases) - ([x(cases,:),1]*[w1,w2,1]'))^2;
		end
		% record the error
		d(i,j) = error;
    end
end

% plot error as a function of w1 and w2
surf(-1:spacing:1,-1:spacing:1,d);
view(-30,-5);
% notice the global minimum
% end of movie, credits roll

%--------------------------------------------------
% in case you are interested, here is the learning
% algorithm for the perceptron

% initialize weights to some small values
w = [0.1, -0.1, 0.2];
eta = 0.1;
for i= 1:100 % do it for 100 iterations
	for j = 1:4 % for each input
		actual = [x(j,:),1]*w';
		target = t(j);
		w = w + eta * (target - actual) * [x(j,:),1];
        end		  
end

