What is the best way to execute a Simulink model step-by-step (interactively) from Matlab

24 views (last 30 days)
Brad on 12 May 2011
Edited: Jan on 16 Feb 2018
Matlab provides at least 3 ways to execute a Simulink model step-by-step from Matlab. Which way is best? (See http://www.mathworks.com/support/tech-notes/1900/1903.html for background) Matlab technical support has not provided sufficient reposes or fixes for a clear best solution. If you have experience in this area, please provide input and source code if possible. My evaluation source code: http://www.et.byu.edu/~bjlarson/SystemModelTools/CompareSimulinkExecution.zip
The times of the following evaluation provide a comparison of executing 200 steps of identical Simulink models from matlab. At each step, new inputs are written from Matlab to the Simulink model and results are read.
  1. All-at-once model execution time for comparison. (Execution time 0.155641 seconds)
  2. "sim" step-by-step execution (Execution time 10.143009 seconds). Initialize each step with previous model state. Advantages: Can choose any solver from Simulink model. Can use standard Simulink In and Out ports. Output data can be at a higher resolution than evaluation steps. Disadvantages: very very slow.
  3. model(,,,'update') (Execution time 0.130168 seconds). Advantages: very fast. Even faster than all-at-once execution. Can use standard Simulink In and Out ports. Disadvantages: Need to create your own ODE solver. Output data is collected only at the model evaluation step resolution. Ignores all of the solver configuration parameters.
  4. set_param(,,'step') (Execution time 0.360453 seconds) Advantages: Can choose any solver from Simulink model. Output data can be at a higher resolution than evaluation steps. Moderately fast. Disadvantages: Must create S-function model input and model output ports. Cannot run Matlab headless.
Running Evaluation:
"CompareSimulinkExecution.m" is the top-level evaluation script. "TestS.mdl" is the model used for the first 3 evaluation methods. "TestSParam.mdl" is used for "set_param(,,'step')" evaluation. "ModelInput.m" and "ModelOutput.m" are s-functions that force immediate data transfer to and from the Matlab workspace.
>> CompareSimulinkExecution
All-at-once sim call - provides a base model evaluation time:
Elapsed time is 0.115553 seconds.
Series of sim calls:
Elapsed time is 8.924163 seconds.
Step-by-step model calls:
Elapsed time is 0.124648 seconds.
set_param step:
Elapsed time is 0.463443 seconds.
Jan on 16 Feb 2018
Mojtaba Varmazyar wrote: "I have the same issue"
@Mojtaba Varmazyar: Please use flags only to inform admins and editors about contributions, which are not conform with the terms of use of the forum, e.g. due to rudeness or spam. Thanks.
The link from the original question is dead. Only the author could provide the files, but he is not active in the forum for 7 years now. This is the old problem of using links to externally stored files instead of attaching them directly in the forum.

Sign in to comment.

Answers (5)

Walter Roberson
Walter Roberson on 12 May 2011
"Best" for what goal?

Jarrod Rivituso
Jarrod Rivituso on 12 May 2011
Forgive the basic question, but why can't you just set the simulation inputs to change at desired points in the simulation?
For instance, if my model had a root-level Inport block, and was set to Import the data [t,u] (in the Simulation -> Configuration Parameters -> Data Import/Export -> Input option), then I could do something like this:
>> t = [ 0; 2; 4; 6; 8]
>> u = [13;17;10;28;37]
Then my simulation would change the input at those times. Note, by default the Inport block will interpolate, but I could disable that by double-clicking the Inport block and disabling the Interpolate Data option.

Brad on 12 May 2011
My Criteria for "Best":
  1. Executes reliably across various Simulink models
  2. Executes efficiently
  3. Preserves expected Simulink workflow.
For example, #3, "model(,,,'update')" would not execute stiff models reliably because you do not have access to the more complex ODE solvers built into Simulink. #2 is just incredibly slow. #4, "4.set_param(,,'step')" requires an unexpected method of model input and output method (s-functions) that will invariably have different capabilities compared to the standard Simulink In and Out blocks.
Why not just provide all input at the beginning and let Simulink run (test #1)? Because Simulink is a part of a larger simulation. The inputs to Simulink are changing in response to its outputs.
Markus Buehren
Markus Buehren on 15 Jun 2013
I cannot access the example code zip file mentioned above ("You don't have permission ..."). Is there another location where to get it?

Sign in to comment.

Markus Buehren
Markus Buehren on 15 Jun 2013
when using method 3 (the model command), scopes in my discrete simulink model do not show any data. Also, the time (first parameter of the model function) does not seem to have any effect. I can set it to zero, to the step number or to [] without any influence.
Any ideas?
Yours Markus

Mojtaba Varmazyar
Mojtaba Varmazyar on 16 Feb 2018
I would like to access http://www.et.byu.edu/~bjlarson/SystemModelTools/CompareSimulinkExecution.zip. I wonder if anyone has access and send it to me.
Thanks Mojtaba



Community Treasure Hunt

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

Start Hunting!