Documentation Center

  • Trial Software
  • Product Updates

Parallel Simulations Using Parfor: Parameter Sweep in Normal Mode

This example shows how you can run multiple Simulink® simulations of a Monte Carlo study in parallel by using Parallel Computing Toolbox™. This involves five main steps:

  1. Start MATLAB® workers on your machine

  2. Set up data required for the multiple simulations


  4. Plot results of multiple simulations

  5. Close MATLAB workers

Model Overview


In this Monte Carlo study, the damping coefficients of the front suspension system are varied to study their effect on the vehicle dynamics. Parallel Computing Toolbox is used to speed up these multiple simulations, as illustrated below.

mdl               = 'sldemo_suspn_3dof';
isModelOpen       = bdIsLoaded(mdl);

Step 1: Start MATLAB Workers on Your Machine


apool = gcp;
if isempty(apool)
    apool = parpool('local');

Step 2: Set up Data Required for the Multiple Simulations

Create a parameter vector for the front suspension damping coefficient that will be used in the Monte Carlo study. Also preallocate an array of SimulationOutput objects in the Simulink package that will be used to collect the simulation results in step 3.

Cf_sweep            = Cf*(0.05:0.1:0.95);
iterations          = length(Cf_sweep);
simout(iterations)  = Simulink.SimulationOutput;


Run the multiple simulations corresponding to the different parameters within a parfor loop. The simsim command returns all the simulation data as a SimulationOutput object of the Simulink package. Note that the simulation results are collected in the simout variable, which was preallocated in step 2.

parfor idx = 1:iterations
    set_param([mdl '/Road-Suspension Interaction'],'MaskValues',...

Step 4: Plot Results of Multiple Simulations

Plot the vertical vehicle displacement from the different simulations to see how varying the damping coefficient affected the vehicle dynamics. Use the get method of the SimulationOutput object to obtain the time and signal data contained in each element of simout.

legend_labels = cell(1,iterations);
for i = 1:iterations
        si = simout(i);
        hold all
xlabel('Time (s)');
ylabel('Vehicle vertical displacement (m)');

Step 5: Close MATLAB Workers


    close_system(mdl, 0);
Was this topic helpful?