MATLAB Answers


Wy does sim function give such bad results after making a neural network timeserie?

Asked by Daan
on 10 Sep 2013

Hi everyone,

I have a question about forecasting using neural network timeseries tool(ntstool).

I am working with a timeserie dataset. I have an input dataset of 217 rows and 24 columns (217x24 matrix) and a target dataset of 217 rows and 1 column (217x1 matrix).All values are numbers

So I go to ntstool and using narx for creating the network. After running the network adjusted dividerand into divideblock and run the script again. The network gave good validation and testing results. This is the code that came with it:

% input - input time series. output - feedback time series.

inputSeries = tonndata(input,false,false);

targetSeries = tonndata(output,false,false);

% Create a Nonlinear Autoregressive Network with External Input

inputDelays = 1:3;

feedbackDelays = 1:3;

hiddenLayerSize = 15;

net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize);

% Choose Input and Feedback Pre/Post-Processing Functions

net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};

net.inputs{2}.processFcns = {'removeconstantrows','mapminmax'};

% Prepare the Data for Training and Simulation

[inputs,inputStates,layerStates,targets] = preparets(net,inputSeries,{},targetSeries);

% Setup Division of Data for Training, Validation, Testing

net.divideFcn = 'divideblock';

net.divideMode = 'value';

net.divideParam.trainRatio = 70/100;

net.divideParam.valRatio = 15/100;

net.divideParam.testRatio = 15/100;

% Choose a Training Function

net.trainFcn = 'trainlm';

% Choose a Performance Function

net.performFcn = 'mse';

% Choose Plot Functions

net.plotFcns = {'plotperform','plottrainstate','plotresponse', ... 'ploterrcorr', 'plotinerrcorr'};

% Train the Network

[net,tr] = train(net,inputs,targets,inputStates,layerStates);

% Test the Network

outputs = net(inputs,inputStates,layerStates);

errors = gsubtract(targets,outputs);

performance = perform(net,targets,outputs)

% Recalculate Training, Validation and Test Performance

trainTargets = gmultiply(targets,tr.trainMask);

valTargets = gmultiply(targets,tr.valMask);

testTargets = gmultiply(targets,tr.testMask);

trainPerformance = perform(net,trainTargets,outputs)

valPerformance = perform(net,valTargets,outputs)

testPerformance = perform(net,testTargets,outputs)

% View the Network


% Closed Loop Network

netc = closeloop(net); = [ ' - Closed Loop'];


[xc,xic,aic,tc] = preparets(netc,inputSeries,{},targetSeries);

yc = netc(xc,xic,aic);

closedLoopPerformance = perform(netc,tc,yc)

% Early Prediction Network

nets = removedelay(net); = [ ' - Predict One Step Ahead'];


[xs,xis,ais,ts] = preparets(nets,inputSeries,{},targetSeries);

ys = nets(xs,xis,ais);

earlyPredictPerformance = perform(nets,ts,ys)

Now with this code I was ready for simulation. I took a sample row of the input dataset (24x1) and use the sim function to simulate the target:

adjusted_rowsample = mapminmax(rowsample);

simulate = tonndata(adjusted_rowsample,false,false);

samplerow_result = sim(netc,simulate).

The result wasnt even close to the target output as it was used in the network. Also when simulate new target value with new input values (24x1) from the next day ( this has not been used for training,validation or testing) the result is no where near the target.

What did I do wrong? How can I get the right target result using sim function?

And why does using sim(net,simulate) give me the error 'Number of inputs does not match net.numInputs.'? Which one to use: net or netc?

Can someone answer these questions?(On forum or private). I would very much appreciate it.



No products are associated with this question.

1 Answer

Answer by Greg Heath
on 19 Sep 2013
 Accepted answer

1. Remove all unnecessary specification of defaults. If you don't know which are defaults, type your net = narxnet command without the semicolon and investigate the results

2. Add a RNG initialization statement

3. Apply your code to the MATLAB time-series data set that best represents your problem.

 help nndatasets

4. If results are poor, run it a few more times to make sure it is not because of the random weight initialization.

5. Still bad? Post relevant code and error messages.

Hope this helps.

Thank you for formally accepting my answers



Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!