文章目录

  • (〇) 前言
  • (一) S函数的结构及编辑
  • (二)S函数的应用:
    • (1)定义S函数
      • ①初始化主函数
      • ②初始化子函数
    • (2)Simulink模型中使用S函数
  • (三)特别注意*——解决初学者第一次应用S函数常碰到的错误
  • (四)常见错误

(〇) 前言

S函数是什么
    S函数是系统函数的英文缩写,指采用一种设计语言(非图形方式)去描述的一个功能模块
如何编写S函数
    可以用Matlab的语言,也可以用其它编程语言入C,C++等
    我只介绍下常用的MATLAB自带“语言”和S函数模板去编写S函数

后文的源代码
https://download.csdn.net/download/Nirvana_Tai/12321280
https://download.csdn.net/download/Nirvana_Tai/12321288

(一) S函数的结构及编辑

①在命令行窗口输入命令: >>edit sfuntmpl.m
    即可打开模板文件

②模板文件sfuntmpl.m包含了一个主函数和六个子函数

【1】我们先看主函数
  主函数首句——引导语句为:
function [sys,x0,str,ts,simStateCompliance] = sfuntmpl(t,x,u,flag)
     其中,fname是S函数的函数名;
     输入形参t,x,y,flag分别为仿真时间,状态向量,输入向量和调用标志
     输出形参sys表示返回参数;x0是初始状态值;str被设置为空阵;ts是一个两列矩阵,一列为各状态变量的采样周期,另一列是相应采样时间的偏移量。
     【M文件中S函数是这么设置的】

   【2】再看看**子函数**:

子函数的前缀为mdl,由flag来控制调用情况
常用的四种情况:
   Flag=0:调用初始化子函数 mdlInitializeSizes;
   Flag=1:调用连续状态更新子函数 mdlDerivatives(t,x,u);
   Flag=2: 调用离散状态更新子函数 mdlUpdate(t,x,u);
   Flag=3: 调用输出子函数 mdlOutputs(t,x,u);

(二)S函数的应用:

举个最简单的栗子:y=kx+b

(1)定义S函数

①初始化主函数

function [sys,x0,str,ts,simStateCompliance] = move(t,x,u,flag)
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);otherwiseDAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end

②初始化子函数

function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates  = 1;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 1;
sizes.NumInputs      = 1;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;   % at least one sample time is needed
sys = simsizes(sizes);
x0  = [0];
str = [];
ts  = [0 0];
simStateCompliance = 'UnknownSimState';
function sys=mdlDerivatives(t,x,u)
sys = u;
function sys=mdlUpdate(t,x,u)
sys=[];
function sys=mdlOutputs(t,x,u)
sys=x;
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1;    %  Example, set the next hit to be one second later.
sys = t + sampleTime;
function sys=mdlTerminate(t,x,u)
sys = [];

(2)Simulink模型中使用S函数

首先我们需要用模块搭建出这个模型
    新手可能找不到器件,这里我简单说一下:simulink标准模块库中,Sinks下有Scope,Sources下有Sine Wave,User-Defined Functions下有S-Function.连起来就可以了
    这里再说一下如何修改Scope的输入端子数:双击打开Scope界面,点击左上方设置,修改Input参数就可以了,记得修改完点Apply

建立完模型如下:

得到结果如下:

    采样时间:对于Simulink模型来说,解算器中的一个步长决定了整个模型最小的采样时间间隔。

(三)特别注意*——解决初学者第一次应用S函数常碰到的错误

第一次应用S函数时肯能会碰到很多问题,我就几个常见问题总结接个需要注意的地方,希望大家能一次写对。

①注意此处 S函数名一定要和编写的.m文件,否则会报错
    ②注意,一定要把S函数的.m文件和模型的.slx文件放在一个目录下,确保点击Edit时,可以找到并打开对应S函数M文件

    ③注意,要把matlab的当前工作目录修改为你这两个文件保存的目录下

(四)常见错误

①步长错误

Starting build procedure for model: test Build procedure for model:
‘test’ aborted due to an error. The specified code generation target
for model ‘test’ cannot be used with a variable-step solver.
Suggested Actions You may configure the solver options for a
fixed-step solver with an appropriate integration algorithm Select a
target that supports a variable-step solver, such as rsim.tlc You can
use ‘Embedded Coder Quick Start’ to help you generate code

遇到这种情况,在模型编辑窗口点击设置,修改步长设置即可

②类似于此,请检查模型的搭建是否符合S函数的逻辑,检查输入输出端。看S函数源代码是否正确
再查看目录是否一致,工作区是否在当前目录

Generating code into build folder: D:\Study Playground\sample_grt_rtw
Unconnected input line found on ‘sample/Scope’ (input port: 1)
Component: Simulink | Category: Block warning Unconnected output line
found on ‘sample/Sine Wave’ (output port: 1) Component: Simulink |
Category: Block warning Invoking Target Language Compiler on
sample.rtw Using System Target File: D:\Baidu network disk
download\Matlab\rtw\c\grt\grt.tlc Loading TLC function libraries
Initial pass through model to cache user defined code Caching model
source code Writing header file sample.h Writing header file
sample_types.h Writing header file rtwtypes.h Writing header file
builtin_typeid_types.h Writing header file multiword_types.h Writing
source file sample.c Writing header file sample_private.h Writing
header file rtmodel.h Writing source file sample_data.c Writing header
file rt_nonfinite.h Writing source file rt_nonfinite.c Writing header
file rtGetInf.h Writing source file rtGetInf.c Writing header file
rtGetNaN.h Writing source file rtGetNaN.c TLC code generation
complete. Using toolchain: LCC-win64 v2.4.1 | gmake (64-bit Windows)
‘D:\Study Playground\sample_grt_rtw\sample.mk’ is up to date Building
‘sample’: D:\Baidu network disk download\Matlab\bin\win64\gmake -f
sample.mk all D:\Study Playground\sample_grt_rtw>set MATLAB=D:\Baidu
network disk download\Matlab D:\Study Playground\sample_grt_rtw>cd .
D:\Study Playground\sample_grt_rtw>if “” == “” (D:\Baidu network disk
download\Matlab\bin\win64\gmake -f sample.mk all ) else (D:\Baidu
network disk download\Matlab\bin\win64\gmake -f sample.mk )
‘D:\Baidu’ 不是内部或外部命令,也不是可运行的程序 或批处理文件。 D:\Study
Playground\sample_grt_rtw>echo The make command returned an error of
9009 The make command returned an error of 9009 D:\Study
Playground\sample_grt_rtw>An_error_occurred_during_the_call_to_make
‘An_error_occurred_during_the_call_to_make’ 不是内部或外部命令,也不是可运行的程序
或批处理文件。 Build procedure for model: ‘sample’ aborted due to an error.
Error(s) encountered while building “sample”: Failed to generate all
binary outputs.

这样基本上就可以保证运行,如果有其他问题,请自行查询或者评论错误截图。

Matlab·Simulink的使用—【S函数的创建与应用】相关推荐

  1. MATLAB/Simulink中的S函数报错

    关于MATLAB/Simulink中的S函数报错: Output returned by S-function 'xxx' in 'xxx' during flag=3 call must be a ...

  2. matlab/simulink中自定义m-s函数作为simulink模块使用实例

    一般而言matlab/simulink能满足多数使用要求,但是往往在研究中会使用User-difinition s-function或者调用c,c++的库函数,这时候就需要matlab能进行c,c++ ...

  3. Matlab/Simulink中查表函数的应用

    1.1-D Lookup Table 模块 1-D Lookup Table 模块是最简单的查表模块 所谓查表,就是说目标为一个填满数据的表格或向量(1维)或矩阵(多维),根据对应维数的输人能够在表中 ...

  4. Matlab/Simulink中的S函数模块嵌入人工智能、神经网络算法设计仿真案例详解(以基于RBF神经网络算法的VSG转动惯量自调节为例)

    参考文献 An improved virtual synchronous generator power control strategy  Deep reinforcement learning b ...

  5. MATLAB Simulink中自定义函数和switch case的用法

    文章目录 1 Simulink自定义函数MATLAB Function 2 Simulink中Switch Case模块的用法 1 Simulink自定义函数MATLAB Function 首先写一个 ...

  6. 在MATLAB中采用M文件实现对Simulink中的S函数程序实现自动调参数

    在做研究的时候我们经常需要对模型的参数就行相应的选择,然而有没有觉得每次更改一个参数都需要运行一次仿真程序觉得很无聊呀,运行完程序还要看效果怎么样,然后再根据效果来调整参数,再次运行程序,如此反复. ...

  7. Matlab simulink中找不到s函数

    Matlab simulink中找不到s函数 问题 Error in S-function 'benchmark/Bioreactor_4/Bioreactor_4': S-Function 'asm ...

  8. Matlab——Simulink输出的数据怎么利用plot函数绘图

    Matlab--Simulink输出的数据怎么利用plot函数绘图 1.将需要导出的数据添加至To Workspace模块. 2.设置To Workspace模块的名称并配置好模式,一般选择array ...

  9. 创建内联函数matlab,浅析MATLAB中的内联函数、匿名函数和函数函数

    原创,转载请注明出处--(不注明也拿你没办法) 内联函数 内联(inline)函数是MATLAB 7以前经常使用的一种构造函数对象的方法.在命令窗口.程序或函数中创建局部函数时,通过使用inline构 ...

  10. matlab simulink实例,simulink实例(有好多实例)..ppt

    * 基于MATLAB/SIMULINK的系统建模与仿真 Simulink仿真实例 例题9,重置弹力球.一个弹力球以15m/s的速度从距水平设置10m的高度抛向空中,球的弹力为0.8,当球到达球面时,重 ...

最新文章

  1. w ndows连接USB不正常,Raspberry Pi Zero W 连接电脑 – 针对Windows 10 缺少RNDIS驱动
  2. Java在Quant应用_java – 如何绘制quantil band(在R中)
  3. Facebook失误泄露反恐审查员信息 生命或受威胁
  4. 4个做管理后才知道的秘密
  5. 1.1 《硬啃设计模式》 第1章 大话设计模式
  6. Norse Attack Map网络攻击实时监测地图
  7. 状态输出导航栏html,Vue实现导航栏效果(选中状态刷新不消失)_百厌_前端开发者...
  8. expsky.php,Typecho漏洞利用工具首发,半分钟完成渗透
  9. 如何使用和自定义Win11快速设置菜单
  10. 12 种经典亿级流量架构之资源隔离思想与方法论
  11. appium ios 下拉刷新输入框输入后收起键盘
  12. 树莓派系列(十五):使用英特尔神经计算棒2(NCS2)
  13. CST仿真指导 | 设置基本单位Units
  14. C/C++ DLL封装及调用
  15. Spherical Harmonics Lighting代码实现(续)
  16. python实现栈的操作_python简单实现队列和栈push、pop操作
  17. 女人拉屎故事_一个敏锐的女性下午的故事
  18. 【UOS】如何用Ventoy工具制作UOS启动盘操作
  19. 张量网络系列(一 从张量到张量网络)
  20. Google分析统计

热门文章

  1. kis商贸系列加密服务器,金蝶KIS商贸系列V3.0发版说明教程.doc
  2. oracle11数据库下载地址并安装使用
  3. 免登陆Oracle下载jdk
  4. 如何在arcmap中使用取色器
  5. 动易php,动易CMS数据转成dedecms的php程序
  6. 网页设计html图片滚动特效,网页设计全屏滚动效果怎么做?
  7. 调用发票管理系统的方法2
  8. 毕业设计html旅游网站,毕业设计--旅游网站的设计与实现(论文)
  9. SIR模型的应用(2) - Influence maximization in social networks based on TOPSIS(3)
  10. java中的关键字有哪些_java关键字有哪些?java关键字大全