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.
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.
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:
Open the example model.
sldemo_fuelsys
Set the model simulation time from 2000
to 50
for
a faster simulation.
In the model, open the Scope block dialog box.
Simulate the model. Resize the window in the Scope 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.
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.
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')
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)
.
Similarly, change the Constant value parameter
of the block labeled normal
from 1/14.6
to 1/17.5
.
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:
Use the Model Data Editor to set the value of the normal
Constant block to 1/mixture
.
Set the value of the rich
block to
1/(mixture*0.8)
.
While editing the rich
value, next to
1/(mixture*0.8)
, click the action button
and select
Create.
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 mixture
in the
base workspace instead of changing the parameter values in the block
dialog boxes.
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:
Open the example model.
sldemo_fuelsys
In the model, open the Scope block dialog box.
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.
In the model, when the status bar indicates approximately
1000
(1000 seconds), click the Pause button
to pause the simulation.
In the scope display, the fuel graph plots the simulation output prior to the pause time.
In the model, on the Modeling tab, click Model Data Editor.
In the Model Data Editor, select the Parameters tab.
In the model, select the Throttle Command
block.
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.
For the rep_seq_y
row, change the value in the
Value column from [10 20 10]
to
[10 30 10]
.
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.
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, see Update Diagram and Run Simulation.
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.
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.
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.
Select the block in the model.
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'
.
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'}
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.
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.
To learn how to update a model diagram, see Update Diagram and Run Simulation.
When you use Model blocks, these parameter tunability limitations apply:
If you set the simulation mode of a Model block 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 or Simulink.Parameter
object
in a model workspace to set the value of a block parameter in a
model. If you use a Model block to refer to this
model:
And you set the simulation mode of the Model block 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 additional Model blocks to refer to the model multiple times in the parent model, you can choose a different simulation mode for each Model block. 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.
These tunability limitations apply to accelerator, rapid accelerator, SIL, and PIL simulations:
Suppose you use a MATLAB variable or Simulink.Parameter
object
in a model workspace to set the value of a block parameter in a
model. If you use the sim
function to simulate
the model in rapid accelerator mode and set the
RapidAcceleratorUpToDateCheck
pair argument
to 'off'
, you cannot use the
RapidAcceleratorParameterSets
pair argument
to specify different values for the variable or object. The
structure returned by
Simulink.BlockDiagram.buildRapidAcceleratorTarget
does 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 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.
For more information about parameter tunability during accelerated simulations, see Tuning Runtime Parameters and sim in parfor with Rapid Accelerator Mode. For more information about parameter tunability during SIL and PIL simulations, see Tunable Parameters and SIL/PIL (Embedded Coder).
For more information about parameter tunability when you use fast restart, see Get Started with Fast Restart .
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).
To debug a Stateflow chart by changing data during simulation, see Debugging Stateflow Charts (Stateflow).