在使用Simulink的过程中经常会遇到一个问题,我希望我自己封装的模块中(一个Mask)的参数实时变化,例如
1.我想要模拟负载电阻突然变化(一开始电阻为2Ω,在1s后电阻变为1Ω)
2.电机运行过程模拟参数实时变化的工况(例如电感随时不断改变)
等等
那么就迫切的需要我们设置的一个参数能随时间实时的改变,那么解决方法可以采用S-funtion模块利用set_param
希望大家有所收获,喜欢本篇文章的话点个 关注 收藏 赞 吧!!!!!!
S-function模块
模块路径
![](https://img-blog.csdnimg.cn/20210603204634460.png)
模块
![](https://img-blog.csdnimg.cn/20210603204737377.png)
S-function需要在matlab中编程,打开S函数模板方法:在matlab命令行窗口输入 edit sfuntmpl 会打开S函数得模板
![](https://img-blog.csdnimg.cn/20210603204851954.png)
S-function模板
function [sys,x0,str,ts,simStateCompliance] = sfuntmpl(t,x,u,flag)
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 1,
sys=mdlDerivatives(t,x,u);
%%%%%%%%%%
% 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 %
%%%%%%%%%%%%%%%%%%%%
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 1;
sizes.NumInputs = 1;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
%
% initialize the initial conditions
%
x0 = [];
%
% 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';
function sys=mdlDerivatives(t,x,u)
sys = [];
function sys=mdlUpdate(t,x,u)
sys = [];
function sys=mdlOutputs(t,x,u)
sys = [];
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 = [];
set paramo函数及功能定义
![](https://img-blog.csdnimg.cn/20210603205817693.png)
语法
![](https://img-blog.csdnimg.cn/20210603205728763.png)
说明
![](https://img-blog.csdnimg.cn/20210603205948212.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xvb2tlcmtpZA==,size_16,color_FFFFFF,t_70)
示例
![](https://img-blog.csdnimg.cn/20210603210054898.png)
![](https://img-blog.csdnimg.cn/20210603210113825.png)
![](https://img-blog.csdnimg.cn/20210603210134932.png)
输入参数
![](https://img-blog.csdnimg.cn/20210603210305976.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xvb2tlcmtpZA==,size_16,color_FFFFFF,t_70)
![](https://img-blog.csdnimg.cn/20210603210332130.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xvb2tlcmtpZA==,size_16,color_FFFFFF,t_70)
![](https://img-blog.csdnimg.cn/20210603210344418.png)
Simulink仿真
1.建立名为test_S的模型
![](https://img-blog.csdnimg.cn/20210603210701784.png)
2.在模型中加入S-function模块
3.搭建模型
假设我们现在需要一个随时间变化的电感在1s前是2H在1s后变为1H 把参数设为L
![](https://img-blog.csdnimg.cn/20210603211201747.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xvb2tlcmtpZA==,size_16,color_FFFFFF,t_70)
![](https://img-blog.csdnimg.cn/20210603211358507.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xvb2tlcmtpZA==,size_16,color_FFFFFF,t_70)
4.建立一个Mask
点击建立子系统
![](https://img-blog.csdnimg.cn/20210603211307584.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xvb2tlcmtpZA==,size_16,color_FFFFFF,t_70)
设置参数
![](https://img-blog.csdnimg.cn/20210603211438946.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xvb2tlcmtpZA==,size_16,color_FFFFFF,t_70)
点击添加参数
![](https://img-blog.csdnimg.cn/20210603211507516.png)
prompt可以随意写 Name必须与上面设置的变量名相同!!!!!!
![](https://img-blog.csdnimg.cn/2021060321154058.png)
完成搭建
![](https://img-blog.csdnimg.cn/20210603211659706.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xvb2tlcmtpZA==,size_16,color_FFFFFF,t_70)
5.S函数编写
按照上面的s模板建立一个名为fortest的函数
![](https://img-blog.csdnimg.cn/20210603211908561.png)
单输入无输出仅起着改变系统参数的作用
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 0;
sizes.NumInputs = 1;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
参照上面给出的语法编写 模型名/模块名 参数名 赋值
function sys=mdlOutputs(t,x,u)
if u |