Run rng Function Before Each Simulation Run

조회 수: 1 (최근 30일)
Baris Ciftci
Baris Ciftci 2018년 5월 22일
댓글: Elliott Rachlin 2018년 10월 23일
Hi,
I would like to run a set of Simulink simulations by parsim function with different random number generator setting in each consecutive run. I tried the way below but it does not work:
numofSims=4;
simIn(1:numofSims)=Simulink.SimulationInput(model);
for i=1:numofSims
simIn(i).PreSimFcn=@(x) rng(i);
end
simOut=parsim(simIn, 'TransferBaseWorkspaceVariables', true, 'showProgress', true);
It gives the error:
'Error executing ''PreSimFcn'' on SimulationInput object. Caused by: Expected input to be one of these types:
Simulink.SimulationInput
Instead its type was struct.'
How can I achieve it? In Simulink model I have an Embedded MATLAB Function block and in that block I call rand function to generate predictable (due to pre-run rng function) random number.
Thanks in advance,
Baris
  댓글 수: 1
Elliott Rachlin
Elliott Rachlin 2018년 10월 23일
Similar question: I am creating a SimEvents model programmatically and repeatedly running it multiple times from a script that executes the following sequence: rng(x);sim(xxx) where x is the number of the simulation being run (and xxx is all the parameters that sim requires). I find that all simulations give the same result, apparently ignoring the rng(x) command given just prior to each simulation made with sim(xxx). Does sim(xxx) ignore the current seed and reinitialize the random number generator? If so, how can I set a seed in a SimEvents model (which is initiated by sim(xxx))?

댓글을 달려면 로그인하십시오.

채택된 답변

Rahul Kumar
Rahul Kumar 2018년 8월 29일
The PreSimFcn expects a SimulationInput object to be returned (in case any output is returned) and in this case 'rng' returns a struct causing the PreSimFcn to error out. You can achieve what you are looking for by defining a local function which does not return any output
numofSims=4;
simIn(1:numofSims)=Simulink.SimulationInput(model);
for i=1:numofSims
simIn(i).PreSimFcn=@(x) seedRNG(i);
end
simOut=parsim(simIn, 'TransferBaseWorkspaceVariables', true, 'showProgress', true);
function seedRNG(seedValue)
rng(seedValue)
end

추가 답변 (0개)

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by