MATLAB: Neural network, narxnet, multi-step prediction

close loopmulti stepnarxnetneural networkprediction

Dear all,
I have a question and hope to hear some advices from you.
Given a narxnet model, I would like to use it to predict the output corresponding to a new input knowing ONLY the initial condition of the output . It's different from what I've seen so far when one is given the past values of both input and output (I made a search already, hope that I did not miss important things).
My question is: is it correct if I do as in the following (please look at the part where I define T2 and make prediction with closeloop. The outcome is depicted in the attached pdf, can also be obtained by running the code. You will see an imperfection during the first ten steps when the prediction by narxnet differs from the actual output)? If it is not correct, can you please give me a hint about what to improve? Any advice is welcome!!!
the code is as follows (thanks to Greg for commenting, hope that it's clearer now)
clear all; clc;
[X, T]= simplenarx_dataset;
net=narxnet();
% set all weights and biases equal to zero
net = setwb(net,zeros(1,net.numWeightElements));
% Prepare the Data for Training and Simulation
[x,xi,ai,t] = preparets(net,X,{},T);
% Train the Network
[net,tr] = train(net,x,t,xi,ai);
% Test the Network
y = net(x,xi,ai);
performance = perform(net,t,y)
view(net)
Closed Loop Network Use this network to do multi-step prediction.
netc = closeloop(net);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%QUESTION IS IN THE FOLLOWING PART
% PEOPLE USUALLY DO THIS, HOWEVER, T IS NOT KNOWN A PRIORI
% [xc,xic,aic,tc] = preparets(netc,X,{},T);
% IS IT CORRECT TO DO AS FOLLOWS???
% multi-step prediction knowing input and initial condition of output. The
% input is taken as the one used for training the network.
% The output T2{1} = T{1}, T2{i>1}=0;
T2=T;
for i=2:length(T)
T2{i}=0;
end
[xc,xic,aic,tc] = preparets(netc,X,{},T2);
yc = netc(xc,xic,aic);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%PLOT FIGURE
%%compare actual output vs. multi-step prediction knowing input and initial condition of output
figure();
plot(cell2mat(t),'-r','linewidth',2);
hold on;
plot(cell2mat(yc),'-.b','linewidth',1.5);
legend('actual output','multi-step prediction knowing input and initial condition of output');
xlabel('time step')
ylabel('output');

Best Answer

SURPRISE!!! ... IT TURNS OUT THAT GOOD CL PREDICTION IS OBTAINABLE WITH JUST THE INPUT AND ZERO INITIAL CONDITIONS! THE ONLY DIFFERENCE IS THE TIME IT TAKES FOR THE ERROR TO BECOME NEGLIGIBLE.
close all, clear all, clc
[ X, T ] = simplenarx_dataset;
neto = narxnet;
[ Xo, Xoi, Aoi, To ] = preparets( neto, X, {},T );
to = cell2mat(To);
varto = var(to,1) % 0.099154
[neto tro Yo Eo Xof Aof ] = train(neto,Xo,To,Xoi,Aoi );
view(neto)
NMSEo = mse(Eo)/varto % 2.2159e-08
netc = closeloop(neto);
T2 = T; T2(1:end) = {0};
[ Xc, Xci, Aci, Tc ] = preparets( netc, X, {}, T2 );
whos Xc Xci Aci Tc
% Aci 2x2 624 cell
% Tc 1x98 11760 cell
% Xc 1x98 11760 cell
% Xci 1x2 240 cell
[ Yc Xcf Acf ] = netc(Xc, Xci, Aci );
yc = cell2mat(Yc);
nerrc = (to-yc)/sqrt(varto);
figure;
subplot(211), hold on;
plot( to, 'b', 'linewidth', 3);
plot( yc, 'r--', 'linewidth',2);
legend( 'TARGET', 'ZERO-IC PREDICTIONS' )
xlabel( 'TIME' )
ylabel( 'OUTPUT' );
title(' ZERO INITIAL CONDITION PREDICTIONS')
subplot(212)
plot(nerrc, 'k' ,'linewidth',3);
legend( ' ZERO-IC PREDICTION ERROR ' )
xlabel( ' TIME ' )
ylabel('NORMALIZED ERROR');
Hope this helps.
Thank you for formally accepting my answer
Greg