gusucode.com > 《精通matlab 7》包括配套光盘 > 配套光盘里的程序附件/Ex-34/dsfunc.m

    function [sys,x0,str,ts] = dsfunc(t,x,u,flag)
% 定义一个离散系统的S-function 
%      x(n+1) = Ax(n) + Bu(n)
%      y(n)   = Cx(n) + Du(n)
A=[-1.3839, -0.5097; 1.0000, 0];
B=[-2.5559, 0; 0, 4.2382];
C=[0, 2.0761; 0, 7.7891];
D=[-0.8141, -2.9334; 1.2426, 0];
switch flag,
  case 0,
    [sys,x0,str,ts] = mdlInitializeSizes(A,B,C,D);
  case 2,                                                
    sys = mdlUpdate(t,x,u,A,B,C,D); 
  case 3,                                                
    sys = mdlOutputs(t,x,u,A,C,D);
  case 9,                                                
    sys = []; % do nothing
  otherwise
    error(['unhandled flag = ',num2str(flag)]);
end