Please let me know the best practice for authoring a iterating logic in Simulink.

2 views (last 30 days)
One of the problem statement that I work on requires to find an optimal value of a variable at every simulation time step. But the catch is I need to run a iterative routine, for loop in my case at every time step to evaluate the required value of the variable.
Based on my knowledge I had three options:
  1. Simulink blocks to implement the logic flow (for loop in my case) - Very difficult if the length of the loop is large
  2. MATLAB function - Easy to implement but dubious on if it is a better choice for hardware deployment/code generation
  3. Stateflow - Again easy to implement but dubious if it is a better choice for code optimizations/hardware deployment.

Answers (1)

Shaunak
Shaunak on 22 Apr 2025
Edited: Shaunak on 23 Apr 2025
Hi Akhil,
As per my understanding, for running an iterative routine at each simulation step, the MATLAB Function block is a practical and flexible choice. It’s efficient for implementing iterative logic and works well for hardware deployment if your code supports code generation. For straightforward iterative optimization, it keeps things simple compared to Stateflow.
If you’re running multiple simulations with only parameter changes (like controller tuning), you might also want to consider using 'parsim' with 'Simulink.SimulationInput'. This approach can speed up your workflow by enabling parallel simulations rather than running them sequentially in a for-loop.
Kindly refer to the MATLAB Answers thread below for more discussion on optimizing iterative Simulink calls:
You can also check out the following MathWorks documentation for 'parsim' here:
Hope this helps!

Categories

Find more on Verification, Validation, and Test in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!