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

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.
>>

댓글 수: 6

I like to follow-up on your comment "Matlab technical support has not provided sufficient reposes", if you can give me the Service Request number for that.
The thread ID is [THREAD ID:1-DXY0YW]. Roger did a great job but a solution lies in the software development, not technical support department.
Doing this in "comments", as I can't contribute technically. But I have a process comment: Part of this Service Request was raising an enhancement request and you followed up on the status 7 months after your first inquiry. And as this was not processed yet, I understand that a good next step is asking the community. One note about enhancement requests is that they are prioritized by severity/impact and frequency. The bad news is that you are still the first customer asking for this capability.
I can't access the demo code at http://www.et.byu.edu/~bjlarson/SystemModelTools/CompareSimulinkExecution.zip ("you don't have permission..."). Is the code available anywhere else?
Jan
Jan 2018년 2월 16일
편집: Jan 2018년 2월 16일
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.

댓글을 달려면 로그인하십시오.

답변 (5개)

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
Brad 2011년 5월 12일
Walter,
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.
Jarrod,
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.

댓글 수: 6

Any method that cannot produce responses within the target accuracy should not be considered. Methods that are slow can have "hardware thrown at them" but faster hardware cannot correct for incorrect solutions.
Hi Brad,
What is driving the larger simulation? The phrase for doing this thing that I hear often is "cosimulation", FYI.
Accuracy is definately high on the list of what makes a model "Best". #3 "model(,,,'update')" is the weakest there because of the need to roll your own ODE solver.
I am driving the larger simulation from C++ as part of my PhD research but could easily plug it into a variety of other modeling packages. The Simulink models are working with various other models such as servo controllers and particle physics models.
Then, I'd say the best thing to do is generate C code using Embbeded Coder. From your C++ code, you should then be able to call into the step function at each time step and pass the necessary inputs to the model.
C code would be a 5th method to call a Simulink model. How would it compare to the others? My guess: Advantages: should execute the fastest of all the methods. Disadvantages: some Simulink models would not readily generate C code. I do not know if all of the ODE solving algorithms would generate C code. Other ideas of how this would stack up to the others?
I cannot access the example code zip file mentioned above ("You don't have permission ..."). Is there another location where to get it?

댓글을 달려면 로그인하십시오.

Markus Buehren
Markus Buehren 2013년 6월 15일
Hi,
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

카테고리

도움말 센터File Exchange에서 Manual Performance Optimization에 대해 자세히 알아보기

제품

태그

질문:

2011년 5월 12일

편집:

Jan
2018년 2월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by