当前位置:网站首页>Matlab S-function详解
Matlab S-function详解
2022-07-27 10:59:00 【char~lie】
使用MATLAB的S-function,可以在simulink里使用S-function模块,然后里面是调用一个m代码文件,可以任意命名。
MATLAB给的模板文件叫sfuntmpl.m,一般可以用edit sfuntmpl打开,不行的话打开安装目录\toolbox\simulink\blocks里面就有sfuntmpl.m这个文件。
如果你懒得打开,可以复制下面的代码,注释是原来英文的翻译加上我的解释,代码都是原先的。
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.
%
% MATLAB S-function 的语法是:
% [SYS,X0,STR,TS,SIMSTATECOMPLIANCE] = SFUNC(T,X,U,FLAG,P1,...,Pn)
%
% SFUNC在给定的时间点T的返回值取决于FLAG的值,当下的状态向量, X
% 和当下的输入,U
%
% FLAG RESULT 描述
% ----- ------ --------------------------------------------
% 0 [SIZES,X0,STR,TS] 初始化,包含系统的各种Size的信息在SYS里
% 初始状态信息在X0里,状态的命令在STR里
% 采样时间在TS里.
% 1 DX 返回SYS里的连续状态变量微分
% 2 DS 更新离散的状态SYS = X(n+1)
% 3 Y 返回SYS的输出.
% 4 TNEXT 根据不同采样时间返回下一个系统的时间点
%
% 5 保留未定义 (root finding).
% 9 [] 终止, 清理现场SYS=[].
%
%
% 状态向量 X 和 X0 包括连续状态变量和离散状态变量
%
% 可选参数, P1,...,Pn 可以供 S-function 使用and
% 也可以在任意FLAG条件下使用
%
% 当 SFUNC 在FLAG = 0情况调用, 函数要输出以下信息,也就是你写程序要输入的信息:
%
% SYS(1) = 连续状态变量个数
% SYS(2) = 离散状态变量个数
% SYS(3) = 输出变量个数
% SYS(4) = 输入U的个数
% 这前四个任意一个都可以被定义为-1,-1就意味着他们的维度是动态的
% 在其它FLAG的值里调用的时候它的长度就等于输入U的维度
% SYS(5) = 为 root finding(可能和系统零极点有关)保留. 定义为0.
% SYS(6) = 直接转移标志 flag (1=yes, 0=no). 如果 s-function
% 在 FLAG=3 调用过程中有直接转移项会用到. 把这项置零那么在FLAG=3的过程中不会有U,
% 如果你不遵守这条,置零还用了U,结果将不符预期。
% SYS(7) = 采样时间的个数. 代表TS里的行数。
%
%
% X0 = 初始状态 或者 [] 代表没有状态变量.
%
% STR = 状态命令字符,通常定义为 [].
%
% TS = m-乘-2 矩阵 包含采样时间(周期, 偏移量) m=采样时间个数
% TS的格式一定是:
%
% TS = [0 0, : 连续采样时间
% 0 1, : 连续,但是在某个步骤修改的采样时间
%
% 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.
%
% 采样时间可以不止一个,他们可以单调递增。
% 当你使用超过1个采样时间时,你要确定
% abs(round((T-OFFSET)/PERIOD) - (T-OFFSET)/PERIOD)
% 小于容差,一般是 1e-8. 容差大小取决于采样和仿真次数。
%
%
% 你也可以通过继承驱动模块里的采样时间。
% 对于在小步骤里改变的采样时间,这个操作要
% 定义 SYS(7) = 1 和 TS = [-1 0].
% 对于在小步骤里保持的采样时间,定义
% SYS(7) = 1 和TS = [-1 1].
%
% SIMSTATECOMPLIANCE = 声明如何存储整个仿真的状态
%
% 它的值可以是: 'DefaultSimState',
% 'HasNoSimState' or 'DisallowSimState'. 如果没声明,它就会被置为'UknownSimState'.
% Copyright 1990-2010 The MathWorks, Inc.
%
% The following outlines the general structure of an S-function.
%
switch flag,
%%%%%%%%%%%%%%%%%%
% 初始化 %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;
%%%%%%%%%%%%%%%
% 微分 %
%%%%%%%%%%%%%%%
case 1,
sys=mdlDerivatives(t,x,u);
%%%%%%%%%%
% 更新 %
%%%%%%%%%%
case 2,
sys=mdlUpdate(t,x,u);
%%%%%%%%%%%
% 输出 %
%%%%%%%%%%%
case 3,
sys=mdlOutputs(t,x,u);
%%%%%%%%%%%%%%%%%%%%%%%
% 获得下个时间点 %
%%%%%%%%%%%%%%%%%%%%%%%
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
%%%%%%%%%%%%%
% 终止 %
%%%%%%%%%%%%%
case 9,
sys=mdlTerminate(t,x,u);
%%%%%%%%%%%%%%%%%%%%
% 预期之外的 flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
% end sfuntmpl
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes
%
% call simsizes for a sizes structure, fill it in and convert it to a
% sizes array.
%
% Note that in this example, the values are hard coded. This is not a
% recommended practice as the characteristics of the block are typically
% defined by the S-function parameters.
%这里定义输入变量个数,输出变量个数,直接输出与否,采样时间个数
sizes = simsizes;
sizes.NumContStates = 0;%连续状态变量个数
sizes.NumDiscStates = 0;%离散状态变量个数
sizes.NumOutputs = 0;%输出状态变量个数
sizes.NumInputs = 0;%输入状态变量个数
sizes.DirFeedthrough = 1;%直接输出与否
sizes.NumSampleTimes = 1; % 至少一个采样时间
sys = simsizes(sizes);
%
% initialize the initial conditions
%
x0 = [];%这里定义变量初始值
%
% str is always an empty matrix
%
str = [];
%
% initialize the array of sample times
%
ts = [0 0];%见上面对ts的说明
% Specify the block simStateCompliance. The allowed values are:
% 'UnknownSimState', < 默认设置
% 'DefaultSimState', < 和built-in block一样
% 'HasNoSimState', < 没有仿真状态
% 'DisallowSimState' < 储存状态出错
simStateCompliance = 'UnknownSimState';
% end mdlInitializeSizes
%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%
function sys=mdlDerivatives(t,x,u)
sys = [];%这里写系统微分方程,比如sys = [0 0 1 0;0 0 0 1;-(K+K2)/M K2/M -B/M 0;K2/m2 -K2/m2 0 0]*x+[0;0;1/M;0]*u;
% end mdlDerivatives
%
%=============================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=============================================================================
%
function sys=mdlUpdate(t,x,u)
sys = [];
% end mdlUpdate
%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(t,x,u)
sys = [];%系统输出,比如sys=x(1);输出第一个变量
% end mdlOutputs
%
%=============================================================================
% mdlGetTimeOfNextVarHit
% Return the time of the next hit for this block. Note that the result is
% absolute time. Note that this function is only used when you specify a
% variable discrete-time sample time [-2 0] in the sample time array in
% mdlInitializeSizes.
%=============================================================================
%
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1; % Example, set the next hit to be one second later.
sys = t + sampleTime;
% end mdlGetTimeOfNextVarHit
%
%=============================================================================
% mdlTerminate
% Perform any end of simulation tasks.
%=============================================================================
%
function sys=mdlTerminate(t,x,u)
sys = [];
% end mdlTerminate
边栏推荐
- C programming language (2nd Edition) -- Reading Notes -- 1.3
- 高斯消元 AcWing 883. 高斯消元解线性方程组
- 求组合数 AcWing 887. 求组合数 III
- Caused by:org.gradle.api.internal. plugins . PluginApplicationException: Failed to apply plugin
- 为什么选择智能电视?
- Local virtual machine initialization script
- Win10 vscode code code format setting and remote breakpoint debugging
- 容斥原理 AcWing 890. 能被整除的数
- 什么是私域流量?
- JUC框架 从Runnable到Callable到FutureTask 使用浅析
猜你喜欢

Gaussian elimination acwing 883. solving linear equations with Gaussian elimination

Redis simple to use

记忆化搜索 AcWing 901. 滑雪

第10章 枚举类与注解

第13章 IO流

Win10 vscode code code format setting and remote breakpoint debugging

C# 自定义集合

为什么选择智能电视?

第12章 泛型

Find the combination number acwing 885. find the combination number I
随机推荐
[shader realizes shake random shaking effect _shader effect Chapter 10]
为什么选择智能电视?
(8) Shell function
剑指 Offer 笔记: T57 - I. 和为 s 的两个数字
Gaussian elimination acwing 883. solving linear equations with Gaussian elimination
Why choose smart TV?
zabbix自定义监控项
第7章 异常处理
状态压缩DP AcWing 91. 最短Hamilton路径
Knapsack problem acwing 9. grouping knapsack problem
LAN SDN technology hard core insider 13 from LAN to Internet
Gaussian elimination acwing 884. Gaussian elimination for solving XOR linear equations
数据包传输:应用层-内核-硬件
Lazy loading of lists and pictures
Wilcoxon rank-sum 和 signed-rank
C programming language (2nd Edition) -- Reading Notes -- 1.5.1
SQL statement learning and the use of pymysql
LAN SDN hard core technology insider 24 outlook for the future - RDMA (middle)
多种进制之间的转换
Stm32f10x -- C Language-1