load('date.txt') %date from inverted pendulum (first column - input, 2 and 3 - output)
inputDATE=date(:,1); %input
outputDATE=date(:,[2,3]); %output
%%from generete Simple Script:
% Solve an Autoregression Problem with External Input with a NARX Neural Network
% Script generated by NTSTOOL
% Created Wed Oct 02 22:26:03 CEST 2013
%
% This script assumes these variables are defined:
%% inputDATE - input time series.
% outputDATE - feedback time series.
inputSeries = tonndata(inputDATE,false,false);targetSeries = tonndata(outputDATE,false,false);% Create a Nonlinear Autoregressive Network with External Input
inputDelays = 1:2;feedbackDelays = 1:2;hiddenLayerSize = 10;net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize);% Prepare the Data for Training and Simulation
% The function PREPARETS prepares timeseries data for a particular network,
% shifting time by the minimum amount to fill input states and layer states.
% Using PREPARETS allows you to keep your original time series data unchanged, while
% easily customizing it for networks with differing numbers of delays, with
% open loop or closed loop feedback modes.
[inputs,inputStates,layerStates,targets] = preparets(net,inputSeries,{},targetSeries);% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;net.divideParam.valRatio = 15/100;net.divideParam.testRatio = 15/100;% 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)% View the Network
view(net)% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, plotregression(targets,outputs)
%figure, plotresponse(targets,outputs)
%figure, ploterrcorr(errors)
%figure, plotinerrcorr(inputs,errors)
% Closed Loop Network
% Use this network to do multi-step prediction.
% The function CLOSELOOP replaces the feedback input with a direct
% connection from the outout layer.
netc = closeloop(net);netc.name = [net.name ' - Closed Loop'];view(netc)[xc,xic,aic,tc] = preparets(netc,inputSeries,{},targetSeries);yc = netc(xc,xic,aic);closedLoopPerformance = perform(netc,tc,yc)% Early Prediction Network
% For some applications it helps to get the prediction a timestep early.
% The original network returns predicted y(t+1) at the same time it is given y(t+1).
% For some applications such as decision making, it would help to have predicted
% y(t+1) once y(t) is available, but before the actual y(t+1) occurs.
% The network can be made to return its output a timestep early by removing one delay
% so that its minimal tap delay is now 0 instead of 1. The new network returns the
% same outputs as the original network, but outputs are shifted left one timestep.
nets = removedelay(net);nets.name = [net.name ' - Predict One Step Ahead'];view(nets)[xs,xis,ais,ts] = preparets(nets,inputSeries,{},targetSeries);ys = nets(xs,xis,ais);earlyPredictPerformance = perform(nets,ts,ys)%%create closeloop system in simulink:
[sysNameC,netNameC] = gensim(netc,'SampleTime',0.001,'InputMode','Workspace');setsiminit(sysNameC,netNameC,netc,xic,aic,1);x1 = nndata2sim(inputSeries,1,1);x1.time=x1.time/1000; %original sample time was 0.001s
Results:
performance = 2.4806e-08 closedLoopPerformance = 8.3127 !!! <- why?earlyPredictPerformance = 2.4806e-08
I can't create nn closeloop in simulink who can model my input to output. What can I do to do this?
EDIT: I created a new data (as in the previous post: 1 column: input to the system, 2 and 3 – output) shorter and better. But it is * impossible * to create a neural network in simulink with a (closed feedback) edit: OPEN loop, which would operate in the same way as the original … Why? How to do it?
Best Answer