Syncing iterations of the loop and the time step of the simulation

4 次查看(过去 30 天)
Dear all,
I am using a For loop in my simulink model. Let's say simple factorial function.
It receives a number(for instance 4) and returns the factorial of the input(24).
1) The main question is
The whole loops (i=1,...4) occurs in one single time stamp of the simulation. Also at the MATLAB document I found a note that
"Simulation time does not advance during iterative executions of a While/(For) Iterator Subsystem block. Nevertheless, blocks in the subsystem treat each iteration as a time step. .."
This is not what I want.
I want in a single step of my simulation i turns to 1, then in the next step it becomes 2, and ,....
How I can do that?
(The type of the solver selection is Fixed-Step but the simulation time is 10 seconds, but we do not want to change the simulation time starting and ending)
2) Does the functions that we wrote as MATLAb function that has a loop inside the code, treats similarly?
I mean the whole code is run once in one single step.
If yes, How I can change the code so each iteration performs in each single step of the simulation.
3) When I do not select "states when starting" to "reset", it returns zero as an output(I already set initial value of the delay to 1). I do not undrestand why?
Thanks...

回答(1 个)

Namnendra
Namnendra 2024-7-15,16:30
Hi Hani,
Let's address your questions one by one:
1. Iterative Execution in Simulink
If you want the iterations of a loop to occur across multiple simulation time steps rather than within a single time step, you need to structure your Simulink model differently. One way to achieve this is by using a combination of a Stateflow chart or a Discrete-Time Integrator block along with a MATLAB Function block.
Here's an example of how you can implement a factorial calculation across multiple time steps:
Using Stateflow:
1. Create a Stateflow Chart:
- Add a Stateflow chart to your model.
- Define states and transitions to handle the iterations.
- Use a counter to keep track of the current iteration and update the factorial value at each time step.
2. Stateflow Chart Example:
- Define two states: `Idle` and `Compute`.
- In the `Idle` state, initialize the counter and factorial value.
- Transition to the `Compute` state where you update the factorial value and the counter at each time step.
% Stateflow Chart Example
% State: Idle
entry:
counter = 1;
factorial = 1;
% Transition from Idle to Compute
[condition to start computation]
% State: Compute
during:
if counter <= input_number
factorial = factorial * counter;
counter = counter + 1;
else
% Computation complete
% Transition back to Idle or another state
end
Using Discrete-Time Integrator and MATLAB Function Block:
1. Add a Discrete-Time Integrator Block:
- Use this block to create a counter that increments at each time step.
2. Add a MATLAB Function Block:
- Use this block to calculate the factorial based on the counter value.
3. Connect Blocks:
- Connect the output of the Discrete-Time Integrator to the input of the MATLAB Function block.
- Use the MATLAB Function block to compute the factorial incrementally.
function factorial = factorial_step(counter, input_number)
persistent fact;
if isempty(fact)
fact = 1;
end
if counter <= input_number
fact = fact * counter;
end
factorial = fact;
end
2. MATLAB Function Block with Loop
MATLAB Function blocks in Simulink execute the entire code within a single time step. If you want to perform each iteration of a loop in separate time steps, you need to manage the loop externally using a counter, as described above.
3. Initial State and Reset Behavior
If you are using a Delay block or any block with a state, and you do not select "reset" for the "states when starting," the block will retain its state from the previous simulation run. This might result in unexpected outputs if the state is not properly initialized.
To ensure correct behavior:
- Set Initial Conditions: Make sure the initial conditions are correctly set in the block parameters.
- Use Reset Logic: If needed, add logic to reset the state at the beginning of the simulation.
Example Model
Here's a simple example of how you can implement the solution using a Discrete-Time Integrator and a MATLAB Function block:
1. Discrete-Time Integrator Block:
- Set the `Initial condition` to 1.
- Set the `Sample time` to the desired step size.
2. MATLAB Function Block:
- Use the following code:
function factorial = factorial_step(counter, input_number)
persistent fact;
if isempty(fact)
fact = 1;
end
if counter <= input_number
fact = fact * counter;
else
fact = fact; % Hold the value once computation is complete
end
factorial = fact;
end
3. Connect Blocks:
- Connect the output of the Discrete-Time Integrator to the input of the MATLAB Function block.
- The output of the MATLAB Function block will give you the factorial value incrementally at each time step.
By structuring your model this way, you ensure that each iteration of the loop occurs at a separate time step, and you can control the simulation time as desired.
I hope above information helps you.
Thank you.

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by