# Training feedforward neural network in Matlab

I have to approximate the function Tnew= (9T1 + 8T2 + 4T3 + 4T4 + 2T5)/27, where T1,T2,T3,T4 and T5 are 13600-by-1 vectors (loaded from a given dataset). All the Ti's are functions of two variables X1 and X2 (X1 and X2 are also loaded from the same dataset). More specifically, I am asked to do the following:

- Your dataset consists now of X1, X2 and Tnew. Draw 3 (independent) samples of 1000 points each. Use them as the training set, validation set, and test set, respectively. Motivate the choice of the datasets. Plot the surface of your training set.
- Build and train your feedforward Neural Network: use the training and validation sets. Build the ANN with 2 inputs and 1 output. Select a suitable model for the problem (number of hidden layers, number of neurons in each hidden layer). Select the learning algorithm and the transfer function that may work best for this problem. Motivate your decisions. When you try different networks, clearly say at the end which one you would select as the best for this problem and why.

This is what I have so far:

```
Tnew=(9.*T1+8.*T2+4.*T3+4.*T4+2.*T5)./27;
trainset1=datasample(X1, 1000);
trainset2=datasample(X2,1000);
trainset3=datasample(Tnew, 1000);
valset1=datasample(X1, 1000);
valset2=datasample(X2,1000);
valset3=datasample(Tnew, 1000);
testset1=datasample(X1, 1000);
testset2=datasample(X2,1000);
testset3=datasample(Tnew, 1000);
plot3(trainset1,trainset2,trainset3), grid on;
title('Surface training set');
xlabel('X1'),ylabel('X2'),zlabel('Tnew');
alg1 = 'traingd';% First training algorithm to use
H = 50;% Number of neurons in the hidden layer
delta_epochs = [1,14,985];% Number of epochs to train in each step
epochs = cumsum(delta_epochs);
z=[trainset1';trainset2'];
net1=feedforwardnet(H,alg1);% Define the feedfoward net (hidden layers)
net1=configure(net1,z,trainset3);% Set the input and output sizes of the net
net1.divideFcn = 'dividetrain';% Use training set only (no validation and test split)
net1=init(net1);% Initialize the weights (randomly)
net1.trainParam.epochs=delta_epochs(1); % set the number of epochs for the training
net1=train(net1,z,trainset3); % train the networks
a11=sim(net1,z) % simulate the network
net1.trainParam.epochs=delta_epochs(2);
net1=train(net1,z,trainset3);
a12=sim(net1,z)
net1.trainParam.epochs=delta_epochs(3);
net1=train(net1,z,trainset3);
a13=sim(net1,z)
```

However, this gives the following error:

```
Error using network/train (line 340)
Inputs and targets have different numbers of samples.
Error in personnalregression (line 33)
net1=train(net1,z,trainset3); % train the networks
```

Can anyone help me? Moreover, how do I incorporate the validation set in my program?