Clear Filters
Clear Filters

How can I swap states in a simulink "for each" system?

21 views (last 30 days)
James
James on 9 May 2024
Answered: Akshat Dalal on 28 Jun 2024 at 9:34
I am trying to simulate multiple state-space models running in parrallel using a "for each" subsystem. At a given time or when a condition is met, I would like to "swap" the states during this simulation, e.g. reset the integrators so that the states from system 1 are assigned as the initial conditions for system 2 and the states of system 2 are assigned as the initial conditions to system 1. I can get this to happen if I trigger the reset based on time, i.e. with a step function. However, if I try to trigger the reset based on the states themselves there is a gap and the destination states get set to where the origin states were one timestep ago. Is there a way that I can fix this? See attachment.

Answers (1)

Akshat Dalal
Akshat Dalal on 28 Jun 2024 at 9:34
Hi James,
The issue you're facing is likely due to the fact that the state update in your simulation happens in discrete time steps, and the reset condition based on the states themselves introduces a delay.
You could try using Stateflow for implenting the swapping infrastructure. This allows for more complex logic and can help ensure that states are swapped simultaneously without delay.
You can also use a triggered subsystem that activates based on the required condition and swaps the states using a logic defined in a MATLAB Function block.
The logic in the MATLAB Function block could be implemented as follows:
function [state1, state2] = swapStates(state1, state2, condition)
% Swap states if condition is met
if condition
temp = state1;
state1 = state2;
state2 = temp;
end
end

Categories

Find more on Large-Scale Modeling in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!