function [sys,x0,str,ts,simStateCompliance] = sfuntmpl(t,x,u,flag)
%SFUNTMPL General MATLAB S-Function Template
% With MATLAB S-functions, you can define you own ordinary differential
% equations (ODEs), discrete system equations, and/or just about
% any type of algorithm to be used within a Simulink block diagram.
%
% The general form of an MATLAB S-function syntax is:
% [SYS,X0,STR,TS,SIMSTATECOMPLIANCE] = SFUNC(T,X,U,FLAG,P1,...,Pn)
%
% What is returned by SFUNC at a given point in time, T, depends on the
% value of the FLAG, the current state vector, X, and the current
% input vector, U.
SFUNTMPL matlab一般 s 函数模板:
通过 matlab 的 s 函数,你可以定义自己的普通微分方程(ODES)、离散系统方程、以及在 simulink 框图中可使用的任意类型的算法。
matlab 的 s 函数一般句法形式是:
[SYS,X0,STR,TS,SIMSTATECOMPLIANCE] = SFUNC(T,X,U,FLAG,P1,...,Pn)
matlab 在给定时间点返回的内容,取决于 FLAG 的值、当前状态变量 x、和当前输入向量 u。
% FLAG RESULT DESCRIPTION
% ----- ------ --------------------------------------------
% 0 [SIZES,X0,STR,TS] Initialization, return system sizes in SYS,
% initial state in X0, state ordering strings
% in STR, and sample times in TS.
% 1 DX Return continuous state derivatives in SYS.
% 2 DS Update discrete states SYS = X(n+1)
% 3 Y Return outputs in SYS.
% 4 TNEXT Return next time hit for variable step sample
% time in SYS.
% 5 Reserved for future (root finding).
% 9 [] Termination, perform any cleanup SYS=[].
- FLAG == 0 结果返回: [SIZES, X0, STR, TS]
初始化,在 SYS 中返回系统大小,初始化状态变量 x0,状态排列字符串 STR,和采样时间 TS。 - FLAG == 1 结果返回: DX
在 SYS 中返回连续状态微分 - FLAG == 2 结果返回: DS
更新离散状态 SYS = X(n + 1) - FLAG == 3 结果返回: Y
在 SYS 中返回输出变量 - FLAG == 4 结果返回: TNEXT
在 SYS 中返回下一次采样时间点的变量值 - FLAG == 5 结果返回:
参数保留 - FLAG == 9 结果返回: [ ]
程序终止,变现为 SYS 为情况状态 SYS = [ ]
% The state vectors, X and X0 consists of continuous states followed
% by discrete states.
%
% Optional parameters, P1,...,Pn can be provided to the S-function and
% used during any FLAG operation.
状态变量X、X0 由连续状态和离散状态组成。
s 函数可以由多个可变参数 P1,…,Pn,组成,并在任意 FLAG 中进行操作。
% When SFUNC is called with FLAG = 0, the following information
% should be returned:
%
% SYS(1) = Number of continuous states.
% SYS(2) = Number of discrete states.
% SYS(3) = Number of outputs.
% SYS(4) = Number of inputs.
% Any of the first four elements in SYS can be specified
% as -1 indicating that they are dynamically sized. The
% actual length for all other flags will be equal to the
% length of the input, U.
% SYS(5) = Reserved for root finding. Must be zero.
% SYS(6) = Direct feedthrough flag (1=yes, 0=no). The s-function
% has direct feedthrough if U is used during the FLAG=3
% call. Setting this to 0 is akin to making a promise that
% U will not be used during FLAG=3. If you break the promise
% then unpredictable results will occur.
% SYS(7) = Number of sample times. This is the number of rows in TS.
%
%
% X0 = Initial state conditions or [] if no states.
%
% STR = State ordering strings which is generally specified as [].
%
% TS = An m-by-2 matrix containing the sample time
% (period, offset) information. Where m = number of sample
% times. The ordering of the sample times must be:
%
% TS = [0 0, : Continuous sample time.
% 0 1, : Continuous, but fixed in minor step
% sample time.
% PERIOD OFFSET, : Discrete sample time where
% PERIOD > 0 & OFFSET < PERIOD.
% -2 0]; : Variable step discrete sample time
% where FLAG=4 is used to get time of
% next hit.
%
% There can be more than one sample time providing
% they are ordered such that they are monotonically
% increasing. Only the needed sample times should be
% specified in TS. When specifying more than one
% sample time, you must check for sample hits explicitly by
% seeing if
% abs(round((T-OFFSET)/PERIOD) - (T-OFFSET)/PERIOD)
% is within a specified tolerance, generally 1e-8. This
% tolerance is dependent upon your model's sampling times
% and simulation time.
%
% You can also specify that the sample time of the S-function
% is inherited from the driving block. For functions which
% change during minor steps, this is done by
% specifying SYS(7) = 1 and TS = [-1 0]. For functions which
% are held during minor steps, this is done by specifying
% SYS(7) = 1 and TS = [-1 1].
%
% SIMSTATECOMPLIANCE = Specifices how to handle this block when saving and
% restoring the complete simulation state of the
% model. The allowed values are: 'DefaultSimState',
% 'HasNoSimState' or 'DisallowSimState'. If this value
% is not speficified, then the block's compliance with
% simState feature is set to 'UknownSimState'.
% Copyright 1990-2010 The MathWorks, Inc.
当 FALG = 0,SFUNC 执行时,下面的信息将被返回:
- SYS(1) 连续状态变量的数量
- SYS(2) 离散状态变量的数量
- SYS(3) 输出变量的数量
- SYS(4) 输入变量的数量
SYS的前四个元素中的任何一个变量,都可以指定为-1,表示它们是动态调整大小的。其他 flags 的真实长度将会等于 u 的长度。 - SYS(5) 保留参数,一定要为 0
- SYS(6) 直接贯穿标志位(1 = yes, 0 = no)
在 FLAG =3 时,如果 U 被用到,则 S 函数是 直接贯穿的。将 SYS(6) 设置为 0 ,类似于保证 U 在 FLAG = 3 的操作中不会被用到,如果 你打破二楼这个保证,则会发生未知错误。 - sys(7) 采样时间,这是 TS 的行数。
- X0 状态变量的初始值,或者为空
- STR 一般设置为空
10.TS 包含采样时间(周期,偏移)信息的m×2矩阵。其中m =采样次数。 采样时间的排序必须是:
% TS = [0 0, : Continuous sample time.
% 0 1, : Continuous, but fixed in minor step
% sample time.
% PERIOD OFFSET, : Discrete sample time where
% PERIOD > 0 & OFFSET < PERIOD.
% -2 0]; : Variable step discrete sample time
% where FLAG=4 is used to get time of
% next hit.