MATLAB Examples

Sweep Parameter Value and Inspect Simulation Results

This example shows how to change a block parameter value between multiple programmatic simulation runs. Use this technique to determine an optimal parameter value by comparing the output signal data of each run.

The example model sldemo_absbrake uses a Constant block to specify a slip setpoint for an anti-lock braking system. Simulate the model with two different slip setpoint values, 0.24 and 0.25, and compare the output wheel speed of each simulation run.

To store the setpoint value, create a variable in the base workspace. This technique enables you to assign a meaningful name to the value.

Open the example model.


In the model, select View > Model Data.

In the Model Data Editor, select the Signals tab.

Set the Change view drop-down list to Instrumentation. The Log Data column shows that the signals yout (which is a virtual bus) and slp are configured for logging. When you simulate the model, you can collect and later inspect the values of these signals by using the Simulation Data Inspector.

In the Model Data Editor, select the Parameters tab. Set Change view to Design.

In the model, select the Constant block labeled Desired relative slip. The Model Data Editor highlights the row that corresponds to the Constant value parameter of the block.

Use the Value column to set the parameter value to relSlip.

While editing the value, next to relSlip, click the action button (with three vertical dots) and select Create.

In the Create New Data dialog box, set Value to 0.2 and click Create. A variable, whose value is 0.2, appears in the base workspace. The model now acquires the relative slip setpoint from this variable.

Alternatively, you can use these commands at the command prompt to create the variable and configure the block:

relSlip = 0.2;
set_param('sldemo_absbrake/Desired relative slip','Value','relSlip')

At the command prompt, create an array to store the two experimentation values for the relative slip setpoint, 0.24 and 0.25.

relSlip_vals = [0.24 0.25];

Create a Simulink.SimulationInput object for each simulation that you want to run (in this case, two). Store the objects in a single array variable, simIn. Use the setVariable method of each object to identify each of the two experimentation values.

for i = 1:length(relSlip_vals)
    simIn(i) = Simulink.SimulationInput('sldemo_absbrake');
    simIn(i) = setVariable(simIn(i),'relSlip',relSlip_vals(i));

Use the sim function to simulate the model. Optionally, store the output in a variable named simOutputs.

simOutputs = sim(simIn);
[31-Jul-2017 23:21:57] Running simulations...
[31-Jul-2017 23:22:41] Completed 1 of 2 simulation runs
[31-Jul-2017 23:22:43] Completed 2 of 2 simulation runs

The model streams the logged signals, yout and slp, to the Simulation Data Inspector. You can view the signal data in the Simulation Data Inspector.

Compare the output data of the two latest simulation runs.

runIDs = Simulink.sdi.getAllRunIDs();
runResult = Simulink.sdi.compareRuns(runIDs(end-1), runIDs(end));

Plot the difference between the values of the Ww signal (which is an element of the virtual bus signal yout) by specifying the result index 1.

signalResult = getResultByIndex(runResult,1);