function [sys,x0,str,ts] = st2i2ocon(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      = 8;
  sizes.DirFeedthrough = 1;
  sizes.NumSampleTimes = 1;
  sys = simsizes(sizes);
  x0  = [];
  str = [];
  ts  = [tsamp 0];
  % Controller initialisation
  A='[0 0; 0 0]';
  B='[1 0; 0 1]';
  C='[1 0;0 1]';
  D='[.1 .1 ; .1 .1]';
  set_param('ad035s/Controller', 'A', A, 'B', B,  'C', C,  'D', D);

function sys=mdlOutputs(t,x,u)
  if t>4 % begin adaptive control
    al = [1+u(1)*s, u(2)*s; u(5)*s, 1+u(6)*s];
    bl = pol([u(3), u(4); u(7), u(8)]);
    [br,ar] = lmf2rmf(bl,al);
    ar = ar * s; % add integrator
    % closed-loop poles
    clpoles = [(0.7*s+1)*(1.5*s+1), 0; 0  (0.7*s+1)*(1.5*s+1)];
    [pl,ql]=xaybc(ar, br, clpoles);
    [ac,bc,cc,dc] = lmf2ss(ql,pl*s);
    A = sprintf('[%f %f; %f %f]',ac');
    B = sprintf('[%f %f; %f %f]',bc');
    C = sprintf('[%f %f; %f %f]',cc');
    D = sprintf('[%f %f; %f %f]',dc');
    set_param('ad035s/Controller', 'A', A, 'B', B,  'C', C,  'D', D);
  end
  sys=[];

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

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