Main Content

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 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 and block parameter values to use and how to build your model.

The Model Fault-Tolerant Fuel Control System 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 have parameters with values calculated based on the air to fuel ratio.

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 Parameters dialog box for each block that uses the value. 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 modify the air to fuel ratio value by changing the value in the block dialog boxes:

  1. Open the Model Fault-Tolerant Fuel Control System example model sldemo_fuelsys.

    openExample("simulink_automotive/ModelingAFaultTolerantFuelControlSystemExample",...
        supportingFile="sldemo_fuelsys")

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

  3. In the model, double-click the Scope block.

  4. Simulate the model. Resize the window for the Scope block 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. In the model, open the Model Data Editor. On the Modeling tab, click Model Data Editor. In the Model Data Editor, inspect the Parameters tab.

  6. In the model or from the MATLAB® command prompt, navigate to the target subsystem.

    open_system(...
        'sldemo_fuelsys/fuel_rate_control/fuel_calc/feedforward_fuel_rate')

  7. In the Model Data Editor, use the Value column to change the Constant value (Value) parameter of the Constant block labeled rich 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. Use the Model Data Editor to set the value of the normal Constant block to 1/mixture.

  2. Set the value of the rich block to 1/(mixture*0.8).

  3. While editing the rich value, next to 1/(mixture*0.8), click the action button and select Create.

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

    The numeric variable mixture appears in the base workspace with value 17.5. Between simulation runs, you can change the value of the variable 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 Model Fault-Tolerant Fuel Control System example model sldemo_fuelsys.

    openExample("simulink_automotive/ModelingAFaultTolerantFuelControlSystemExample",...
        supportingFile="sldemo_fuelsys")

  2. In the model, double-click the Scope block.

  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), pause the simulation. In the Simulink Toolstrip, on the Simulation tab, click Pause.

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

  6. In the Simulink Toolstrip, on the Modeling tab, click Model Data Editor.

  7. In the Model Data Editor, select the Parameters tab.

  8. In the model, select the Throttle Command block.

  9. In the Model Data Editor, select the rep_seq_y row. Make sure that you do not select the rep_seq_t row as well.

  10. For the rep_seq_y row, change the value in the Value column from [10 20 10] to [10 30 10].

  11. To advance the simulation by one major time step, in the Simulink Toolstrip, on the Simulation tab, click Step Forward. Continue clicking Step Forward until you see a change in the fuel graph in the scope window.

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

  12. To resume the simulation, in the Simulink Toolstrip, click 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. To update the model diagram, on the Simulink Toolstrip, on the Modeling tab, in the Compile section, click Update Model.

Prepare for Parameter Tuning and Experimentation

  • Use 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.

  • 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.

  • Specify 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.

  • 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, see Simulate a Model Interactively and Run 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.

      paramInfo.SampleTime
      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, while editing the value of a block parameter that uses the variable, click the nearby action button and select Explore. 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. If you use a workspace variable to set the value of one or more block parameters, after you change the value of the variable during a simulation, you must update the model diagram.

Tunability Considerations and Limitations for Other Modeling Goals

Referenced Models

When a referenced model simulates only in normal mode:

  • During simulation, you can tune parameter values in the referenced model. For example, for a Gain block that has Gain set to 5, you can change the value of Gain from 5 to 10.

  • During simulation, you can tune parameter values specified by MATLAB variables and Simulink.Parameter objects in the base workspace, a data dictionary, or the model workspace. For example, for a Gain block that has Gain set to k, you can change the value of k from 5 to 10.

When one or more Model blocks simulate a referenced model in accelerator mode:

  • During simulation, you can tune parameter values specified by MATLAB variables and Simulink.Parameter objects in the base workspace or a data dictionary.

  • Between simulation runs that are not fast restart simulation runs, you can change the value of parameters in the referenced model.

  • Between simulation runs that are not fast restart simulation runs, you can change the value of MATLAB variables and Simulink.Parameter objects in the model workspace. During the next simulation run, the software regenerates the model reference simulation target. For more information, see Manage Simulation Targets for Referenced Models.

Regardless of the simulation mode, you can tune model arguments.

Rapid Accelerator, SIL, and PIL Simulations

For information about parameter tunability in rapid accelerator simulations, see Tune Runtime Parameters.

For more information about parameter tunability in SIL and PIL simulations, see Tunable Parameters and SIL/PIL (Embedded Coder).

Fast Restart

For more information about parameter tunability when you use fast restart, see Get Started with Fast Restart.

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 and between simulation runs 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 Preserve Variables in Generated Code (Simulink Coder).

Stateflow Charts

To debug a Stateflow chart by changing data during simulation, see Set Breakpoints to Debug Charts (Stateflow).

See Also

Related Topics