6
votes

I'm running Monte Carlo simulation for a Simulink model with a Matlab script that looks more or less like this :

model = 'modelName';

load_system(model)

for ii = 1 : numberOfMC
    % Some set_param...
    % Some values are set

    sim(model);
    results{ii, 1} = numberOfMC;
    % ect...
end
close_system(model,0);

As the number of Monte Carlo trials increase, the time of one simulation increases as well like n^2.

Is there a simple explanation for that and is there a solution to have something linear in time?

Thank you!

EDIT:

When I split my simulation in 6 batchs, and I run them in series, the sum of the simulation times is far less than when I run the entire simulation ine one shot.

1
Are you preallocating results? Maybe it's growing dynamycally, and that costs timeLuis Mendo
Do you really use results{ii, 1} = numberOfMC? What's the purpose of that line? numberOfMC seems to be a constantLuis Mendo
I'm highly interested in knowing the answer to that kind of problem as well (memory problem of having the 'sim(model)' inside a loop, loaded before the loop)m_power
Is there anything about the parameter changes that makes the system stiff, and hence the solver is having problems? How many time steps are being taken by the model each time through the loop?Phil Goddard
My bet would be memory issues, if you want to eliminate this see whether the problem still occurs if you don't store the result in the first place, simply remove this line: results{ii, 1} = numberOfMC;. Also confirm that you don't have other growing variables or that you accidentally make the input more complex as you go. It is probably not relevant, does the time also increase like this if you do all simulations in reversed order? Or if you do the full amount of iterations, but each time with exactly the same input?Dennis Jaheruddin

1 Answers

1
votes

As it seems that there is a limit to what one can do without feedback from the asker I will just post my comment as an answer:

My bet would be memory issues, if you want to eliminate this see whether the problem still occurs if you don't store the result in the first place, simply remove this line:

results{ii, 1} = numberOfMC;

Also confirm that you don't have other growing variables or that you accidentally make the input more complex as you go. It is probably not relevant, does the time also increase like this if you do all simulations in reversed order? Or if you do the full amount of iterations, but each time with exactly the same input?