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

Chen_adaptive(t,x,u,flag,nunf1,nunf2,nung1,nung2,lrf,lrg,lr_inc,lr_dec,offset,ts,tl)
function [sys, x0] = Chen_adaptive(t,x,u,flag,nunf1,nunf2,nung1,nung2,lrf,lrg,lr_inc,lr_dec,offset,ts,tl)

% Program simulates adaptive Chen algorithm. It uses two hidden layers
% networks 'f' and 'g' in order to perform the model
%
%     y(i) = f(y(i-1)) + g(y(i-1))*u(i-1)
%
% Initial weight and biases xf for the network 'f' and xg for the network 'g'
% are random.
%

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


nf = 2*nunf1 + nunf1*nunf2 + 2*nunf2 + 1;
df = [nunf1,1
      nunf1,1
      nunf2,nunf1
      nunf2,1
      1,nunf2
      1,1]';

ng = 2*nung1 + nung1*nung2 + 2*nung2 + 1;

dg = [nung1,1
      nung1,1
      nung2,nung1
      nung2,1
      1,nung2
      1,1]';

fffn = 2*nf+2;
gggn = 2*ng+2;

if flag == 0

  sys = [0,fffn+gggn+5,1,2,0,0];

  x0 = zeros(fffn+gggn+5,1);
  x0(1:nf) = init_f(df);
  x0(fffn+1:fffn+ng) = init_g(dg);

  x0(fffn) = lrf;
  x0(fffn+gggn-1) = 1;
  x0(fffn+gggn) = lrg;

  x0(fffn+gggn+2) = 1e8;

elseif abs(flag) == 2

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

      % networks gradient and output calculation

      xf = x(1:nf);
      derf = x(nf+1:2*nf);
      f = x(2*nf+1);
      llrf = x(2*nf+2);

      xg = x(fffn+1:fffn+ng);
      derg = x(fffn+ng+1:fffn+2*ng);
      g = x(fffn+2*ng+1);
      llrg = x(fffn+gggn);

      SE = x(fffn+gggn+1);
      SSE = x(fffn+gggn+2);

      d_1 = x(fffn+gggn+4);
      u_1 = x(fffn+gggn+5);

      [g_new,derg_new] = net(xg,dg,u(1));
      [f_new,derf_new] = net(xf,df,u(1));

      u_ster = (u(2) - f_new)/g_new;

      % error calculation

      e = d_1 - u(1);
      SE = SE + e*e/tl;

      % learning

      gradf = e*(1/g)*derf;
      xf = xf - llrf*gradf;

      gradg = e*(1/g)*u_1*derg;
      xg = xg - llrg*gradg;

      % adaptive learning rate

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

         if SE > SSE
            llrf = llrf * lr_dec;
            llrg = llrg * lr_dec;
         else
            llrf = llrf * lr_inc;
            llrg = llrg * lr_inc;
         end
         SSE = SE;
         SE = 0;

      end

      sys(1:nf) = xf;
      sys(nf+1:2*nf) = derf_new;
      sys(2*nf+1) = f_new;
      sys(2*nf+2) = llrf;

      sys(fffn+1:fffn+ng) = xg;
      sys(fffn+ng+1:fffn+2*ng) = derg_new;
      sys(fffn+2*ng+1) = g_new;
      sys(fffn+gggn) = llrg;

      sys(fffn+gggn+1) = SE;
      sys(fffn+gggn+2) = SSE;

      sys(fffn+gggn+3) = u(1);
      sys(fffn+gggn+4) = u(2);

      sys(fffn+gggn+5) = u_ster;

  else
      sys =x;
  end

elseif flag == 3

     sys = x(fffn+gggn+5);

elseif flag == 4

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

else

     sys = [];

end

Contact us