% this function provides a rough outline how to set up 
% a feedforward neural network in MATLAB. Check "help  backprop"
% for more information

% load data sets

load X.data -ascii;
load T.data -ascii;

% Create the Neural Network

% specify first layer

S1=2;              % number of units first layer
F1='logsig';      % activation function of first layer

% specify the second layer

S2=2;              % number of units second layer (output layer for 2 layer net)
F2='purelin';      % activation function of second layer

% note: you can specify a third layer if desired (see "help newff")

% initialize the network (check "help newff")
net = newff([min(X)' max(X)'],{S1,S2},{F1,F2},'traingd','learngd','mse');

% initialize some of the "steering parameters" for backprop training
net.trainParam.epochs = 1000; % Maximum number of epochs to train
net.trainParam.lr     = 0.01; % Learning rate
net.trainParam.show   = 100;  % Epochs between updating display
net.trainParam.goal   = 0;    % Sum-squared error goal (rarely useful)

% initialize arrays to monitor training and testing error during learning

MSE_train=[];
MSE_test=[];
epochs=[];

% activate figure #1
figure(1);
clf;

% assign a test data set (currently the same as training data)
VV.P=X';
VV.T=T';

% training with for net.trainParam.epochs
[net,tr]=train(net,X',T',[],[],VV);

% activate figure #2
figure(2);
clf;

% extract performance variables
epochs     = tr.epoch;
MSE_train  = tr.perf;

% plot the error curve
plot(epochs,MSE_train);
ylabel('nMSE-train');
xlabel('epochs');
