基于S-Function/S函数的蹦极实例教程

本文介绍了如何使用MATLAB中的S-Function编写蹦极物理模型,包括状态方程的设定、仿真计算和安全评估,警示读者在模拟极限运动时必须考虑安全因素。
摘要由CSDN通过智能技术生成

这是我个人学习s-function编写时完成的一个经典实例,与大家分享一些学习思路。
题目:蹦极是一种身体极限运动,蹦极者系着一根弹力绳从高处的桥梁或山崖向下跳。下图为整个系统的示意图。距地20m处为x轴,向下为x正方向。桥梁距离地面的高度为50m,绳子的原长为30m。

若蹦极者系在一个弹性系数为 k =20的绳索上,设绳索下端初始位置为 0,则蹦极者受到的弹力:

b(x)=\left\{\begin{matrix}-kx \, \, \, \, \, \, \, x > 0 \\ 0\, \, \, \, \, \, \, \, \, x\leqslant 0 \end{matrix}\right.

整个蹦极系统的数学模型为:

m\ddot{x}=mg+b(x)-a_{1}\dot{x}-a_{2}\left |\dot{x} \right |\dot{x}
其中,m为物体质量,g为重力加速度取10,x为物体位置,第二项为物体受到的弹性力,第三项和第四项表示空气的阻力,a1=a2=1。试判断质量为70kg的人能否安全地享受此游戏带来的乐趣。

解:为了便于后面的仿真计算,可以将物理模型写成如下状态方程形式。可设状态向量:

x_{1}=x,x_{2}=\dot{x}

故状态方程为:

\left\{\begin{matrix}\dot{x_{1}}=x_{2} \\ \dot{x_{2}}=g+b(x_{1})/m-a_{1}x_{2}/m-a_{2}\left | x_{2} \right |x_{2}/m \end{matrix}\right.

1.首先介绍s函数的打开方法。

在matlab命令行窗口运行如下代码:

edit sfuntmpl

随即便可打开matlab自带的s函数源代码,全选复制并粘贴到新建m文件脚本。(切忌在matlab源代码上直接进行修改)。保存m文件并重命名,注意m文件名要和函数名保持一致(不然会报错),如图所示:


2.然后在新建s函数上修改代码

function [sys,x0,str,ts,simStateCompliance] = sfun_demo1(t,x,u,flag,m,a1,a2,g,k)

switch flag,

  %%%%%%%%%%%%%%%%%%
  % Initialization %
  %%%%%%%%%%%%%%%%%%
  case 0,
    [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes(m,a1,a2,g,k);

  %%%%%%%%%%%%%%%
  % Derivatives %
  %%%%%%%%%%%%%%%
  case 1,
    sys=mdlDerivatives(t,x,u,m,a1,a2,g,k);

  %%%%%%%%%%
  % Update %
  %%%%%%%%%%
  case 2,
    sys=mdlUpdate(t,x,u);

  %%%%%%%%%%%
  % Outputs %
  %%%%%%%%%%%
  case 3,
    sys=mdlOutputs(t,x,u,m,a1,a2,g,k);

  %%%%%%%%%%%%%%%%%%%%%%%
  % 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

% end sfuntmpl

%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes(m,a1,a2,g,k)

%
% 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      = 0;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;   % at least one sample time is needed

sys = simsizes(sizes);

%
% initialize the initial conditions
%
x0  = [-30 0];  % x=[x1 x2]=[x dx]=[-30 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,m,a1,a2,g,k)

sys(1) = x(2);   % dx1
if x(1)>0
sys(2) = (m*g-k*x(1)-a1*x(2)-a2*x(2)*abs(x(2)))/m;     % dx2
else
sys(2) = (m*g-a1*x(2)-a2*x(2)*abs(x(2)))/m;             % dx2
end
sys = [sys(1) sys(2)];  % [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,m,a1,a2,g,k)
sys = [x(1) 20-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

simulink模型的搭建:(模型命名切勿与s函数命名一致)

s-function函数的配置(也可进行mask封装):

观察示波器:

蹦极者位移x超出20m(或20-x成为负数),此蹦极系统并不能让70kg的人嗨。

综上所述,蹦极有风险,入坑需谨慎!

-----------------------------------------------------------------------------------------------------------

注意:若双击打开s-function模块并edit 出现m文件找不到的情况,请检查:1、是否将slx文件及其对应的m文件放置于同一文件夹内(如文件夹A)。2、matlab当前文件夹是否为文件夹A。

有不懂的地方可以留言,感谢支持。

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱斯维尔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值