clear;
clc;
close all;

 X=[0 0       %input matrix
        0 1
        1 0
        1 1];
 
  D=[0        %correct output matrix
        1
        1
        0];

net = feedforwardnet;  %  Create the network

X=X';
D=D';

net.trainparam.goal =0;
net.divideparam.valratio = 0;
net.divideparam.trainratio =1;
net.divideparam.testratio =0;
net.trainparam.min_grad = 1e-100;
net.trainparam.showwindow=0;  %don't show the training window when running the program

net = train(net,X,D);     % Train the network 

X1=input('Enter the value of the first input node = ');
X2=input('Enter the value of the first input node = ');

y = net([X1 X2]') ;  % y is the output of the trained network given an input sample trial. In this case the input values at the two input nodes are X1 and X2. 
y=round(y);  % Forcing the output number to be either 1 or 0

disp(['For input values of ',num2str(X1),' and ',num2str(X2),' the network output is ',num2str(y)]);

%You can save your network for later use by typing:
%save myXORnetwork net;  %We've created a variable called myXORnetwork and
%placed "net" our trained network in that variable.
%You can then reload "net" later by using: load myXORnetwork 
%net is our trained network and is a variable with many fields.  
%if you type class(net) in the command window you'll see that net is a
%variable of type "network".  This is a Matlab class just like if you type
%x=1 and then type class(x) you'll see "double" (double precision number).


