6

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.

moumoute6919
  • 2,406
  • 1
  • 14
  • 17
  • 3
    Are you preallocating `results`? Maybe it's growing dynamycally, and that costs time – Luis Mendo Dec 04 '13 at 15:08
  • Yes, results is preallocated before the loop starts. – moumoute6919 Dec 04 '13 at 15:09
  • 1
    Do you really use `results{ii, 1} = numberOfMC`? What's the purpose of that line? `numberOfMC` seems to be a constant – Luis Mendo Dec 04 '13 at 15:10
  • To be exact, values located in the Workspace from the simulation are stored in results. numberOfMC is not really tracked, I put it there for the example. – moumoute6919 Dec 04 '13 at 15:15
  • Well, if your result variables are very large, that could cause a steeper increase of time. Try removing that storage of results and check if it's linear then – Luis Mendo Dec 04 '13 at 15:17
  • I tried removing results, but it doesn't affect the simulation time that much. – moumoute6919 Dec 04 '13 at 16:07
  • 1
    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 Dec 04 '13 at 21:08
  • 1
    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 Dec 05 '13 at 04:22
  • The time steps are variable (Variable-step) because we use a toolbox named TrueTime that must be used with Variable-step. But I don't think the problem is there. When I split the Monte Carlo simulation in 6 batch, the sum of the simulation time is far less than when I run the entire simulation in one shot. – moumoute6919 Dec 05 '13 at 15:59
  • 5
    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 Dec 05 '13 at 16:14
  • 1
    My guess is problems with the memory garbage collector. If after a Simulink run, you store variables in the workspace, try the following: `clear vars_from_Simulink;` `memory;` – tashuhka Feb 12 '14 at 08:56
  • 1
    @m_power If you are actually struggling with this issue, perhaps you could add the minimal relevant code to reproduce this behavior? – Dennis Jaheruddin Apr 02 '14 at 09:31
  • @DennisJaheruddin I use the same kind of approach for running multiple times the same Simulink model. I'm curious at what is causing the OP simulation increase, because I might in the future get the same problems with multiple runs. – m_power Apr 02 '14 at 20:36

1 Answers1

1

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?

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122