function [A,B,D,K] = sample(A,B,C,D,K,T,inters)
%SAMPLE  xxx

%   Copyright 1986-2000 The MathWorks, Inc.
%   $Revision: 1.6 $ $Date: 2000/08/24 12:53:41 $

[nx,nu]=size(B);
ny=size(D,1);
switch lower(inters(1))
case 'z'
   s = expm([[A [B K]]*T; zeros(nu+ny,nx+nu+ny)]);
   A = s(1:nx,1:nx);
   B = s(1:nx,nx+1:nx+nu);
   K = s(1:nx,nx+nu+1:nx+nu+ny);
case 'f'
   
   nuy = nu+ny;
   s = expm([[A [B K] zeros(nx,nuy)]*T;[zeros(nuy,nx+nuy),T*eye(nuy)];...
         zeros(nu+ny,nx+2*nuy)]);
   A = s(1:nx,1:nx);
   gtil = s(1:nx,nx+nuy+1:nx+2*nuy);
   BK = s(1:nx,nx+1:nx+nuy)+(A-eye(nx))*gtil/T;
   B = BK(:,1:nu); K = BK(:,nu+1:nuy);
   D = D+C*gtil(:,1:nu)/T;
otherwise
   error('Only ''Zero-order'' hold and ''First-order-hold'' sampling is supported')
end


