📄 sfundsc2.m
字号:
function [sys, x0] = sfundsc2(t,x,u,flag)
%SFUNDSC2 An example S-function for discrete systems with a sample time.
% This M-file is designed to be used as a template for other
% S-functions. Right now it acts as a zero order hold. This template
% is an example of a discrete system with an explicit update time.
% That is, it makes use of FLAG==4, so the states get updated only at
% the specified time intervals.
%
% Copyright (c) 1990-94 by The MathWorks, Inc.
% Ned Gulley 8-11-92
% Uncomment the following two lines to show how the flags are called.
% dispString=['flag = ' num2str(flag) ' at time = ' num2str(t)];
% disp(dispString)
% Set the sample time to 0.1 seconds.
Tsamp = 0.1;
if abs(flag) == 2,
% The FLAG==2 branch is at the top of the file for efficiency, since
% the S-function will be called with FLAG==2 very frequently.
% If FLAG==2, then SIMULINK is looking for the next point in real time.
% **** In this template system, the current state x gets the input u ****
% Now we must explicitly test to see if this is the appropriate update
% time. If it is, update the discrete state. Otherwise, leave it at its
% current value.
% There has to be an explicit test here for the correctness of the time
% because a FLAG==2 call is made every time the clock advances, not
% simply at the time specified as TNEXT under FLAG==4.
if abs(round(t/Tsamp)-(t/Tsamp)) < 1e+8*eps,
% (SYS =) XNEW = U
sys = u;
else
% (SYS =) XNEW = X
sys = x;
end;
elseif flag == 3,
% If FLAG==3, then SIMULINK wants to know what the next output is.
% **** In this template system, y gets the current state x ****
% (SYS =) Y = X
sys = x;
elseif flag == 4,
% If FLAG==4, then SIMULINK wants to know what TNEXT is.
% TNEXT is the time when the integrator should be constrained to
% call the system with FLAG==2.
% First compute the number of samples so far
numSamp = floor(t/Tsamp + 1e+8*eps);
% Now sys returns TNEXT, the next time the discrete state should
% be updated.
% (SYS =) TNEXT = ...
sys = (numSamp + 1)*Tsamp;
elseif flag == 0,
% This part takes care of all initialization; it is used only once.
% **** In this template system, there is one discrete state, one ****
% **** input and one output. ****
% The sizes vector is six elements long, and it looks like this:
% sizes(1) = number of continuous states
sizes(1) = 0;
% sizes(2) = number of discrete states
sizes(2) = 1;
% sizes(3) = number of system outputs (length of output y)
sizes(3) = 1;
% sizes(4) = number of system inputs (length of input u)
sizes(4) = 1;
% sizes(5) = number of discontinuous roots; unused feature, set to zero
sizes(5) = 0;
% sizes(6) = direct feedthrough flag; used to detect algebraic loops.
% Set sizes(6) to 1 if the output y depends directly on the input u.
% Otherwise, it should be set to 0.
sizes(6) = 0;
% Set the initial conditions on the states
x0 = 0;
% (SYS =) SIZES = [0 1 1 1 0 0]';
sys = sizes';
else
% Flags not considered here are treated as unimportant.
% Notice that since there are no continuous states in this system,
% there is no need to deal with FLAG==1.
% Output is set to [].
sys = [];
end % if abs(flag) == ...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -