Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. load iddata-02
  2.  
  3. % X - input time series.
  4. % T - feedback time series.
  5.  
  6. X = tonndata(id.u,false,false);
  7. T = tonndata(id.y,false,false);
  8.  
  9. % Choose a Training Function
  10. % 'trainbr' is best suited for difficult cases
  11. trainFcn = 'trainbr'; %Bayesian Regulation backpropagation
  12.  
  13. inputDelays = 1:7;
  14. feedbackDelays = 1:2;
  15. hiddenLayerSize = 10;
  16. net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize,'open',trainFcn);
  17. net.performFcn = 'mse';
  18. % Prepare the Data for Training and Simulation
  19. [x,xi,ai,t] = preparets(net,X,{},T);
  20.  
  21. % Setup Division of Data for Training, Validation, Testing
  22. net.divideParam.trainRatio = 100/100;
  23. net.divideParam.valRatio = 0/100;
  24. net.divideParam.testRatio = 0/100;
  25. net.trainParam.epochs = 2000
  26.  
  27. % Train the Network
  28. [net,tr] = train(net,x,t,xi,ai);
  29.  
  30. % Test the Network
  31. y = net(x,xi,ai);
  32. e = gsubtract(t,y);
  33. performance = perform(net,t,y)
  34. figure, plotperform(tr)
  35.  
  36.  
  37. %% Closed Loop Network
  38. netc = closeloop(net);
  39. [xc,xic,aic,tc] = preparets(netc,X,{},T);
  40. yc = netc(xc,xic,aic);
  41. closedLoopPerformance = perform(net,tc,yc)
  42. figure, plot(id.y(inputDelays(end:end):end),'-r')
  43. hold on
  44. plot(cell2mat(yc),'-b');
  45.  
  46. % Step-Ahead Prediction Network
  47.  
  48. nets = removedelay(net);
  49. [xs,xis,ais,ts] = preparets(nets,X,{},T);
  50. ys = nets(xs,xis,ais);
  51. stepAheadPerformance = perform(nets,ts,ys)
  52. plot(cell2mat(ys),'-y');
  53. legend('identification output','closed loop of Neural Network output', 'One-Step-Ahead Prediction output')
  54. title(['Closed Loop MSE: ' num2str(closedLoopPerformance), ' Prediction MSE: ' num2str(stepAheadPerformance)]);
  55. grid
  56. hold off
  57. shg
  58.  
  59. %% Validation set
  60.  
  61. % Xval - input time series.
  62. % Tval - feedback time series.
  63.  
  64. Xval = tonndata(val.u,false,false);
  65. Tval = tonndata(val.y,false,false);
  66.  
  67. % Closed Loop Network
  68.  
  69. netcVal = closeloop(net);
  70. [xc,xic,aic,tc] = preparets(netcVal,Xval,{},Tval);
  71. ycval = netcVal(xc,xic,aic);
  72. closedLoopPerformance = perform(netcVal,tc,ycval)
  73. figure, plot(val.y(inputDelays(end:end):end),'-r')
  74. hold on
  75. plot(cell2mat(ycval),'-b');
  76.  
  77. % Step-Ahead Prediction Network
  78.  
  79. netsVal = removedelay(net);
  80. [xs,xis,ais,ts] = preparets(netsVal,Xval,{},Tval);
  81. ysval = netsVal(xs,xis,ais);
  82. stepAheadPerformance = perform(netsVal,ts,ysval)
  83. plot(cell2mat(ysval),'-g');
  84. legend('validation output','closed loop of Neural Network output', 'One-Step-Ahead Prediction output')
  85. title(['Closed Loop MSE: ' num2str(closedLoopPerformance), ' Prediction MSE: ' num2str(stepAheadPerformance)]);
  86. grid
  87. hold off
  88. shg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement