Running two simulink model in close loop

6 views (last 30 days)
I want to run two simulink models in loop. When the simulation starts, the first model should send states at time 't' to other simulink model. The other simulink model will take input at time 't' from first model and run its own calculation for time 't+20' seconds. That is it should be able to create a time trajectory (prediction) for the first model. The results of model two should be feedback to model one as input and the cycle should go on. I cannot find any direct command for this. Also, matlab function block inside simulink doesnt support sim command.
  1 Comment
Dr. JANAK TRIVEDI
Dr. JANAK TRIVEDI on 1 Feb 2023
One way to achieve this is to use a "for" loop in MATLAB and run the simulations for each time step, updating the inputs and outputs of each model for each iteration. After the first simulation is complete, you can store the results in a variable, and use it as the input for the next simulation.
Here is a general outline of the steps you can follow:
  1. Create two Simulink models and save them.
  2. In MATLAB, create a "for" loop with the desired number of iterations, where each iteration corresponds to one time step.
  3. Within the loop, set the inputs of the first model using the previous outputs of the second model or the initial conditions if it is the first iteration.
  4. Use the "sim" command to run the first model and store the outputs in a variable.
  5. Use the outputs of the first model as inputs to the second model, and run the simulation using the "sim" command.
  6. Store the outputs of the second model in a variable, which will be used as inputs for the next iteration.
  7. Repeat the process for the desired number of time steps.
You can also use Simulink Data Transfer blocks to pass data between the two models.

Sign in to comment.

Answers (1)

Aishwarya Shukla
Aishwarya Shukla on 3 Mar 2023
I think your problem will get resolved by the solution given by @Dr. JANAK TRIVEDI in comments, still if you need further assistance you can ask!

Categories

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!