Random Variable in Parallel Simulation

조회 수: 4 (최근 30일)
Baris Ciftci
Baris Ciftci 2018년 5월 19일
답변: Rahul Kumar 2018년 8월 29일
Hi,
In my Simulink model, I have an Embedded MATLAB Function which generates random variables by rand command periodically during the simulation interval. Now, I want to run multiple simulations with parsim command. However, the same random variables are generated in different (multiple) runs of the model. I tried to add rng('shuffle') command into Embedded MATLAB Function, but it gave error saying it is not supported to use 'shuffle' command in Embedded MATLAB Function. Then, I tried to add rng(seed) command in Embedded MATLAB Function. rng(seed) works fine in Embedded MATLAB Function if the seed is a static number (but of course generates the same random variables since the seed integer is the same). However, if I try to change it in each run (by 'From Workspace' block), it again gives error complaining about this matrix value not having a time value etc.
Do you have an advice to have different random variable realizations in Embedded MATLAB Function block in each parsim loop?
Thanks in advance,
Baris

채택된 답변

Rahul Kumar
Rahul Kumar 2018년 8월 29일
Here is an example to use different random seeds for each simulation
in(1:12) = Simulink.SimulationInput(mdlName);
for idx = 1:numWorkers
in(idx) = in(idx).setPreSimFcn(@(x) PreSimFcnCallback(idx));
end
function PreSimFcnCallback(seed)
rng(seed);
end
Please note that the example above is looping over 'numWorkers' the number of workers in the parallel pool, instead of the number of simulations, since it is sufficient to set the seed only once on each worker.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Manual Performance Optimization에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by