No BSD License  

Highlights from
Advanced Control with MATLAB and Simulink

image thumbnail

Advanced Control with MATLAB and Simulink

by

 

20 Aug 2002 (Updated )

Companion Software

NEMOL(t,x,u,flag,nA,nB,k,nun,lr,offset,ts);
function [sys, x0] = NEMOL(t,x,u,flag,nA,nB,k,nun,lr,offset,ts);
%S-function for on-line identification of neural model. The model is of
%the form of static feed-forward linear network with one hidden layer
%sec. 4.8.5.
%
%   Parameters of S-function are:
%
%      nA  : number of input neurons corresponding to y(i-1),y(i-2)...y(i-nA);
%      nB  : number of input neurons corresponding to u(i-1-k),u(i-2-k)...u(i-nA-k);
%      k   : additional delay time of the model;
%      nun : number of neurons in the hidden layer;
%      lr  : learning rate.
%

%---------------------------------------------------------------
% copyright 1995, by J. Moscinski and Z. Ogonowski. For use with 
% the book "Advanced Control with MATLAB and SIMULINK"
% (Prentice-Hall, 1995).
%---------------------------------------------------------------


n = nA+nB+k+1; 

if flag == 0
  
   sys = [0,n+(n+3)*nun+3,1,2,0,0];

   x0  = zeros(n+(n+3)*nun+3,1);
   if exist('W10') == 1 
     x0(n+1:n*(nun+1)) = W10(:);
   else
     x0(n+1:n*(nun+1)) = rand(n*nun,1)*0.5;
   end;
   if exist('B10') == 1 
     x0(n*(nun+1)+1:(n+1)*nun+n) = B10;
   else
     x0(n*(nun+1)+1:(n+1)*nun+n) = rand(nun,1)*0.5;
   end;
   if exist('W20') == 1 
     x0((n+1)*nun+n+1:(n+2)*nun+n) = W20;
   else
     x0((n+1)*nun+n+1:(n+2)*nun+n) = rand(nun,1)*0.5;
   end;
   if exist('B20') == 1 
     x0((n+2)*nun+n+1) = B20;
   else
     x0((n+2)*nun+n+1) = rand(1,1)*0.5;
   end;

   W1 = zeros(nun,n);
   W1(:) = x0(n+1:n*(nun+1));
   B1 = x0(n*(nun+1)+1:(n+1)*nun+n);
   W2 = x0((n+1)*nun+n+1:(n+2)*nun+n)';
   B2 = x0((n+2)*nun+n+1);

   P = zeros(n,1);
   T = 0;
   A1 = purelin(W1*P,B1);
   A2 = purelin(W2*A1,B2);
   E = T-A2;

   x0((n+2)*nun+n+2:(n+3)*nun+n+1) = A1;
   x0((n+3)*nun+n+2) = A2;
   x0((n+3)*nun+n+3) = E;

elseif abs(flag) == 2

   if abs( round((t - offset)/ts) - (t - offset)/ts) < 1e-8

     W1 = zeros(nun,n);

     P = x(1:n);
     T = u(1);

     W1(:) = x(n+1:n*(nun+1));
     B1 = x(n*(nun+1)+1:(n+1)*nun+n);
     W2 = x((n+1)*nun+n+1:(n+2)*nun+n)';
     B2 = x((n+2)*nun+n+1);

     A1 = x((n+2)*nun+n+2:(n+3)*nun+n+1);
     A2 = x((n+3)*nun+n+2);
     E  = x((n+3)*nun+n+3);


     D2 = deltalin(A2,E);

     D1 = deltalin(A1,D2,W2);
     [dW1,dB1] = learnbp(P,D1,lr);
     [dW2,dB2] = learnbp(A1,D2,lr);
     W1 = W1 + dW1; B1 = B1 + dB1;
     W2 = W2 + dW2; B2 = B2 + dB2;


     A1 = purelin(W1*P,B1);
     A2 = purelin(W2*A1,B2);
     E = T-A2;

     sys(1:n) = [u(1);x(1:nA-1);u(2);x(nA+1:n-1)];
     sys(n+1:n*(nun+1)) = W1(:);
     sys(n*(nun+1)+1:(n+1)*nun+n) = B1;
     sys((n+1)*nun+n+1:(n+2)*nun+n) = W2';
     sys((n+2)*nun+n+1) = B2;
     sys((n+2)*nun+n+2:(n+3)*nun+n+1) = A1;
     sys((n+3)*nun+n+2) = A2;
     sys((n+3)*nun+n+3) = E;

   else
     
     sys = x;

   end;
 
elseif flag == 3

     W1 = zeros(nun,n);
     P = x(1:n);

     W1(:) = x(n+1:n*(nun+1));
     B1 = x(n*(nun+1)+1:(n+1)*nun+n);
     W2 = x((n+1)*nun+n+1:(n+2)*nun+n)';
     B2 = x((n+2)*nun+n+1);

    sys = purelin(W2*purelin(W1*P,B1),B2);

elseif flag == 4

     ns = (t - offset) / ts;
     sys = offset + (1 + floor(ns + 1e-13*(1+ns)))*ts;

else
  
     sys = [];

end

Contact us