Different simulation behavior if simulation is called from within function vs. from script

1 visualización (últimos 30 días)
Hello,
I have a simulink model which uses a variety of parameters from the workspace. If I execute the exact same piece of code (initialization of parameters+ run simulation) from within a function vs. from a script the simulation results are (slightly) different which leads to the simulation running into a stopping condition if called from the script, but finishing smoothly if called from within the function.
The two different pieces of code are like this:
params_val = goodRuns_all(ind_choosen,:);
%% Version 1 from script
parameters = params_val;
init_script;
MP = setParams(MP, 1, parameters(8), parameters(10), parameters(11), parameters(12), parameters(13));
simulation_length = 30;
options = simset('SrcWorkspace','current');
warning('off','all')
simOut = sim('model_name', [],options);
warning('on','all')
%% Version 2 with function call
simOut_func = runSim(params_val);
%% Function declaration
function simOut = runSim(parameters)
init_script;
MP = setParams(MP, 1, parameters(8), parameters(10), parameters(11), parameters(12), parameters(13));
simulation_length = 30;
options = simset('SrcWorkspace','current');
warning('off','all')
simOut = sim('model_name', [],options);
warning('on','all')
end
Also, if I first execute the code from the script and then (without a clear) run the function, also the simulation called from the function runs into a stopping condition whereas if I directly call the function it does not.
What could be the reason for the changed behavior?

Respuestas (1)

Steve Miller
Steve Miller el 17 de Nov. de 2021
I believe the 'SrcWorkspace','current' is different if you call from within a function or a script.
See this answer.
--Steve
  1 comentario
Elsa Bunz
Elsa Bunz el 30 de Nov. de 2021
Sorry for not responding and thanks a lot for your answer and time!
In the meantime I continued to search for the reason and actually the simulation results don't seem to be different but the difference comes from a delay before my stopping condition.
This is how that part (and the settings of the delay) of my model looks like:
The scope for the call from the script looks as follows:
whereas for the function call it is like this:
Therefore, I guess somehow the sampling time seems to be affected causing this effect? Something in my script call seems to influence this, because if I first run the script and then the function without a clear inbetween then also the function version runs into the stopping condition. I didn't find where exactly it comes from, but I just solved the problem by removing the delay.
However, if you have any hint on where this could come from I'd be very happy to hear it!

Iniciar sesión para comentar.

Categorías

Más información sobre General Applications en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by