Advertisement
Guest User

Untitled

a guest
May 26th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.36 KB | None | 0 0
  1. % DEMONSTRATION PROGRAM FOR TESTING NNARX
  2. %
  3. % Programmed by Magnus Norgaard, IAU/IMM/EI, Technical Univ. of Denmark
  4. % LastEditDate: Jan 15, 2000
  5.  
  6. close all
  7. StopDemo=0;
  8. figure
  9. guihand=gcf;
  10. for k=1:1, %dummy loop
  11.  
  12.   % >>>>>>>>>>>>>>>>  BUILD GUI INTERFACE  <<<<<<<<<<<<<<<<<
  13.   %[guihand,edmulti,contbut,quitbut]=pmnshow;
  14.   set(guihand,'Name','NNARX demonstration');
  15.  
  16.   % >>>>>>>>>>>>>>>>  SCREEN 1  <<<<<<<<<<<<<<<<<
  17.   s0='1';
  18.   s1='The purpose of this demo is to show how the NNARX';
  19.   s2='function can be used for modelling a second order';
  20.   s3='nonlinear dynamic system.';
  21.   s4=[];
  22.   s5='To generate the model we will use the noise corrupted';
  23.   s6='data set shown above.';
  24.   smat=str2mat(s0,s1,s2,s3,s4,s5,s6);
  25.  
  26.   %load spmdata
  27.   % ###########  ROBIMY PRO-SZTOSIWO FUNKCJE  ############# %
  28.   N = 1000;
  29.   U = rand(1,1000);
  30.   Y = zeros(1,1000);
  31.  
  32.   for k= 5:N
  33.       Y(k)=0.6*Y(k)+0.1*Y(k-1)+(U(k)^2)+0.5*U(k-1); % bez zakłóceń;
  34.       %Y(k)=0.6*Y(k)+0.1*Y(k-1)+(U^2)*(k)+0.5*U(k-1)+randn(); % z zakłóceniami
  35.  
  36.  
  37.   %ze zbiorów U i Y usówamy początkowe
  38.   U=U(11:N);
  39.   Y=Y(11:N);
  40.  
  41.   u1=U(1:floor(N-10)/2);
  42.   u2=U(ceil(((N-10)/2)+1):end);
  43.  
  44.   y1=Y(1:floor(N-10)/2);
  45.   y2=Y(ceil(((N-10)/2)+1):end);
  46.   end
  47.   % ############  KONIEC FUNKCJI  ################  %
  48.  
  49.   subplot(411)
  50.   plot(U);
  51.   title('Input and output sequence')
  52.   subplot(412)
  53.   plot(Y);
  54.   pmnshow(smat,guihand,edmulti,contbut,quitbut);
  55.   if StopDemo==1, close all, break; end
  56.  
  57.   % >>>>>>>>>>>>>>>>  SCREEN 2  <<<<<<<<<<<<<<<<<
  58.   s0='2';
  59.   s1='First we have to select a model structure inside';
  60.   s2='which we wish to search for a good model.';
  61.   s3=[];
  62.   s4='The model structure selection consists of two';
  63.   s5='subproblems: Choosing a regressor structure and';
  64.   s6='choosing a network architecture.';
  65.   smat=str2mat(s0,s1,s2,s3,s4,s5,s6);
  66.   pmnshow(smat,guihand,edmulti,contbut,quitbut);
  67.   if StopDemo==1, close all, break; end
  68.  
  69.  
  70.   % >>>>>>>>>>>>>>>>  SCREEN 3  <<<<<<<<<<<<<<<<<
  71.   subplot(411);delete(gca);subplot(412);delete(gca)
  72.   subplot('position',[0.1 0.5 0.6 0.5])
  73.   W1=rand(5,5);
  74.   W2=rand(1,6);
  75.   drawnet(W1,W2,eps,{'y(t-1)' 'y(t-2)' 'u(t-1)' 'u(t-2)'},{'yhat(t)'});
  76.   s0='3';
  77.   s1='As regressors we will use two past inputs and';
  78.   s2='two past outputs.';
  79.   s3=[];
  80.   s4='Furthermore we will choose a network architecture';
  81.   s5='with 5 hidden ''tanh'' units and one linear output unit.';
  82.   smat=str2mat(s0,s1,s2,s3,s4,s5);
  83.   pmnshow(smat,guihand,edmulti,contbut,quitbut);
  84.   if StopDemo==1, close all, break; end
  85.  
  86.  
  87.   % >>>>>>>>>>>>>>>>  SCREEN 4  <<<<<<<<<<<<<<<<<
  88.   s0='4';
  89.   s1='Now that a model structure has been selected, we are';
  90.   s2='ready to begin training.';
  91.   s3=[];
  92.   s4='Let''s run the NNARX function, which uses a Levenberg-';
  93.   s5='Marquardt algorithm for generating a NNARX-model.';
  94.   smat=str2mat(s0,s1,s2,s3,s4,s5);
  95.  
  96.   % ----- Choose training parameters -----
  97.   trparms = settrain;
  98.   trparms = settrain(trparms,'maxiter',100,'D',1e-4);
  99.   NetDef = ['HHHHH'
  100.             'L----'];
  101.   NN = [2 2 1];
  102.   pmnshow(smat,guihand,edmulti,contbut,quitbut);
  103.   if StopDemo==1, close all, break; end
  104.  
  105.  
  106.   % >>>>>>>>>>>>>>>>  SCREEN 5  <<<<<<<<<<<<<<<<<
  107.   % ----- Train network -----
  108.   s0='5';
  109.   s1=[];
  110.   s2='    >> Training process in action!! <<';
  111.   s3=[];
  112.   s4=[];
  113.   s5='We run up to 100 iterations so you may have to';
  114.   s6='wait for a while.';
  115.   smat=str2mat(s0,s1,s2,s3,s4,s5,s6);
  116.   set(edmulti,'String',smat);
  117.   drawnow
  118.   [W1,W2,NSSEvec,iteration,lambda]=nnarx(NetDef,NN,[],[],trparms,y1,u1);
  119.   delete(gca);
  120.   subplot('position',[0.1 0.55 0.45 0.38]);
  121.   semilogy(NSSEvec);
  122.   xlabel('Iteration');
  123.   ylabel('Criterion of fit');
  124.   grid
  125.  
  126.  
  127.   % >>>>>>>>>>>>>>>>  SCREEN 6  <<<<<<<<<<<<<<<<<
  128.   % ----- Evaluate network -----
  129.   s0='6';
  130.   s1='The network has now been trained and we conclude';
  131.   s2='the session by validating the model on a fresh';
  132.   s3='data set.';
  133.   smat=str2mat(s0,s1,s2,s3);
  134.   pmnshow(smat,guihand,edmulti,contbut,quitbut);
  135.   if StopDemo==1, close all, break; end
  136.   [Yhat,NSSE]=nnvalid('nnarx',NetDef,NN,W1,W2,y2,u2);
  137.  
  138.   %semilogy(NSSEvec);
  139.   %BLAD SREDNIOKWADRATOWY
  140.   %MSE=mse(Yhat-y2(10:end));
  141.   %MSE=2*NSSE;
  142.   figure(1)
  143.  
  144.   % >>>>>>>>>>>>>>>>  SCREEN 7  <<<<<<<<<<<<<<<<<
  145.   s1=[];
  146.   s2='                  >> THE END <<';
  147.   smat=str2mat(s1,s1,s1,s1,s2);
  148.   set(edmulti,'String',smat);
  149.   drawnow
  150. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement