Matlab Simulink 主时间步(major time step)、子时间步(minor time step)

高亮颜色说明:突出重点
个人觉得,:待核准个人观点是否有误
高亮颜色超链接

文章目录

    • [对Simulink 时间步的理解](#对Simulink 时间步的理解)
    • [Simulink 采样时间的类型](#Simulink 采样时间的类型)
      • [Discrete Sample Times(离散采样时间)](#Discrete Sample Times(离散采样时间))
        • [Controllable Sample Time(可控采样时间)](#Controllable Sample Time(可控采样时间))
      • [Continuous Sample Times(连续采样时间)](#Continuous Sample Times(连续采样时间))
        • [Continuous Sample Time](#Continuous Sample Time)
        • [Fixed-in-Minor-Step Sample Time(子步中不变采样时间)](#Fixed-in-Minor-Step Sample Time(子步中不变采样时间))
        • [Variable Sample Time](#Variable Sample Time)
    • [Simulink 子时间步日志生成](#Simulink 子时间步日志生成)
    • [S-Function Callback Methods](#S-Function Callback Methods)
    • [Simulink 逐步仿真](#Simulink 逐步仿真)
      • [Stepping Forward](#Stepping Forward)
      • [Capturing Simulation Snapshots](#Capturing Simulation Snapshots)
      • [Stepping Back](#Stepping Back)
      • [Pausing on Breakpoints](#Pausing on Breakpoints)
      • [Additional Considerations for Stepping Through Simulation](#Additional Considerations for Stepping Through Simulation)
      • [See Also](#See Also)
      • [Related Topics](#Related Topics)
    • 二级标题
    • 待补充

20240915记:Matlab Help Center 检索"minor time step"、"minor time step",

给定一个具有离散采样时间的模块,Simulink® 软件将在以下时间执行模块输出或更新方法:

t n = n T s + ∣ T o ∣ , t_n=nT_s+|T_o|, tn=nTs+∣To∣,

其中,采样时间周期 T s T_s Ts 始终大于零且小于仿真时间 T s i m T_{sim} Tsim。周期数 ( n n n) 是一个整数且必须满足以下条件:

0 ≤ n ≤ T s i m T s . 0\leq n \leq \frac{T_{sim}}{T_s}. 0≤n≤TsTsim.

在仿真过程中,软件在每个固定时间间隔 t n t_n tn 只计算一次模块输出。这些仿真时间,即软件为给定的采样时间执行模块输出方法的时间,称为采样时间命中点 。如果您需要延迟初始采样时间命中点,可以定义偏移量 T o T_o To。

如果模型具有连续状态和/或非采样过零点,则在模型仿真的积分阶段会涉及到子时间步,因为Simulink使用数值积分算法(如ODE45)来模拟系统。连续 采样时间命中点分为主时间步(major time steps)和子时间步(minor time steps)。子时间步是对主时间步的细分。您选择的 ODE 求解器将对从仿真开始到 给定的主时间步或子时间步 为止的所有连续状态进行积分。求解器将确定子时间步的时间点,并在每个主时间步生成结果,而不向您展示中间计算过程,您只能看到在主时间步的模块输出。求解器使用子时间步的结果来提高主时间步结果的准确性,因为对于某些连续时间模型,子时间步有助于模型中方程的解的收敛。

20240915记:
*** Types of Sample Time - MATLAB & Simulink

(##) 采样时间的类型

Discrete Sample Times(离散采样时间)

假如一个模块具有离散采样时间,Simulink® 软件将在以下时间执行模块输出或更新方法:

t n = n T s + ∣ T o ∣ , t_n=nT_s+|T_o|, tn=nTs+∣To∣,

其中,the sample time period(采样时间周期) T s T_s Ts 始终大于零且小于 the simulation time(仿真时间) T s i m T_{sim} Tsim。周期数 ( n n n) 是一个整数且必须满足以下条件:

0 ≤ n ≤ T s i m T s . 0\leq n \leq \frac{T_{sim}}{T_s}. 0≤n≤TsTsim.

在仿真过程中,软件在每个固定时间间隔 t n t_n tn 只计算一次模块输出。这些仿真时间,即软件为给定的采样时间执行模块输出方法的时间,称为sample time hits(采样时间命中点)。"采样时间命中点是已知先验的"的采样时间类型,只有唯一一种,即离散采样时间。

如果您需要延迟初始采样时间命中点,可以定义偏移量 T o T_o To。

Unit Delay 模块就是一种具有离散采样时间的模块。

Controllable Sample Time(可控采样时间)

Continuous Sample Times(连续采样时间)

Continuous Sample Time

连续采样时间命中点分为主时间步(major time steps)和子时间步(minor time steps)。子时间步是对主时间步的细分。求解器在每个主时间步生成结果。求解器使用子时间步的结果来(???)提高主时间步结果的准确性。

您选择的 ODE 求解器将对从仿真开始到给定的主时间步或子时间步为止的所有连续状态进行积分。求解器将确定子时间步的时间点,并使用子时间步的结果来提高主时间步结果的准确性。您只能看到在主时间步的模块输出。

Fixed-in-Minor-Step Sample Time(子步中不变采样时间)

如果模块的采样时间是 [0 1],则模块将采用子步中不变的采样时间。对于此采样时间,模块不会在子时间步执行。模块仅在主时间步执行。对于主时间步之间无法改变输出结果的模块,采用子步中不变的采样时间消除了不必要的计算。

Variable Sample Time

20240915记:
*** Minor Time Step Logging >> File Exchange Pick of the Week - MATLAB & Simulink 20110610

Will 本周推荐的是 Guy Rouleau 的 Minor Time Step Logging

当你在Simulink中按下播放按钮时会发生什么?Simulink是如何处理您的系统的?当算法看起来是正确的,但你的结果似乎是可疑的,你如何排除故障?我记得当我第一次开始开发Simulink模型时,这些想法在我的脑海中渗透。如果你对这样的事情感到好奇,一个很好的起点是阅读 Simulating Dynamic Systems 的文档。在有用信息的宝库中,您会发现Simulink的关键概念之一是主时间步(major time step)和子时间步(minor time step)。一旦仿真完成,Simulink就会在特定的时间点提供结果。这些是主时间步的结果。当使用可变步长求解器时,结果精确到您在 Configuration Parameters 中定义的误差容限。

通常不向您展示的是Simulink的中间计算,因为它在每个主时间步中处理您的系统。对于某些连续时间模型,子时间步有助于模型中方程的解的收敛。任何上过数值方法课的人都会熟悉这个概念,并且可能听说过 Runge-Kutta求解器,这是Simulink中可用的许多求解器算法之一。

十有八九,你不必担心Simulink在一个子时间步上计算的结果。然而,拥有这些信息对于调试来说是非常宝贵的,并且它可以帮助解决一个古老的问题,"我应该为我的问题选择哪个求解器?"获取在子时间步上的值(minor step values)的方法有很多种,但我绝对喜欢Guy提供的解决方案。

您所要做的就是将 Minor Time Step Logging block 从库中拖到模型中,并将其连接到感兴趣的信号。运行您的仿真,瞧,您有一个显示在主时间步和子时间步上的值(the major and minor step values)的MATLAB图形。我在许多具有单一和多维信号的模型上进行了试验......每次都很有效。
minorstep_model.png

我在我久经考验的整流器模型上使用了这个块,并进行了一个小实验。我用ode45和ode23t求解器进行了仿真,并比较了结果,如下所示。正如您所看到的,ode23t在第一个子时间步中可以更好地获得正确的值(所有蓝色X标记都在红色圆圈附近)。然而,由于求解器精度的固有差异,它需要大约两倍的主时间步才能得到解。最终,哪个求解器更适合我的问题?我将把这个问题留作讨论,但这些结果肯定会帮助您理解行为并做出明智的决定。
minorstep_plots.png

Guy的block是如何产生这些图形的?他依靠一个 Level 2 MATLAB S-Function 将Simulink数据存储到MATLAB工作空间。查看LogMinorTimeStep.m,来看看他是如何利用start、Output和Terminate方法来完成他的目标的。这是一个学习 S-Functions 的好例子,如果你从来没学过的话。
20240916记:
Force Update a Block Output at Minor Time Steps - Help Center Answers - MATLAB & Simulink20131011

S-Function Callback Methods

20240915记:
How does the minor time step work in Simulink? What does minor time step mean? - Help Center Answers - MATLAB & Simulink 20211123

Minor time steps occur if your model has continuous states. It occurs during the integration stage of your model. Simulink uses a numerical integration algorithm such as ODE45 to simulate a system. During the integration stage the solver obtains the derivatives from the system and uses it to compute a new state vector for the next time step. This entire process is called the integration phase or minor time step.

如果模型具有连续状态,则会涉及到子时间步(minor time step),它出现在模型的积分阶段。Simulink使用数值积分算法,如ODE45来模拟系统。在积分阶段,求解器从系统中获得导数,并使用它来为下一个时间步计算一个新的状态向量。这一整个过程称为积分阶段或子时间步。

Page 3-16 of the Writing S-functions manual (version 3.0) shows a graphical representation of the calling sequence of an S-function which should help to make this more clear. This is also shown here: https://www.mathworks.com/help/simulink/sfg/what-is-an-s-function.
20240915记:
What Is an S-Function? - MATLAB & Simulink
S-Function Callback Methods

An S-function comprises a set of S-function callback methods that perform tasks required at each simulation stage. During simulation of a model, at each simulation stage, the Simulink engine calls the appropriate methods for each S-Function block in the model. Tasks performed by S-function callback methods include:

  • Compilation --- 略
  • Calculation of outputs --- 略
  • Update discrete states --- 略
  • Initialize and Terminate Methods --- 略
  • Integration --- This applies to models with continuous states and/or nonsampled zero crossings. If your S-function has continuous states, the engine calls the output and derivative portions of your S-function at minor time steps. This is so the solvers can compute the states for your S-function. If your S-function has nonsampled zero crossings, the engine also calls the output and zero-crossings portions of your S-function at minor time steps so that it can locate the zero crossings.
    20240915记:
    什么是 S-Function - MATLAB & Simulink - MathWorks 中国
    S-Function 回调方法

S-Function 由一组 S-Function 回调方法组成,这些方法执行每个仿真阶段所需的任务。在模型仿真期间的每个仿真阶段,Simulink 引擎都会为模型中的每个 S-Function 模块调用适当的方法。S-Function 回调方法执行的任务包括:

  • 编译 - 在此阶段,Simulink 引擎初始化 S-Function。任务包括:
    • 将库模块合并到模型中,并传播信号宽度(what???)、数据类型和采样时间
    • 设置输入和输出端口的数目和维度
    • 计算模块参数,并确定模块执行顺序
    • 分配内存和存储区域
  • 计算输出 - 在此状态下,计算输出,直到所有模块输出端口都对当前时间步有效,即所有输出值都在某个误差界限内。
  • 更新离散状态 - 在此调用中,模块执行那些在每个时间步执行一次的活动,如更新离散状态。
  • 初始化和终止方法 - 这些可选方法只执行一次 S-Function 所需的初始化和终止活动。初始化活动可能包括设置用户数据,或在 S-Function 中初始化状态向量。终止方法执行任何操作,例如当仿真终止或从模型中删除 S-Function 模块时释放所需的内存。
  • 积分 - 这适用于具有连续状态和/或非采样过零点(what???)的模型。如果您的 S-Function 具有连续状态,引擎会在子时间步上调用 S-Function 的输出和导数部分。这是为了让求解器可以计算 S-Function 的状态。如果 S-Function 具有非采样过零点,引擎也会在子时间步上调用 S-Function 的输出和过零点部分,以便能够定位过零点。

20240916记:
How Stepping Through Simulations Works - MATLAB & Simulink

(##) How Stepping Through Simulations Works

在"Simulation Stepping Options(仿真步进选项)"对话框中指定的参数值会影响软件的运行方式:

  • 当您单击"前进(Step Forward)"或"后退(Step Back)"时,向前推进模拟;
  • 捕获模型工况点的快照以支持回退(stepping back);

在仿真中,当您step forward时,软件将继续向前推进仿真。但是,当您step backward时,软件不会向后回退仿真。为了支持在仿真中stepping back,软件在仿真运行时捕获模型工况点的快照。当您单击"Step Back"时,软件将从其中一个快照恢复模型工况点。

Note: 只有开启stepping back功能,软件才会采集stepping back仿真快照。当您为正在进行的仿真启用stepping back功能时,您必须先step forward,然后才能step back。您不能step back到启用stepping back之前的仿真中的某个点。

在"配置参数"对话框的"数据导入/导出(Data Import/Export)"窗格中选择"Save final operating point (保存最终工况点)"时,仿真快照包含的模型工况点信息与记录的相同。软件在仿真快照中捕获模型工况点信息仅支持stepping back。软件为stepping back而捕获的仿真快照在仿真期间或之后都不可访问。有关保存模型工况点的更多信息,请参见"Save Block States and Simulation Operating Points(保存模块状态和仿真工况点)"。

Stepping Forward

"Move back/forward by"参数指定每次单击"Step Forward"时,将仿真向前推进的时间步数。下图(Fig.1)说明了在"Move back/forward by"选项被指定为1或2时仿真将如何进行:

  • 在仿真开始时,State 0,该参数值为2。单击"Step Forward"后,仿真将通过State 1,在State 2暂停。
  • 在第二个时间步之后暂停时,假设您将该参数值更改为1。现在,当您单击"Step Forward"时,仿真将前进到State 3,它对应于下一个时间步。
  • 假设您在第四个主时间步之后暂停时将该参数值更改回2。现在,当您单击"Step Forward"时,仿真将前进到State 6。

simulationtoolbar_snapsteps1.png

Fig.1: A timeline of the simulation that represents the simulation state for each time step as a green circle. Arced arrows illustrate the simulation progression.

Fig.1: 仿真的时间轴,用绿色圆圈表示每个时间步的仿真状态。圆弧箭头表示仿真过程。

Capturing Simulation Snapshots

捕获仿真快照软件收集的用于stepping back的仿真快照包含从快照拍摄点开始继续仿真所需的所有信息。收集快照需要内存,并且会影响仿真性能。当您配置仿真步进选项时,您可以指定:

  • Maximum number of saved back steps(所保存后退时间步的最大数量) - 保存快照的总数量
  • Interval between stored back steps(所保存后退时间步之间的间隔) - 保存快照之间跳过的仿真主时间步的数量

保存较少数量的快照以及在快照之间跳过一些时间步可以减轻启用stepping back功能对仿真性能的影响。

保存快照的仿真步进选项独立于"Move back/forward by"选项,后者选项控制单击"Step Forward"或"Step Back"时仿真的前进方式。

下图(Fig.2)说明了当"Interval between stored back steps"参数值为3时,如何捕获快照。软件在仿真开始时、在第三个主时间步之后和在第6个主时间步之后捕获快照。
simulationtoolbar12.png

Fig.2: A timeline of the simulation that represents the simulation state as a green circle. The shade of green is darker for each state that is captured as a snapshot.

Fig.2: 仿真的时间轴,用绿色圆圈表示仿真状态。快照捕获了绿色阴影较深的状态。

当需要以较小的增量推进仿真以分析感兴趣的点时,您可以在仿真期间更改快照之间的间隔。例如,下图(Fig.3)说明了如果将快照之间的间隔从3更改为1,上一个图(Fig.2)中的仿真将如何捕获快照。

  • 软件在仿真开始时捕获State 0的快照。
  • 经过三个时间步,软件捕获State 3的快照。
  • 在第三个时间步之后的State 3上暂停时,将"Interval between stored back steps"参数值从3更改为1。
  • 此后,软件为每个时间步捕获仿真状态的快照,通过State 6。

simulationtoolbar14.png

Fig.3: A timeline of the simulation that represents the simulation state as a green circle. The shade of green is darker for each state that is captured as a snapshot.

Stepping Back

软件在仿真中后退的方式取决于您所后退到的时刻是否存在快照。

  • 当该时间步存在快照时,软件将恢复快照中包含的模型工况点。
  • 当该时间步不存在快照时,软件从该时间步之前的最后一个快照恢复模型工况点,然后向前仿真。

例如,假设在仿真的第6个时间步之后暂停:

  • 每隔3个时间步对模型工况点进行快照
  • 每单击一次"Step Back",将后退1个时间步

当您单击"Step Back",软件将恢复第三个时间步之后的快照,并向前仿真2个时间步,在第5个时间步之后暂停。
simulationtoolbar_snapsteps2.png

Fig.4: A timeline of the simulation that represents the simulation state as a green circle. The shade of green is darker for each state that is captured as a snapshot. A solid arced arrow represents the progression of the simulation when you click Step Back. Dotted arced arrows illustrate how the software steps back.

Fig.4: 仿真的时间轴,用绿色圆圈表示仿真状态。作为快照捕获的每个状态的绿色阴影较深。当您单击"Step Back"时,实圆弧箭头表示仿真的进度。虚线箭头说明了软件是如何后退的。

您可以在仿真期间暂停时修改可调参数的值。该软件将新的参数值应用于下一步的仿真。根据您如何配置仿真步进选项,在单击"Step Back"时可能会执行仿真的下一个前进步骤。

Pausing on Breakpoints

当您的模型包含活动的断点时,如果在前进的过程中断点的条件被满足了,则仿真将在断点上暂停。软件在后退时忽略断点。

默认情况下,软件被配置为一旦断点的条件被满足就在一个时间步内暂停。当您在断点上暂停时单击"Step Forward"时,仿真前进将通过当前的时间步。单击"Step Back"后,软件将后退到当前时间步的起始位置。有关详细信息,请参见Breakpoints List(断点列表)

Additional Considerations for Stepping Through Simulation

See Also

Related Topics


20240916记:

(待阅读)Simulation Stepping Options - Enable stepping back and configure number of time steps to move when stepping through simulation - MATLAB
20240916记:

(待阅读)Step Through Simulations Using the Simulink Editor - MATLAB & Simulink

这个例子展示了如何逐步执行仿真。通过使用Simulink编辑器中的控件,您可以以一个或多个时间步推进仿真,也可以通过逐块步进仿真来逐步跨过一个时间步。

此示例仅说明了与逐步执行仿真相关的功能和选项。若希望通过逐步执行仿真来在要研究的(时刻)点上暂停,请使用信号断点。有关详细信息,请参见Debug Simulation Using Signal Breakpoints(使用信号断点调试仿真)

要在脚本化仿真中逐步执行一个或多个时间步,请使用Simulation对象。

二级标题

待补充

待补充

相关推荐
The hopes of the whole village6 小时前
matlab 绘图
开发语言·matlab·信息可视化
程高兴16 小时前
基于Matlab的车牌识别系统
开发语言·matlab
XuX0319 小时前
手搓雷达图(MATLAB)
matlab·贴图
freexyn1 天前
Matlab自学笔记五十一:(推荐)输入参数的数量和可变数量的输入
笔记·算法·matlab
不吃酸的柠檬1 天前
MATLAB 中的图形绘制
人工智能·机器学习·matlab
studyer_domi1 天前
Matlab 复合模糊PID
开发语言·matlab
DarrenPig2 天前
【新能源科学与技术】MATALB/Simulink小白教程(一)实验文档【新能源电力转换与控制仿真】
matlab·开源·github·simulink·交流
简简单单做算法2 天前
基于GA遗传优化TCN-BiGRU注意力机制网络模型的时间序列预测算法matlab仿真
matlab·tcn-bigru·时间序列预测·注意力机制·ga遗传优化
Cc小跟班2 天前
MATLAB小技巧记录(特殊符号、图例位置...)
开发语言·算法·matlab
随风飘摇的土木狗3 天前
【MATLAB第116期】基于MATLAB的NBRO-XGBoost的SHAP可解释回归模型(敏感性分析方法)
matlab·gsa·敏感性分析·特征排序·灵敏度分析·shap·可解释