How to trigger an embedded Matlab block every x seconds in Simulink

24 views (last 30 days)
Hello,
I need to use fmincon on Simulink. I coded my optimization problem in an Matlab function block, but I need to only trigger that block every x seconds. For example, let's say my simulation time is 50 s, I want to run the optimization every 10 s and update the parameters based on the results. How can I do that?
Thank you!

Answers (1)

Hari
Hari on 24 Apr 2024
Edited: Hari on 24 Apr 2024
Hi cata20,
I understand that you have an optimization problem coded within a MATLAB Function block in Simulink, and you wish to trigger this block to execute every 10 seconds during a simulation that runs for 50 seconds. Further, you aim to use the results of the optimization to update parameters periodically throughout the simulation.
I assume you have a MATLAB Function block ready with your "fmincon" optimization code and you're looking for a method to periodically trigger this block without manual intervention during simulation.
To achieve periodic execution of a MATLAB Function block in Simulink, you can use the following approach:
  • Use a Pulse Generator: The Pulse Generator block can be configured to output a pulse every 10 seconds, which can be used to trigger the execution of your MATLAB Function block.
1. Set the Pulse Generator's "Period" to 10 (for 10 seconds).
2. Set the "Pulse Width" to a value less than the period but sufficient to trigger the block (e.g., 1).
3. Set the "Phase Delay" to 0 to start the pulse at the beginning of the simulation.
  • Triggered Subsystem: Encapsulate your MATLAB Function block within a Triggered Subsystem. The pulse from the Pulse Generator will serve as the trigger signal for this subsystem.
1. Connect the output of the Pulse Generator to the trigger input of the Triggered Subsystem.
2. Place your MATLAB Function block inside the Triggered Subsystem.
  • Configuring the MATLAB Function Block: Ensure your MATLAB Function block is set up to perform the optimization and update parameters when triggered. Use global or persistent variables if you need to retain state or results between triggers.
  • Simulation Settings: Make sure your simulation time is set to 50 seconds (or your desired simulation length) in the Simulink model configuration parameters.
References for further exploration:
Hope this helps!

Categories

Find more on Simulink Coder in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!