different result for simulink in for loop
58 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Youngjin
el 19 de Nov. de 2024 a las 4:18
Comentada: Paul
el 21 de Nov. de 2024 a las 3:50
greetings!
i generated code that play simulink in for loop like belows
but it has different results in for = 2 when for 1:3 and for 2:2
so i set new variables but it remains same.
how can i solve this?
optionsetting = {
[1, 2, 0], % FTP phase1
[1, 3, 0], % FTP phase2
[1, 2, 0], % FTP Phase3
[1, 3, 0], % FTP phase4
[2, 0, 0], % Hwy
[3, 1, 2], % US06 Phase1
[3, 2, 3], % US06 Phase2
[3, 3, 4]} % US06 Phase3
result_total = [];
variables_to_keep = who;
%%
for iii = 1 : 2
current_vars = who;
vars_to_clear = setdiff(current_vars, [{'iii', 'variables_to_keep'}, variables_to_keep']);
if ~isempty(vars_to_clear)
clear(vars_to_clear{:});
end
sim_index = iii;
soc_init = SOC_setting{sim_index}(1);
soc_end = SOC_setting{sim_index}(2);
on_line = onoffline_setting{sim_index}(1);
off_line = onoffline_setting{sim_index}(2);
option1 = optionsetting{sim_index}(1);
option2 = optionsetting{sim_index}(2);
option3 = optionsetting{sim_index}(3);
Select_driving_mode
Main
Lck_chg = (iii == 1 || iii == 3);
if iii == 6
OOL_factor = 1.2
elseif iii == 8
OOL_factor = 1.4;
else
OOL_factor = 1;
end
if option1 ==3
Shift_Gain_L = [1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00]*1.2;
Shift_Gain_H = [1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00]*1.35;
TCU_input
else
Shift_Gain_L = [1 1 1 1 1 1 1 1].*1.2;
Shift_Gain_H = [1 1 1 1.00 1.00 1.00 1.00 1.00]*1.2;
TCU_Input
end
out = sim('Simulater');
Fuel_Auto_ver2;
if iii==1
result_total = result_names;
end
clear out
result_total = [result_total result_values];
end
3 comentarios
Paul
el 21 de Nov. de 2024 a las 3:50
I don't understand why Simulink.SimulationInput won't work, but o.k.
Regardless, I sincerely doubt that the approach that looks like it's trying to clear different variables from the base workspace each time through the loop is the way go.
If you really must run from the base workspace (as opposed to inside a function), then it seems like it would be better to have a function that is called from the base workspace and returns the parameters needed to run the simulation. And if you want to be super careful, clear those variables from the base workspace prior to next call to sim. That way you know exactly what parameters are used for each call to sim.
Respuesta aceptada
Rahul
el 20 de Nov. de 2024 a las 4:57
Hi Youngin,
The issue described is likely caused by how variables or states persist between Simulink simulations. Even though you are trying to clear variables using clear, Simulink simulations and MATLAB workspace handling can lead to unintended interactions. Here's how you can troubleshoot and resolve this issue:
Ensure Full Reset of Simulink States
Simulink retains internal states, especially when blocks have states or memory. To ensure a full reset, you can use the ‘sim’ command with the 'SaveFinalState' and 'LoadInitialState' options, which explicitly sets the initial state for each iteration.
Isolating Variables
The use of clear to remove variables between iterations is a good approach but might not catch all dependencies.
To ensure variables aren't carried over, you can use a separate function or script for each iteration. This prevents variable carryover between loops.
Reset Persistent Simulink Block State
If your Simulink model uses blocks with states (e.g., integrators or memory blocks), you can ensure sure their states are reset between iterations through the following steps:
- Using the ‘InitialCondition’ parameter to explicitly reset states.
- Checking for any "persistent" behavior in blocks.
- Reset these blocks by stopping and restarting the simulation programmatically.
Use Clean Workspace for Simulink
Run Simulink in an isolated workspace to prevent MATLAB workspace interference by setting the 'SrcWorkspace' parameter in the sim function to ‘current’.
Check Dependency on Simulation Order
If the issue persists, the results may depend on the order in which simulations are run. For instance, global variables or states in your Simulink model might be carrying over. To isolate, you can:
- Explicitly reinitialize these variables or states in your Simulink ‘InitFcn’ callback.
- Avoid using global or persistent variables, unless necessary.
Here’s how you can restructure your code in MATLAB:
variables_to_keep = who;
for iii = 1:2
% Clear all variables except loop index and baseline variables
current_vars = who;
vars_to_clear = setdiff(current_vars, [{'iii', 'variables_to_keep'}, variables_to_keep']);
if ~isempty(vars_to_clear)
clear(vars_to_clear{:});
end
% Set simulation parameters
sim_index = iii;
soc_init = SOC_setting{sim_index}(1);
soc_end = SOC_setting{sim_index}(2);
on_line = onoffline_setting{sim_index}(1);
off_line = onoffline_setting{sim_index}(2);
option1 = optionsetting{sim_index}(1);
option2 = optionsetting{sim_index}(2);
option3 = optionsetting{sim_index}(3);
% Log debug info
fprintf('Starting simulation %d\n', iii);
fprintf('SOC_init: %f, SOC_end: %f, Option1: %d\n', soc_init, soc_end, option1);
% Run simulation
set_param('Simulator', 'LoadInitialState', 'off');
set_param('Simulator', 'SaveFinalState', 'off');
out = sim('Simulator', 'SrcWorkspace', 'current');
% Process results
Fuel_Auto_ver2;
if iii == 1
result_total = result_names;
end
result_total = [result_total, result_values];
% Clear simulation outputs to avoid contamination
clear out;
end
To know more about the usage of various functions used in the above code, refer to the documentation links mentioned below:
- Set Simulink parameter value using 'set_param'
- Run and script programmatic simulations of Simulink models using 'sim'
Best!
2 comentarios
Paul
el 21 de Nov. de 2024 a las 3:41
What is the evidence that "Simulink retains internal states"? I've never seen any evidence of that. If it were true I think it would cause massive problems, so I'd be quite curious to see an example that exhibits this behavior.
The 'SrcWorkspace' has been not recommended since R2009. I'm surprised it's still accepted.
Más respuestas (0)
Ver también
Categorías
Más información sobre Event Functions en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!