Simulink build a loop and define intial conditions

2 views (last 30 days)
I want to build a loop in simulink with some intital conditions, and then for the next time step use the outputs as the inputs. How do I do this?

Answers (1)

Jon on 16 Nov 2021
Edited: Jon on 16 Nov 2021
In general you should be able to run your simulation iteratively by starting the simulation programatically using the MATLAB sim function (if you're not already familiar with the sim function, type doc sim on your command line to read the documentation)
So you would make a loop, something like this
for k = 1:numIterations
simout = sim('myModel',...)
% now do something with the output variables ...
% for example assign base workspace variable that are used as inputs in
% next loop
a = simout....
b = simout. ...
You may want to look at the documentation for the fast restart option, to avoid compiling the Simulink model with each iteration
Exactly how you assign the outputs to the inputs depends upon what kind of values you are changing. However if they are values of constant blocks or mask parameter values that are assigned to base workspace variables it is as simple as reassigning these base workspace values (as I indicated in the example above)

Sign in to comment.


Find more on Modeling 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!