different result for simulink in for loop
46 次查看(过去 30 天)
显示 更早的评论
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
2 个评论
Paul
2024-11-19,4:43
Hi Youngjin,
I don't have an answer becasue I can't follow the logic. But I can say that the code will be much better, more flexible, easier to understand, and easier to process outputs if you switch to using the Simulink.SimulationInput paradigm.
回答(1 个)
Rahul
about 7 hours 前
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Event Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!