Tune and Experiment with Block Parameter Values - MATLAB & Simulink (original) (raw)

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:

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 modelsldemo_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 number14.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 from14.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 modelsldemo_fuelsys.
    openExample("simulink_automotive/ModelingAFaultTolerantFuelControlSystemExample",...
    supportingFile="sldemo_fuelsys")
  2. Change the model stop time from 2000 to50 for a faster simulation.
  3. In the model, double-click the Scope block.
    Scope block with fuel and air_fuel_ratio as inputs.
  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, thefuel signal oscillates between approximately0.9 and 1.6. Theair_fuel_ratio signal quickly climbs to15 without overshoot.
  5. In the model, open the Model Data Editor. On theModeling tab, click Model Data Editor. In the Model Data Editor, inspect theParameters 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 from1/(14.6*0.8) to1/(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 to1/(mixture*0.8).
  3. While editing the rich value, next to1/(mixture*0.8), click the action button and select .
  4. In the Create New Data dialog box, set Value to17.5 and clickCreate.
    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 modelsldemo_fuelsys.
    openExample("simulink_automotive/ModelingAFaultTolerantFuelControlSystemExample",...
    supportingFile="sldemo_fuelsys")
  2. In the model, double-click the Scope block.
    Scope block with fuel and air_fuel_ratio as inputs.
  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 approximately1000 (1000 seconds), pause the simulation. In the Simulink Toolstrip, on the Simulation tab, clickPause.
  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, clickModel 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 theValue 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, clickStep 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 theCompile section, click Update Model.

Prepare for Parameter Tuning and Experimentation

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:

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

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:

Tunability Considerations and Limitations for Other Modeling Goals

Referenced Models

When a referenced model simulates only in normal mode:

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

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, seeGet 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® by changing data during simulation, see Set Breakpoints to Debug Charts (Stateflow).

See Also

set_param

Topics