基于S函数的simulink仿真
基于S函数的simulink仿真
S函数可以用计算机语言来描述动态系统。在控制系统设计中,S函数可以用来描述控制算法、自适应算法和模型动力学方程。
S函数中使用文本方式输入公式和方程,适合复杂动态系统的数学描述,并且在仿真过程中可以对仿真参数进行更精确的描述、
1.1 S函数简介
S函数是系统函数(system function)的简称。可以用MATLAB代码、C、C++等语言来编写S函数。
1.2 S函数的使用步骤
步骤如下:
- 创建S函数源文件
- 在动态系统的simulink模型框图中添加S-function模块,并且进行正确设置
- 在simulink模型框图中按照定义好的功能连接输入输出端口
1.3 S函数的基本功能及重要参数设定
S函数的基本功能及重要参数设定如下:
- S函数功能模块:各种功能模块完成不同的任务,这些功能模块(函数)称为仿真例程或回调函数(call - back functions),包括初始化(initialization)、导数(mdlDerivative)、输出(mdlOutput)等。
- NumContStates表示S - 函数描述的模块中连续状态的个数。
- NumDiscStates表示离散状态的个数。
- NumOutputs和NumInputs分别表示模块输出和输入的个数。
- 直接馈通(dirFeedthrough)为输入信号是否在输出端出现的标识,取值为0或1。例如,形如 y = k × u y = k×u y=k×u的系统需要输入(即直接反馈),其中, u u u是输入, k k k是增益, y y y是输出,形如等式 y = x , x ˙ = u y = x,\dot{x}=u y=x,x˙=u的系统不需要输入(即不存在直接反馈),其中, x x x是状态, u u u是输入, y y y为输出。
- NumSampleTimes为模块采样周期的个数,S函数支持多采样周期的系统。 除了sys外,还应设置系统的初始状态变量 x 0 x_0 x0、说明变量str和采样周期变量 t s t_s ts。 t s t_s ts变量为双列矩阵,其中每一行对应一个采样周期。对连续系统和单个采样周期的系统来说,该变量为 [ t 1 , t 2 ] [t_1,t_2] [t1,t2], t 1 t_1 t1为采样周期, t 1 = − 1 t_1 = - 1 t1=−1表示继承输入信号的采样周期, t 2 t_2 t2为偏移量,一般取为0。对连续系统来说, t s t_s ts取为 [ − 1 , 0 ] [-1,0] [−1,0]。
1.4 S函数描述实例
在控制系统设计中,S函数可以用于控制器、自适应律和模型描述。
以模型 J θ ¨ = u + d ( t ) J\ddot{\theta}=u+d(t) Jθ¨=u+d(t)为例,其中, u u u为控制输入, d ( t ) d(t) d(t)为加在控制输入端的扰动,模型输出为 θ 和 θ ˙ \theta和\dot{\theta} θ和θ˙,即转动角度和角速度, J J J为转动惯量,该模型可以描述如下:
x ˙ 1 = x 2 x ˙ 2 = 1 J ( u + d ( t ) ) \begin{align*} \dot{x}_1&=x_2\\ \dot{x}_2&=\frac{1}{J}(u + d(t)) \end{align*} x˙1x˙2=x2=J1(u+d(t))
其中: x 1 = θ , x 2 = θ ˙ x_1=\theta ,x_2=\dot{\theta} x1=θ,x2=θ˙
1 首先,初始化Initialization函数
采用S函数来描述动力学方程,可选取1输人2输出系统,如果角度和角速度的初始值取零,则模型初始化参数写为[0,0],模型初始化S函数描述如下:(见模板)

2 微分方程描述的mdlDerivative函数
该函数可用于描述微分方程并实现数值求解。在控制系统中,可以采样该函数来描述被控对象和自适应律等,并通过Simulink环境下选择数值分析方法来实现对模型的数值求解
取 J = 2 , d ( t ) = s i n t J=2,d(t)=sint J=2,d(t)=sint,则采用S函数可以实现模型角度和角速度的求解,描述如下:
function sys=mdlDerivatives(t,x,u)J=2;
dt=sin(t);
ut=u(1);
sys(1)=x(2);
sys(2)=1/J*(ut+dt);sys = [dx1;dx2];

3 用于输出的mdlOutput函数
S函数的mdlOutput函数通常用于描述控制器或模型的输出。采用S函数的mdlOutput模块来描述模型角度和角速度的输出:
function sys=mdlOutputs(t,x,u)sys(1) = x(1);
sys(2) = x(2);

最后,给出S函数模板
function [sys,x0,str,ts,simStateCompliance] = plant(t,x,u,flag,pa)
%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.
%
% 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=[].
%
%
% 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.
%
% 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.%
% The following outlines the general structure of an S-function.
%
switch flag,%%%%%%%%%%%%%%%%%%% Initialization %%%%%%%%%%%%%%%%%%%case 0,[sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;%%%%%%%%%%%%%%%% Derivatives %%%%%%%%%%%%%%%%case 1,sys=mdlDerivatives(t,x,u,pa);%%%%%%%%%%% Update %%%%%%%%%%%case 2,sys=mdlUpdate(t,x,u);%%%%%%%%%%%% Outputs %%%%%%%%%%%%case 3,sys=mdlOutputs(t,x,u);%%%%%%%%%%%%%%%%%%%%%%%% GetTimeOfNextVarHit %%%%%%%%%%%%%%%%%%%%%%%%case 4,sys=mdlGetTimeOfNextVarHit(t,x,u);%%%%%%%%%%%%%% Terminate %%%%%%%%%%%%%%case 9,sys=mdlTerminate(t,x,u);%%%%%%%%%%%%%%%%%%%%% Unexpected flags %%%%%%%%%%%%%%%%%%%%%otherwiseDAStudio.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 = 2;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 2;
sizes.NumInputs = 1;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1; % at least one sample time is neededsys = simsizes(sizes);%
% initialize the initial conditions
%
x0 = [0,0];%
% str is always an empty matrix
%
str = [];%
% initialize the array of sample times
%
ts = [0 0];% Specify the block simStateCompliance. The allowed values are:
% 'UnknownSimState', < The default setting; warn and assume DefaultSimState
% 'DefaultSimState', < Same sim state as a built-in block
% 'HasNoSimState', < No sim state
% 'DisallowSimState' < Error out when saving or restoring the model sim state
simStateCompliance = 'UnknownSimState';% end mdlInitializeSizes%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%
function sys=mdlDerivatives(t,x,u,pa)
k=pa.k;
m=pa.m;x1=x(1);
x2=x(2);dx1=x2;
dx2=-k/m*x1^3+u/m;sys = [dx1;dx2];% 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 = x;% 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
相关文章:
基于S函数的simulink仿真
基于S函数的simulink仿真 S函数可以用计算机语言来描述动态系统。在控制系统设计中,S函数可以用来描述控制算法、自适应算法和模型动力学方程。 S函数中使用文本方式输入公式和方程,适合复杂动态系统的数学描述,并且在仿真过程中可以对仿真…...
每日一题洛谷P8664 [蓝桥杯 2018 省 A] 付账问题c++
P8664 [蓝桥杯 2018 省 A] 付账问题 - 洛谷 (luogu.com.cn) 思路:要使方差小,那么钱不能一下付的太多,可以让钱少的全付玩,剩下还需要的钱再让钱多的付(把钱少的补上)。 将钱排序,遍历一遍&…...
迅饶科技X2Modbus网关-GetUser信息泄露漏洞
免责声明:本号提供的网络安全信息仅供参考,不构成专业建议。作者不对任何由于使用本文信息而导致的直接或间接损害承担责任。如涉及侵权,请及时与我联系,我将尽快处理并删除相关内容。 漏洞描述 该漏洞的存在是由于GetUser接口在…...
【Pandas】pandas DataFrame values
Pandas2.2 DataFrame Attributes and underlying data 方法描述DataFrame.index用于获取 DataFrame 的行索引DataFrame.columns用于获取 DataFrame 的列标签DataFrame.dtypes用于获取 DataFrame 中每一列的数据类型DataFrame.info([verbose, buf, max_cols, …])用于提供 Dat…...
蓝桥杯Java B组省赛真题高频考点近6年统计分类
基础考点 考点高频难度模拟9基础枚举5基础思维4基础动态规划3基础规律2基础单位换算2基础搜索 1基础双指针1基础数学1基础哈希表1基础暴力1基础Dijkstra1基础 二分1基础 中等考点 考点高频难度动态规划6中等数学5中等枚举4中等模拟3中等思维3中等贪心3中等前缀和3中等二分2中…...
关于inode,dentry结合软链接及硬链接的实验
一、背景 在之前的博客 缺页异常导致的iowait打印出相关文件的绝对路径-CSDN博客 里 2.2.3 一节里,我们讲到了file,fd,inode,dentry,super_block这几个概念,在这篇博客里,我们针对inode和dentr…...
PandasAI:当数据分析遇上自然语言处理
数据科学的新范式 在数据爆炸的时代,传统的数据分析工具正面临着前所未有的挑战。数据科学家们常常需要花费70%的时间在数据清洗和探索上,而真正的价值创造时间却被大幅压缩。PandasAI的出现,正在改变这一现状——它将生成式AI的强大能力注入…...
Unity网络开发基础 (3) Socket入门 TCP同步连接 与 简单封装练习
本文章不作任何商业用途 仅作学习与交流 教程来自Unity唐老狮 关于练习题部分是我观看教程之后自己实现 所以和老师写法可能不太一样 唐老师说掌握其基本思路即可,因为前端程序一般不需要去写后端逻辑 1.认识Socket的重要API Socket是什么 Socket(套接字࿰…...
做题记录:和为K的子数组
来自leetcode 560 前言 自己只会暴力,这里就是记录一下前缀和哈希表的做法,来自灵神的前缀和哈希表:从两次遍历到一次遍历,附变形题 正文 首先,这道题无法使用滑动窗口,因为滑动窗口需要满足单调性&am…...
VMware虚拟机卡顿、CPU利用率低、编译Linux内核慢,问题解决与实验对比
目录 一、总结在前面(节约时间就只看这里)0 环境说明1 遇到的问题:2 问题的原因:3 解决办法:4 实验验证:5 关于虚拟机内核数量设置6 关于强行指定Vm能用的CPU内核 二、管理员启动,实验对比实验1…...
【7】数据结构的队列篇章
目录标题 队列的定义顺序队列的实现初始化入队出队顺序队列总代码与调试 循环队列的实现初始化入队出队获取队首元素循环队列总代码与调试 链式队列的实现链式队列的初始化入队出队获取队首元素链式队列总代码与调试 队列的定义 定义:队列(Queue&#x…...
颜色归一化操作
当我们不太关注图像具体细节,只关注图像大致的内容时,为了避免光照角度、光照强度对图像的影响,可以采用下面进行归一化操作。这种颜色系统具有通道对表面方向、照明方向具有鲁棒性的特性,适用于图像分割等领域,在机器…...
2874. 有序三元组中的最大值 II
给你一个下标从 0 开始的整数数组 。nums 请你从所有满足 的下标三元组 中,找出并返回下标三元组的最大值。 如果所有满足条件的三元组的值都是负数,则返回 。i < j < k(i, j, k)0 下标三元组 的值等于 。(i, j, k)(nums[i] - nums[j]) * nums[k…...
05-Spring Security 认证与授权机制源码解析
Spring Security 认证与授权机制源码解析 结合之前的IOC、AOP、事务管理, 这一篇讲讲Spring 的安全性,以下是小弟对Spring Security的一些理解,以及在真实面试中碰到的一些问题做了些整理,欢迎各位大佬一起观摩指点!&a…...
深度学习处理文本(6)
理解词嵌入 重要的是,进行one-hot编码时,你做了一个与特征工程有关的决策。你向模型中注入了有关特征空间结构的基本假设。这个假设是:你所编码的不同词元之间是相互独立的。事实上,one-hot向量之间都是相互正交的。对于单词而言…...
STL-vector的使用
1.STL-vector 向量是可以改变其大小的线性序列容器。向量使用连续的空间存储元素,表明向量可以像数组通过下标来访问元素,但是向量的大小可以动态变化。向量的容量可能大于其元素需要的实际容量,向量通过消耗更多的内存来换取存储管理效率。…...
MySQL深入
体系结构 连接层:主要处理客户端的连接进行授权认证、校验权限等相关操作 服务层:如sql的接口、解析、优化在这里完成,所有跨存储引擎的操作在这里完成 引擎层:索引是在存储引擎层实现的,所以不同的存储引擎他的索引…...
为什么LoRA在目标检测方向不奏效?
最近在思考,为啥目标检测方向没有出现LORA的相关用法,搜索到了一篇文章,挺有深度的。 Why LoRA Struggles with Object Detection (and Why I Learned This the Hard Way) 链接:https://medium.com/predict/why-lora-struggles-with-object-detection-and-why-i-learned-…...
Vue面试常考内容[从宏观到微观]
以下是Vue面试常考内容的系统性解析,从框架设计思想到源码实现细节,结合最新技术动态(截至2025年4月)整理而成: 一、宏观层面:Vue设计哲学与框架定位 渐进式框架核心 • 分层可扩展架构:从视图层核心逐步集成路由、状态管理等能力,支持"按需取用"的渐进式开发…...
Genspark:重新定义搜索体验的AI智能体引擎
关于我们 飞书-华彬智融知识库 由前百度高管景鲲(Eric Jing)和朱凯华(Kay Zhu)联合创立的AI搜索引擎Genspark,正以革命性的技术架构和用户导向的设计理念,为全球用户带来一场搜索体验的范式革命。本文将基…...
从零实现Json-Rpc框架】- 项目实现 - 服务端主题实现及整体封装
📢博客主页:https://blog.csdn.net/2301_779549673 📢博客仓库:https://gitee.com/JohnKingW/linux_test/tree/master/lesson 📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正! &…...
AI助力PPT制作,让演示变得轻松高效
AI助力PPT制作,让演示变得轻松高效!随着科技的进步,AI技术早已渗透到各行各业,特别是在办公领域,AI制作PPT已不再是未来的梦想,而是现实的工具。以前你可能需要花费数小时来制作一个完美的PPT,如…...
React-01React创建第一个项目(npm install -g create-react-app)
1. React特点 JSX是javaScript语法的扩展,React开发不一定使用JSX。单向响应的数据流,React实现单向数据流,减少重复代码,比传统数据绑定更简单。等等 JSX是js的语法扩展,允许在js中编写类似HTML的代码 const …...
HTML应用指南:利用POST请求获取三大运营商5G基站位置信息(二)
在当前信息技术迅猛发展的背景下,第五代移动通信(5G)技术作为新一代的无线通信标准,正逐步成为推动社会进步和产业升级的关键驱动力。三大电信运营商(中国移动、中国联通、中国电信)在全国范围内的5G基站部署,不仅极大地提升了网络性能,也为智能城市、物联网、自动驾驶…...
C++学习笔记之内存管理
仅用于记录学习理解 选择题答案及解析 globalVar:C(数据段 (静态区)) 解析:全局变量存放在数据段(静态区),生命周期从程序开始到结束,程序运行期间一直存在。 staticGlobalVar&…...
针对 MySQL 数据库中 主键/唯一约束的更新方法 和 ON DUPLICATE KEY UPDATE 语法的详细说明及示例,并以表格总结
以下是针对 MySQL 数据库中 主键/唯一约束的更新方法 和 ON DUPLICATE KEY UPDATE 语法的详细说明及示例,并以表格总结: 一、主键的更新 1. 更新主键的条件 允许更新:MySQL 允许更新主键列,但需满足以下条件: 唯一性…...
day21 学习笔记
文章目录 前言一、删除数据二、索引操作1.loc方法2.iloc方法 三、添加数据1.loc方法添加数据2.concat方法拼接数据 四、重置索引 前言 通过今天的学习,我掌握了对Pandas对象数据元素进行增删操作以及重置索引的操作 一、删除数据 DataFrame.drop(labelsNone, axis…...
【MyBatis】深入解析 MyBatis XML 开发:增删改查操作和方法命名规范、@Param 重命名参数、XML 返回自增主键方法
增删改查操作 接下来,我们来实现一下用户的增加、删除和修改的操作。 增( Insert ) UserInfoMapper接口: 我们写好UserInfoMapper接口后,自动生成 XML 代码; UserInfoMapper.xml实现: 增删改查方法命名规范 如果我们…...
【Pandas】pandas DataFrame select_dtypes
Pandas2.2 DataFrame Attributes and underlying data 方法描述DataFrame.index用于获取 DataFrame 的行索引DataFrame.columns用于获取 DataFrame 的列标签DataFrame.dtypes用于获取 DataFrame 中每一列的数据类型DataFrame.info([verbose, buf, max_cols, …])用于提供 Dat…...
使用Python构建Kafka示例项目
新建项目 mkdir python-kafka-test cd python-kafka-test 安装依赖 pip install confluent_kafka 创建配置文件 # Kafka配置文件# Kafka服务器配置 KAFKA_CONFIG {bootstrap.servers: localhost:9092,# 生产者特定配置producer: {client.id: python-kafka-producer,acks:…...
