% This function defines the system dynamics for the decentralized system

function [y] = extdec_ctrlsys(t, x, flag, SysData, fcnflag)
global tnext;

kappa = SysData.kappa;
c = SysData.c;
mu = SysData.mu;
eta = SysData.eta;

% Strip out the states
x11 = x(1); x21 = x(2);
dx = zeros(2, 1);     % pre-allocate

% Define the error system
e11 = x11; e21 = x21;

% Define the control law
phi12 = sqrt(abs(x21));
phi21 = sqrt(abs(x11));
q1 = mu*(2 + c*phi12^4);
q2 = mu*(2 + c*phi21^4);

zeta11 = x11*x11;
zeta21 = x21*x21;
z11 = zeta11/(2*sqrt(c));
z21 = zeta21/(2*sqrt(c));

nu11 = eta*z11 + q1;
nu21 = eta*z21 + q2;

u1 = -(kappa + nu11)*e11;
u2 = -(kappa + nu21)*e21;

% Define the derivative
switch (fcnflag)
case 'deriv'
  dx(1) = x11*x11*x21 + u1;
  dx(2) = x21*x21*x11 + u2;
  y = dx;

case 'output'  
  y = [x11; x21];
end

