Run a Simulink modell stepwise inside Matlab
16 views (last 30 days)
Show older comments
Hello together!
I'm currently trying to integrate a Simulink model into a Matlab function and simulating it step-wise.
Goal would later be to integrate a (already existent) huge model with a vehicle motion control into a simulation run by Matlab.
This simulation needs the model to run step-wise for adjusting input variables and getting the results every step.
So the plan would be:
- Setting inputs
- Running the model for 1 step
- Getting the output data
- Repeat.
The internal states of the simulation should of course still remain.
I searched quite some time and have now done the following approach:
load_system("simmodel");
step_size = get_param("simmodel", "FixedStep");
% generate a SimulationInput object from the model
simIn = Simulink.SimulationInput('simmodel');
% change general settings to allow saving it's state
simIn = simIn.setModelParameter('SaveCompleteFinalSimState','on', 'SaveFinalState','on','FinalStateName','savePoint');
n = 0;
while n < 100
% here set input parameter for model
input = timeseries(0, n*str2double(step_size));
% set the stop time to the current time + 1 step_size
simIn = simIn.setModelParameter('StopTime',num2str((n+1)*str2double(step_size)));
% simulate with the settings
simOut = sim(simIn);
% get the current state
myOperPoint = simOut.get('savePoint');
% apply this state to the SimulationInput object
simIn.InitialState = myOperPoint;
% here example of getting output signals from model
value = simOut.simout.Data(2)
n= n+1;
end
So the idea is to simulate the model while saving its final states, setting these states as the inital states in the SimulatinInput object and simulating the next step.
First, is that approach working for my purpose? For my small test model it seems fine, but could I run in to problems with the big model?
But my main question:
Is there any better approach regarding runtime? Currently simulating one step takes approx. the same time than simulating the whole at once.
I hope the problem / my question is clear.
I would be happy if there is anyone out there who has experience in this area and can provide input and source code if possible.
0 Comments
Answers (1)
Abdolkarim Mohammadi
on 24 Sep 2020
Edited: Abdolkarim Mohammadi
on 24 Sep 2020
You can use MATLAB function block. It is executed on every timestep just like other blocks, and you can return outputs for every step to the MATLAB workspace at the end of the simulation for further processing by connecting it to a To Workspace block. I think it is simpler than your current approach.
3 Comments
See Also
Categories
Find more on Dependency Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!