Matlab Simulink S -function (2) Level-1 MATLAB S-Functions

本文主要参考Matlab help 文档。
一个 Level-1 MATLAB S-函数 如下所示:
[sys,x0,str,ts]=f(t,x,u,flag,p1,p2,...) [ s y s , x 0 , s t r , t s ] = f ( t , x , u , f l a g , p 1 , p 2 , . . . ) , 这里 f f 是S函数名称。
在仿真时,Simulink重复调用 f,使用 flag f l a g 参数来表明 要为某个特定的调用 将要执行的任务。 S函数执行任务并将结果返回到输出向量中。

下面是 Level-1 MATLAB S-函数的参数:

参数名称含义
tCurrent time
xState vector
uInput vector
flagInteger value that indicates the task to be performed by the S-function

The following table describes the values that flag can assume and lists the corresponding Level-2 MATLAB S-function method for each value.

Flag Argument

Level-1 FlagLevel-2 Callback MethodDescription
0setupDefines basic S-Function block characteristics, including sample times, initial conditions of continuous and discrete states, and the sizes array (see Define S-Function Block Characteristics for a description of the sizes array).
1mdlDerivativesCalculates the derivatives of the continuous state variables.
2mdlUpdateUpdates discrete states, sample times, and major time step requirements.
3mdlOutputsCalculates the outputs of the S-function.
4mdlOutputsmethod updates the run-time objectNextTimeHit property Calculates the time of the next hit in absolute time. This routine is used only when you specify a variable discrete-time sample time in the setup method.
9mdlTerminatePerforms any necessary end-of-simulation tasks.

相应的代码如下所示: (在文件 sfuntmpl.m 中可以找到)

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值