% Homework 2 Question 3
function [w, b, e, num_epochs] = hw2q3(p_in, t_in)

     % the function gets input-target pairs like the example in manual
     % p = [[2;2] [1;-2] [-2;2] [-1;1]]
     % t  = [0 1 0 1]
     % the intital values for weight matrix and bias is 0 as default

     % at each loop, perceptron is trained once
     % weight matrix, bias, corresponding error
     % and the count on epoch is displayed at each iteration
     % so it is easy to follow up the algorithm

     % number of epochs are used to count the trials
     % check is used to decide when to stop training
     % initial error is 1
     num_epochs = 0;
     e = 1;
     check = 0;

     % defining and initiallizing the perceptron w/ 1 neuron
     % input range is defined to be [-100 100] just to be safe
     my_net = newp([-100 100; -100 100], 1);
     my_net.trainParam.epochs = 1;
     p = p_in;
     t = t_in;
     while ( check < 1)
	num_epochs = num_epochs + 1
	if (norm(e) == 0)
		check = 1;
	end
     	my_net = train(my_net, p, t);
     	w = my_net.IW{1,1}
     	b = my_net.b{1}
     	e = hardlim(w * p + b) - t
     	
     	
     end
