这是我个人学习s-function编写时完成的一个经典实例,与大家分享一些学习思路。
题目:蹦极是一种身体极限运动,蹦极者系着一根弹力绳从高处的桥梁或山崖向下跳。下图为整个系统的示意图。距地20m处为x轴,向下为x正方向。桥梁距离地面的高度为50m,绳子的原长为30m。
若蹦极者系在一个弹性系数为 k =20的绳索上,设绳索下端初始位置为 0,则蹦极者受到的弹力:
整个蹦极系统的数学模型为:
其中,m为物体质量,g为重力加速度取10,x为物体位置,第二项为物体受到的弹性力,第三项和第四项表示空气的阻力,a1=a2=1。试判断质量为70kg的人能否安全地享受此游戏带来的乐趣。
解:为了便于后面的仿真计算,可以将物理模型写成如下状态方程形式。可设状态向量:
故状态方程为:
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。
有不懂的地方可以留言,感谢支持。