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 aTransfer Fcnblock, 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, seeGet 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, seeOptimize, Estimate, and Sweep Block Parameter Values.

For basic information about accessing and setting block parameter values, seeSet 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 to use and how to build your model.

TheModeling a Fault-Tolerant Fuel Control Systemexample modelsldemo_fuelsysrepresents 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 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 from14.6to17.5to 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 theModeling a Fault-Tolerant Fuel Control Systemexample modelsldemo_fuelsys.

    openExample('simulink_automotive/ModelingAFaultTolerantFuelControlSystemExample') sldemo_fuelsys

  2. Set the model simulation time from2000to50for a faster simulation.

  3. In the model, open theScopeblock dialog box.

  4. Simulate the model. Resize the window in theScopedialog box to see all of the simulation results.

    The scope display shows that throughout the simulation, thefuelsignal oscillates between approximately0.9and1.6. Theair_fuel_ratiosignal quickly climbs to15without overshoot.

  5. In the model, open the Model Data Editor. On theModelingtab, clickModel Data Editor. In the Model Data Editor, inspect theParameterstab.

  6. In the model or at the 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 theValuecolumn to change theConstant value(Value) parameter of theConstantblock labeledrichfrom1/(14.6*0.8)to1 / (17.5 * 0.8).

  8. Similarly, change theConstant value车辆参数的ck labelednormalfrom1/14.6to1/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 thenormalConstantblock to1/mixture.

  2. Set the value of therichblock to1/(mixture*0.8).

  3. While editing therichvalue, next to1/(mixture*0.8), click the action buttonand selectCreate.

  4. In theCreate New Datadialog box, setValueto17.5and clickCreate.

    The numeric variablemixtureappears in the base workspace with value17.5. Between simulation runs, you can change the value ofmixturein 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 modelsldemo_fuelsyscontains aConstantblock,Throttle Command, that represents the throttle command. To observe the effect of increasing the magnitude of the command during simulation:

  1. Open theModeling a Fault-Tolerant Fuel Control Systemexample modelsldemo_fuelsys.

    openExample('simulink_automotive/ModelingAFaultTolerantFuelControlSystemExample') sldemo_fuelsys

  2. In the model, open theScopeblock dialog box.

  3. Begin a simulation.

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

  4. In the model, when the status bar indicates approximately1000(1000 seconds), click the Pause buttonto pause the simulation.

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

  6. In the model, on theModelingtab, clickModel Data Editor.

  7. In the Model Data Editor, select theParameterstab.

  8. In the model, select theThrottle Commandblock.

  9. In the Model Data Editor, select therep_seq_yrow. Make sure that you do not select therep_seq_trow as well.

  10. For therep_seq_yrow, change the value in theValuecolumn from[10 20 10]to[10 30 10].

  11. Click the Step Forward buttonto advance the simulation step by step. Click the button about 15 times or until you see a change in thefuelgraph in the scope display.

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

  12. 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. For more information about updating the model diagram, seeUpdate Diagram and Run Simulation.

Prepare for Parameter Tuning and Experimentation

  • Use workspace variables to set block parameter values.

    To access the value of a block parameter, such as theConstant valueparameter of aConstantblock, 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 functionset_paramat 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, seeSet 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 theScopeblock. 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, seeDecide 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为集团指定最小值和最大值k 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, seeSimulate a Model InteractivelyandRun 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, seeTune and Visualize Your Model with Dashboard Blocks.

这一块在仿真参数可调on?

Nontunable block parametersare parameters whose values you cannot change during simulation. For example, you cannot tune theSample timeblock 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 aSumblock.

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

  • Parameters that control the block algorithm, such as theIntegrator methodparameter of aDiscrete-Time Integratorblock.

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 flagsread-writeandread-only-if-compileddescribe the parameter.

    1. Select the block in the model.

    2. At the command prompt, use the functionget_paramto 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 replacegcbwith the block path, such as“myModel / mySubsystem / myBlock”.

    3. View the information about the target block parameter. For example, to view the information about theSample timeparameter of a block, view the value of the fieldSampleTime, 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’sAttributesfield, whose value is a cell array of character vectors. If the flagread-writeappears in the cell array, you can modify the parameter value. However, if the flagread-only-if-compiledalso 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 eachModelblock.

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 variablemyVarin 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 namedmyVarin 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 buttonand selectExplore. 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, seeSymbol 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为集团指定最小值和最大值k 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.

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

Tunability Considerations and Limitations for Other Modeling Goals

Referenced Models

When you useModelblocks, these parameter tunability limitations apply:

  • If you set the simulation mode of aModelblock to an accelerated mode or if you simulate the parent model in an accelerated mode, you cannot tune block parameters in the referenced model during simulation. However, if the referenced model uses variables in the base workspace or a data dictionary to set parameter values, you can tune the values of the variables.

  • Suppose you use a MATLAB variable orSimulink.Parameterobject in a model workspace to set the value of a block parameter in a model. If you use aModelblock to refer to this model:

    • And you set the simulation mode of theModelblock to an accelerated mode or simulate the parent model in an accelerated mode, you cannot change the value of the variable or object during the simulation.

    • When you simulate the parent model in an accelerated mode, changing the value of the variable or object between simulation runs causes Simulink to regenerate code.

    • And you use additionalModelblocks to refer to the model multiple times in the parent model, you can choose a different simulation mode for eachModelblock. If at least one block uses normal simulation mode and any other block uses a different simulation mode, you cannot change the value of the variable or object during simulation. Also, when you simulate the parent model with fast restart on, you cannot change the value of the variable or object between fast-restart simulation runs.

    As a workaround, move the variable or object to the base workspace or a data dictionary.

Accelerator and SIL/PIL Simulations

These tunability limitations apply to accelerator, rapid accelerator, SIL, and PIL simulations:

  • Suppose you use a MATLAB variable orSimulink.Parameterobject in a model workspace to set the value of a block parameter in a model. If you use thesimfunction to simulate the model in rapid accelerator mode and set theRapidAcceleratorUpToDateCheckpair argument to'off', you cannot use theRapidAcceleratorParameterSetspair argument to specify different values for the variable or object. The structure returned bySimulink.BlockDiagram.buildRapidAcceleratorTargetdoes not contain information about the variable or object.

  • If a block parameter value references workspace variables, you cannot change the block parameter value during rapid accelerator simulation, such as by using the functionset_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, seeTuning Runtime Parameters.

For more information about parameter tunability during accelerated simulations, seeTuning Runtime Parametersandsim in parfor with Rapid Accelerator Mode. For more information about parameter tunability during SIL and PIL simulations, seeTunable 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 parameterDefault parameter behavior. You can also adjust settings for individual MATLAB variables,Simulink.Parameterobjects, and other parameter objects. For more information, seePreserve Variables in Generated Code(Simulink Coder).

Stateflow Charts

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

See Also

Related Topics