function [sys,x0,str,ts] = stdiscon(t,x,u,flag,tsamp)
  switch flag,
  case 0,
    [sys,x0,str,ts]=mdlInitializeSizes(tsamp);
  case 2,
    sys=mdlUpdate(t,x,u);
  case 3,
    sys=mdlOutputs(t,x,u);
  case 9,
    sys=mdlTerminate(t,x,u);
  otherwise
    error(['Unhandled flag = ',num2str(flag)]);
end

function [sys,x0,str,ts]=mdlInitializeSizes(tsamp)
  sizes = simsizes;
  sizes.NumContStates  = 0;
  sizes.NumDiscStates  = 0;
  sizes.NumOutputs     = 0;
  sizes.NumInputs      = 5;
  sizes.DirFeedthrough = 1;
  sizes.NumSampleTimes = 1;
  sys = simsizes(sizes);
  x0  = [];
  str = [];
  ts  = [tsamp 0];
  % Controller initialisation
  Denominator = '[1 .7]';
  Numerator = '[1 .4 .2]';
  set_param('ad031s/Controller', 'Numerator', Numerator, ...
                                 'Denominator', Denominator);

function sys=mdlOutputs(t,x,u)
  if t>4  % begin adaptive control
    b = [1 zi zi^2] * [0; u(1:2)];
    a = [1 zi zi^2] * u(3:5);
    a = a * (1-zi); % add integrator
    [x0,y0]=axbyc(a,b,1);
    tt = 0;
    xx = x0 - b * tt;
    yy = y0 + a * tt;
    Denominator = sprintf('[%f %f]',xx{0},xx{1});
    Numerator = sprintf('[%f %f %f]',yy{0},yy{1}, yy{2});
    set_param('ad031s/Controller', 'Numerator', Numerator, ...
                                   'Denominator', Denominator);
  end
  sys=[];

function sys=mdlUpdate(t,x,u)
  sys = [];

function sys=mdlTerminate(t,x,u)
  sys = [];
