Tune and Experiment with Block Parameter Values

As you construct a model you can experiment with block parameters, such as the coefficients of a Transfer Fcn block, to help you decide which blocks to use. You can simulate the model with different parameter values, and capture and observe the simulation output.

You can change the values of most numeric block parameters during a simulation. To observe the effects, you can visualize the simulation output in real time. This technique allows you to quickly test parameter values while you develop an algorithm. You can visually:

  • Tune and optimize control parameters.

  • Calibrate model parameters.

  • Test control robustness under different conditions.

When you begin a simulation, Simulink® first updates the model diagram. This operation can take time for larger models. To test parameter values without repeatedly updating the model diagram, you can tune the parameter values during a single simulation run.

Alternatively, to avoid updating the model diagram, use Fast Restart. For more information about Fast Restart, see Get Started with Fast Restart.

If you cannot visually analyze the simulation output in real time, or if you must run many simulations, consider using a programmatic approach to sweeping parameter values. You can capture the simulation output data and perform analysis later. For more information, see Optimize, Estimate, and Sweep Block Parameter Values.

For basic information about accessing and setting block parameter values, see Set Block Parameter Values.

Iteratively Adjust Block Parameter Value Between Simulation Runs

This example shows how to rapidly prototype a model by changing block parameter values between simulation runs. You can experiment with parameter values and observe simulation results to help you decide which blocks to use and how to build your model.

The example model sldemo_fuelsys represents the fueling system of a gasoline engine. A subsystem in the model, feedforward_fuel_rate, calculates the fuel demand of the engine by using the constant number 14.6, which represents the ideal (stoichiometric) ratio of air to fuel that the engine consumes. Two blocks in the subsystem use the number to set the values of parameters.

Suppose that you want to change the design value of the ideal air-to-fuel ratio from 14.6 to 17.5 to observe the effect on the fuel demand. To store the design value in the model, you can modify the value in the block dialog boxes. Alternatively, you can store the value in a variable with a meaningful name, which allows you to reuse the value in the two blocks.

To observe the change in simulation outputs by changing the value in the block dialog boxes:

  1. Open the example model.


  2. Set the model simulation time from 2000 to 50 for a faster simulation.

  3. In the model, open the Scope block dialog box.

  4. Simulate the model. Resize the window in the Scope block dialog box to see all of the simulation results.

    The scope display shows that throughout the simulation, the fuel signal oscillates between approximately 0.9 and 1.6. The air_fuel_ratio signal quickly climbs to 15 without overshoot.

  5. Navigate to the target subsystem.


  6. Open the block parameter dialog box for the Constant block labeled rich.

  7. Change the Constant value parameter from 1/(14.6*0.8) to 1/(17.5*0.8).

  8. Similarly, change the Constant value parameter of the block labeled normal from 1/14.6 to 1/17.5.

  9. Simulate the model.

    The scope display shows that the signals now respond differently.

To replace the literal values in the block dialog boxes with a numeric variable:

  1. In the block dialog box for the block labeled normal, set Constant value to 1/mixture and click OK.

  2. In the block dialog box for the block labeled rich, set Constant value to 1/(mixture*0.8) and click Apply.

  3. Right-click the expression 1/(mixture*0.8) and select Create Variable > mixture.

  4. In the Create New Data dialog box, set Value to 17.5 and click Create. In the Data properties dialog box, click OK.

    The numeric variable mixture appears in the base workspace with value 17.5. Between simulation runs, you can change the value of mixture in the base workspace instead of changing the parameter values in the block dialog boxes.

Tune Block Parameter Value During Simulation

This example shows how to observe the effect of changing a block parameter value during a simulation. This technique allows you to avoid updating the model diagram between simulation runs and to interactively test and debug your model.

The example model sldemo_fuelsys contains a Constant block, Throttle Command, that represents the throttle command. To observe the effect of increasing the magnitude of the command during simulation:

  1. Open the example model.


  2. In the model, open the Scope block dialog box.

  3. Begin a simulation.

    The model is configured to simulate 2000 seconds. During the simulation, the values of the fuel and air_fuel_ratio signals appear on the scope graph in real time.

  4. In the model, when the status bar indicates approximately 1000 (1000 seconds), click the Pause button to pause the simulation.

  5. In the scope display, the fuel graph plots the simulation output prior to the pause time.

  6. Open the block dialog box for the block labeled Throttle Command. Change the value of the Output values parameter from [10 20 10] to [10 30 10] and click OK.

  7. Click the Step Forward button to advance the simulation step by step. Click the button about 15 times or until you see a change in the fuel graph in the scope display.

    The plot of the signal fuel indicates a sharp increase in fuel demand that corresponds to the increased throttle command.

  8. In the model, resume the simulation by clicking the Continue button .

    The scope display shows the significant periodic increase in fuel demand, and the periodic reduction in the air-to-fuel ratio, throughout the rest of the simulation.

During the simulation, you must update the model diagram after:

  • You change the value of a workspace variable.

  • You use the function set_param to change the value of a block parameter.

For more information about updating the model diagram, see Update Diagram and Run Simulation.

Prepare for Parameter Tuning and Experimentation

  • Consider using workspace variables to set block parameter values.

    To access the value of a block parameter, such as the Constant value parameter of a Constant block, you must navigate to the block in the model and open the block dialog box, search for the block by using the Model Explorer, or use the function set_param at the command prompt.

    Alternatively, if you set the block parameter value by creating a workspace variable, you can change the value of the variable by using the command prompt, the MATLAB® Workspace browser, or the Model Explorer. You can also create a variable to set the same value for multiple block parameters. When you change the variable value, all of the target block parameters use the new value. For more information about accessing and setting block parameter values, see Set Block Parameter Values.

  • Learn how to visualize simulation output.

    To observe simulation output in real time while you tune block parameter values, you can use blocks in a model such as the Scope block. You can also capture simulation output at the end of a simulation run, and view the data in the Simulation Data Inspector. For more information, see Decide How to Visualize Simulation Data.

  • Consider specifying value ranges for block parameters that you expect to tune during simulation.

    If you expect another person to use your model and tune the parameter, you can control the allowed tuning values by specifying a range. Also, it is a best practice to specify value ranges for all fixed-point block parameters that you expect to tune. To specify block parameter value ranges, see Specify Minimum and Maximum Values for Block Parameters.

  • Learn how to control simulation duration and pace.

    A simulation run can execute so quickly that you cannot tune block parameter values. Also, if you want to change a parameter value at a specific simulation time, you must learn to control the simulation pace. You can configure the simulation to run for a specific duration or to run forever, and pause and advance the simulation when you want to. For more information about controlling simulation execution, see Simulate a Model Interactively. To programmatically control simulations, see Control Simulations Programmatically.

Interactively Tune Using Dashboard Blocks

You can tune block parameter values by adding blocks from the Dashboard library to your model. Dashboard blocks allow you to adjust the parameter values of other blocks, and to observe simulation output in real time, by interacting with knobs, switches, and readouts that mimic the appearance of industrial controls. You can interact with the Dashboard blocks without having to locate the target block parameters in the model. For more information, see Tune and Visualize Your Model with Dashboard Blocks.

Which Block Parameters Are Tunable During Simulation?

Nontunable block parameters are parameters whose values you cannot change during simulation. For example, you cannot tune the Sample time block parameter. If a parameter is nontunable, you cannot change its value during simulation by changing the value in the block dialog box or by changing the value of a workspace variable.

Nontunable block parameters include:

  • Sample times.

  • Parameters that control the appearance or structure of a block such as the number of inputs of a Sum block.

  • Priority, which allows you to control block execution order.

  • Parameters that control the block algorithm, such as the Integrator method parameter of a Discrete-Time Integrator block.

To determine whether a block parameter is tunable during simulation, use one of these techniques:

  • Begin a simulation and open the block dialog box. If the value of the target block parameter is gray during simulation, you cannot tune the parameter.

  • At the command prompt, determine whether the flags read-write and read-only-if-compiled describe the parameter.

    1. Select the block in the model.

    2. At the command prompt, use the function get_param to return information about the block dialog box parameters. The function returns a structure that has a field for each parameter in the block dialog box.

      paramInfo = get_param(gcb,'DialogParameters');

      Alternatively, rather than locating and selecting the block in the model, you can replace gcb with the block path, such as 'myModel/mySubsystem/myBlock'.

    3. View the information about the target block parameter. For example, to view the information about the Sample time parameter of a block, view the value of the field SampleTime, which is also a structure.

      ans = 
              Prompt: 'Sample time:'
                Type: 'string'
                Enum: {}
          Attributes: {'read-write'  'read-only-if-compiled'  'dont-eval'}

    4. Inspect the structure's Attributes field, whose value is a cell array of character vectors. If the flag read-write appears in the cell array, you can modify the parameter value. However, if the flag read-only-if-compiled also appears in the cell array, you cannot modify the parameter value during simulation.

If you use masks to create custom interfaces for blocks and subsystems, you can control the tunability of individual mask parameters. If you use model arguments to parameterize referenced models, you can tune the value of each model argument in each Model block.

Why Did the Simulation Output Stay the Same?

If the output of your simulation does not change after you change a parameter value, use these troubleshooting techniques:

  • Locate the definition of a workspace variable.

    If you use a workspace variable to set block parameter values, determine where the variable definition resides. For example, if you define a variable myVar in a model workspace and use it to set a block parameter value in the model, you cannot change the parameter value by changing the value of a variable named myVar in the base workspace. You must access the variable definition in the model workspace.

    To locate the definition of a variable, open a block dialog box that uses the variable. Right-click the block parameter value and select Explore Variable. A dialog box opens, such as the Model Explorer, which displays the definition of the variable in the appropriate workspace. For more information about how models use variables, see Symbol Resolution.

  • Specify value ranges for fixed-point parameters that you want to tune during simulation.

    If the block parameter you want to tune uses a fixed-point data type with best-precision scaling, specify a minimum and maximum value for the parameter so that Simulink can calculate and apply an appropriate scaling. If you do not specify a value range, Simulink might apply a scaling that excludes the tuning values that you want to use. To specify value ranges, see Specify Minimum and Maximum Values for Block Parameters.

  • Update the model diagram during a simulation run.

    After you tune a block parameter value during a simulation run, you must update the model diagram if you use:

    • A workspace variable to set the value of one or more block parameters. After you change the value of the variable, update the model diagram.

    • The set_param function to change the parameter value.

    To learn how to update a model diagram, see Update Diagram and Run Simulation.

Tunability Considerations for Other Modeling Goals

Rapid Accelerator Simulations

If a block parameter value references workspace variables, you cannot change the block parameter value during rapid accelerator simulation, such as by using the function set_param. Instead, you can tune the values of the referenced variables.

Alternatively, use parameter sets to tune runtime parameters in between rapid accelerator simulations. For more information, see Tuning Runtime Parameters.

Code Generation and Simulation of External Programs

Parameters that are tunable during simulation can appear as nontunable inlined parameters in the generated code. If you simulate an external program by using SIL, PIL, or External mode simulation, parameter tunability during the simulation can depend on your code generation settings.

To control parameter tunability in the generated code, you can adjust the code generation settings for a model by using the configuration parameter Default parameter behavior. You can also adjust settings for individual MATLAB variables, Simulink.Parameter objects, and other parameter objects. For more information, see Configure Block Parameter Tunability for Rapid Prototyping in the Simulink Coder™ documentation.

Stateflow Charts

To debug a Stateflow chart by changing data during simulation, see Change Data Values During Simulation.

See Also

Related Examples

Was this topic helpful?