Hi Andrew,
In Simulink, the behavior of the For Iterator or While Iterator subsystems is primarily governed by the simulation settings and the configuration of the blocks within these subsystems. By default, these iterator subsystems execute once per simulation time step. However, there are a few approaches you can consider to achieve the behavior you're describing, where the subsystem needs to re-evaluate every time a relevant variable is perturbed within a time step.
- Subsystems with Enabled Ports: You can use an Enabled Subsystem where the enable signal is driven by the change in your relevant variable. This can potentially cause the subsystem to execute multiple times within a single time step if the enable signal changes. However, this approach depends heavily on how your variables change and how the enable signal is generated.
- Rate Transition Blocks: If the changing variable is sampled at a higher rate than the rest of your model, you can use Rate Transition blocks to manage the data transfer. This might not exactly execute the subsystem multiple times per time step, but it can help in managing data at different rates.
- Using Function-Call Subsystems: This approach is a bit more advanced. You can use function-call subsystems that are triggered by a function call event. This can potentially allow the subsystem to re-execute whenever the function call is triggered within a time step. This would require generating function call events based on the changes in your variables.
- Solver Configuration: Adjusting the solver settings, like decreasing the step size or using a variable-step solver, can increase the frequency of the evaluations. This doesn’t directly cause multiple evaluations per time step but can make the solver more responsive to changes in the system dynamics.
- Custom S-Function: Writing a custom S-Function block allows for more control over execution during a simulation step. You can program the S-Function to re-evaluate based on changes in certain variables. This, however, requires a good understanding of MATLAB S-Function API and how Simulink works internally.
- Stateflow for Event-Driven Logic: If none of the above solutions work, using Stateflow might be a viable option. Stateflow excels in designing event-driven systems where states and actions can change dynamically within a time step. However, integrating Stateflow with the existing Simulink model might require some restructuring of your model logic.
In summary, while vanilla Simulink might offer some solutions, they often require careful design and may not directly offer the straightforward functionality of re-evaluating a subsystem multiple times within a single time step based on variable changes. Stateflow offers more flexibility for event-driven dynamics, but it requires transitioning part of your logic to a Stateflow chart. Each method has its trade-offs, and the choice largely depends on the specific requirements and constraints of your model.
Hope this helps!