Contenido principal

Run Parameter Sweep Using Parallel Simulations

R2026b

This example shows how to run multiple simulations of a Monte Carlo study in parallel using parsim and Parallel Computing Toolbox™. Parallel execution leverages the multiple cores of your host machine to speed up multiple simulations. These simulations can also be run in parallel on compute clusters using MATLAB® Parallel Server™. If you do not have Parallel Computing Toolbox or MATLAB Parallel Server, the simulations in this example run serially.

Explore Model

The model sldemo_suspn_3dof simulates vehicle dynamics based on the interaction between the road and the vehicle suspension for different road profiles. The model captures vehicle dynamics in three degrees of freedom: vertical displacement, roll, and pitch. The Signal Editor block named Road Profiles stores measured road profile data for the left and right tires as different test groups. The Road-Suspension Interaction subsystem calculates the suspension forces on the vehicle at the four tire locations based on the road data and the current vehicle state. The Body Dynamics subsystem uses these forces and the resulting pitch and roll moments to calculate the vehicle motion in each of the three degrees of freedom.

mdl = "sldemo_suspn_3dof";
open_system("sldemo_suspn_3dof.slx");

To log the vertical vehicle displacement over time, the signal connected to the vertical_disp output port of the Body Dynamics subsystem is marked for signal logging, as indicated by the logging badge. To examine the logging settings, right-click the signal and select Properties. In the Signal Properties dialog box, the Log signal data parameter is selected and the signal is configured to log data using the custom logging name vertical_disp. For more information, see Save Signal Data Using Signal Logging.

Prepare Parameter Inputs

To inspect the impact of the front suspension on vehicle dynamics, run multiple simulations, each with a different front suspension damping coefficient value. The model uses a data dictionary to store parameter values. For more information, see Simulink.data.Dictionary. Use the parameter Cf from the data dictionary (dd) to calculate the sweep values for the front suspension damping coefficient as percentages of the design value ranging from 5% to 95% in increments of 10%. Store the sweep values in a variable, Cf_sweep, in the base workspace.

Cf_sweep = dd.Cf*(0.05:0.1:0.95);

The number of simulations to run is equal to the number of sweep values.

numSims = length(Cf_sweep);

Use vectorization and a for loop to:

  1. Preallocate an array of Simulink.SimulationInput objects for the model, one per simulation, and store the objects as an array in the variable in.

  2. Specify the sweep value for the mask parameter Cf by calling setBlockParameter on each SimulationInput object.

in(1:numSims) = Simulink.SimulationInput(mdl);

for idx = 1:numSims
    in(idx) = setBlockParameter(in(idx),mdl+"/Road-Suspension Interaction","Cf",num2str(Cf_sweep(idx)));
end

Note that specifying the block parameter on the SimulationInput object does not apply it to the model immediately. The software applies the specified value during simulation and reverts to the original value, if possible, after the simulation finishes.

Run Simulations in Parallel

To execute the simulations in parallel, use parsim on the array of SimulationInput objects. The parsim command returns an array of Simulink.SimulationOutput objects, stored in the variable out, that contain the signal data and metadata. To print the progress of the simulations, specify the ShowProgress name-value argument as on.

out = parsim(in,ShowProgress="on");
[12-Aug-2026 10:14:23] Checking for availability of parallel pool...
Starting parallel pool (parpool) using the 'Processes' profile ...
12-Aug-2026 10:15:35: Job Running. Waiting for parallel pool workers to connect ...
12-Aug-2026 10:16:36: Job Running. Waiting for parallel pool workers to connect ...
12-Aug-2026 10:17:36: Job Running. Waiting for parallel pool workers to connect ...
Connected to parallel pool with 10 workers.
[12-Aug-2026 10:18:46] Starting Simulink on parallel workers...
[12-Aug-2026 10:19:25] Configuring simulation cache folder on parallel workers...
[12-Aug-2026 10:19:26] Loading model on parallel workers...
[12-Aug-2026 10:19:48] Running simulations...
[12-Aug-2026 10:20:32] Completed 1 of 10 simulation runs
[12-Aug-2026 10:20:32] Received simulation output (size: 3.06 MB) for run 1 from parallel worker.
[12-Aug-2026 10:20:32] Completed 2 of 10 simulation runs
[12-Aug-2026 10:20:32] Received simulation output (size: 3.06 MB) for run 2 from parallel worker.
[12-Aug-2026 10:20:32] Completed 3 of 10 simulation runs
[12-Aug-2026 10:20:32] Received simulation output (size: 3.06 MB) for run 3 from parallel worker.
[12-Aug-2026 10:20:32] Completed 4 of 10 simulation runs
[12-Aug-2026 10:20:32] Received simulation output (size: 3.06 MB) for run 4 from parallel worker.
[12-Aug-2026 10:20:32] Completed 5 of 10 simulation runs
[12-Aug-2026 10:20:32] Received simulation output (size: 3.06 MB) for run 5 from parallel worker.
[12-Aug-2026 10:20:32] Completed 6 of 10 simulation runs
[12-Aug-2026 10:20:32] Received simulation output (size: 3.06 MB) for run 6 from parallel worker.
[12-Aug-2026 10:20:32] Completed 7 of 10 simulation runs
[12-Aug-2026 10:20:32] Received simulation output (size: 3.06 MB) for run 7 from parallel worker.
[12-Aug-2026 10:20:32] Completed 8 of 10 simulation runs
[12-Aug-2026 10:20:32] Received simulation output (size: 3.06 MB) for run 8 from parallel worker.
[12-Aug-2026 10:20:32] Completed 9 of 10 simulation runs
[12-Aug-2026 10:20:32] Received simulation output (size: 3.06 MB) for run 9 from parallel worker.
[12-Aug-2026 10:20:32] Completed 10 of 10 simulation runs
[12-Aug-2026 10:20:32] Received simulation output (size: 3.06 MB) for run 10 from parallel worker.
[12-Aug-2026 10:20:32] Cleaning up parallel workers...

When you run multiple simulations using parsim, the software captures errors so that subsequent simulations can continue to run. Errors are stored in the ErrorMessage property of the SimulationOutput object.

Plot Results

To see how varying the damping coefficient affects the vehicle dynamics, plot the vertical vehicle displacements.

  1. Access the SimulationOutput object for each simulation. The SimulationOutput object contains a property for each type of logged data.

  2. Access the logged signal data using dot notation. Within each SimulationOutput object, signal logging data and metadata are grouped in a Simulink.SimulationData.Dataset object with the default name of logsout.

  3. Access the vertical_disp signal using get.

  4. Access the timeseries object that contains the time and signal data for vertical_disp using the Values property of the vertical_disp signal.

  5. Plot the results for each simulation.

legend_labels = cell(1,numSims);
for idx = numSims:-1:1
    simOut = out(idx);
sigLoggingData = simOut.logsout;
    sig = get(sigLoggingData,"<vertical_disp>");
    ts = sig.Values;

plot(ts);
    legend_labels{idx} = "Cf =  "+num2str(Cf_sweep(idx));         
    hold on
end

title("Response of a 3-DoF Suspension Model")
xlabel("Time (s)");
ylabel("Vehicle vertical displacement (m)");
legend(legend_labels,Location="NorthEastOutside");

Figure contains an axes object. The axes object with title Response of a 3-DoF Suspension Model, xlabel Time (s), ylabel Vehicle vertical displacement (m) contains 10 objects of type line. These objects represent Cf = 125, Cf = 375, Cf = 625, Cf = 875, Cf = 1125, Cf = 1375, Cf = 1625, Cf = 1875, Cf = 2125, Cf = 2375.

Close MATLAB Workers

Close the parallel pool.

delete(gcp("nocreate"));
Parallel pool using the 'Processes' profile is shutting down.

See Also

| |

Topics