Main Content

param.Discrete

Discrete tunable parameter

Since R2022b

Description

A discrete parameter is a parameter that can take one of a specified, finite set of values. The allowed values can be a set of numeric values or a set of strings. The parameter itself can be scalar-valued or array-valued. For an array-valued parameter, each free entry in the array can take any of the allowed values, independently.

Typically, you use discrete parameters to estimate or optimize tunable parameters in parametric models. For instance, when performing response optimization or parameter estimation with sdo.optimize, use sdo.getParameterFromModel to create param.Discrete objects corresponding to parameters in your Simulink® model.

Note

To use discrete-valued parameters for response optimization or parameter estimation, you must use the surrogateopt optimization method, specified with sdo.OptimizeOptions.

Creation

Create a discrete parameter object in one of the following ways.

  • sdo.getParameterFromModel to create objects corresponding to discrete-valued parameters in a Simulink model.

  • param.Discrete function.

Description

example

p = param.Discrete(name) creates a scalar parameter and sets the Name property. The remaining properties of the object have default values.

example

p = param.Discrete(name,value) creates a parameter with the same dimensions as value and sets the Value property.

example

p = param.Discrete(name,value,valueset) specifies the set of discrete values that the parameter can take. value must be present in valueset.

Properties

expand all

This property is read-only.

Parameter name, specified as a character vector or string scalar and stored as a character vector. Set Name at object creation.

Parameter value, specified as a scalar, array, string scalar, or string array. The dimensions of this property determine the dimensions of the parameter. The parameter value must be an element in the ValueSet property.

Allowed parameter values, specified as a numerical array or a string array. When you use the parameter for response optimization or parameter estimation, the optimizer uses only values specified in this set. Discrete numeric values need not be integers or equally spaced. When using string values, you must provide a string array, not a cell array of character vectors.

For array-valued parameters, each entry in the parameter can independently take any of the values specified in ValueSet.

Example: [1.0,1.5,3.0,5.5]

Example: 1:10

Example: ["off","low","high"]

Indication of whether parameter is tunable, specified as numeric or logical 1 (true) or 0 (false) or an array of such values.

For scalar parameters:

  • Free = 1 (true) means the parameter is tunable during optimization.

  • Free = 0 (false) means the parameter value is fixed during optimization.

For array-valued parameters, the dimensions of Free match the dimensions of the Value property. For array-valued parameters, you can:

  • Fix individual array elements. For example, p.Free = [1 0; 0 1] fixes the off-diagonal elements of a 2-by-2 matrix-valued parameter during optimization but allows the diagonal elements to be tuned. Similarly, p.Free([2 3]) = 0 fixes the second and third elements in p.

  • Use scalar expansion to fix all array elements. For example, p.Free = false fixes the values of all elements of p during optimization.

Parameter units and labels, specified as a structure array with fields Label and Unit. The array dimension must match the dimension of the Value property.

Use this property to store parameter units and labels that describe the parameter. For example, p.Info(1,1).Unit = 'N/m'; or p.Info(1,1).Label = 'spring constant'.

The default value for both the Label and Unit fields is ''.

Examples

collapse all

Configure variable parameters in a Simulink® model for response optimization or parameter estimation, where the parameter values are restricted to a specified discrete set of values.

Open the model.

open_system("sdoMotorPosition")

The Controller subsystem is a PI controller whose gains Kp and Ki are set by the values of the resistors in the voltage divider of the following diagram.

Resistors are commonly manufactured in standard values. Therefore, when using response optimization to identify suitable values for the resistors R1, R2, R3, and R4, it is useful to confine these parameters to a discrete set of readily available resistor values. To do so, use sdo.getParameterFromModel to create param.Discrete parameter objects for each of the resistors. In this case, because all model parameters you plan to optimize are discrete, use [] for the continuous parameters.

DesignVars = sdo.getParameterFromModel("sdoMotorPosition",[],["R1","R2","R3","R4"]);

DesignVars is an array of param.Discrete parameter objects, ordered in the order you specified in the call to sdo.getParameterFromModel. In other words, DesignVars(1) corresponds to R1, DesignVars(2) corresponds to R2, and so on. For instance, examine the parameter corresponding to R4.

DesignVars(4)
 
ans =
 
        Name: 'R4'
       Value: 10
    ValueSet: 10
        Free: 1
        Info: [1x1 struct]

 
1x1 param.Discrete
 

sdo.getParameterFromModel sets both the Value and ValueSet properties to the current values of the parameters in the Simulink model. To prepare for optimization, set the ValueSet properties of these parameters to the discrete set of resistor values that each parameter can take.

DesignVars(1).ValueSet = [22,33,47,68,100];
DesignVars(2).ValueSet = [120,150,180,220,270];
DesignVars(3).ValueSet = [6.8,8.2,10,12,15];
DesignVars(4).ValueSet = [6.8,8.2,10,12,15];

You can now continue the response optimization workflow by defining requirements and a cost function and using sdo.optimize. Note that when you use discrete-valued parameters for either response optimization or parameter estimation with sdo.optimize, you must set the optimization method to surrogateopt (see sdo.OptimizeOptions).

Construct a param.Discrete object and set its initial value to the identity matrix.

p = param.Discrete("K1") 
 
p =
 
        Name: 'K1'
       Value: 0
    ValueSet: 0
        Free: 1
        Info: [1x1 struct]

 
1x1 param.Discrete
 

Set the initial value and the values that the parameter can take during optimization. The value set must include the initial value. Note that the allowed values do not need to be integers or evenly spaced.

p.ValueSet = [-10,-5,-2.5,-1,0];
p.Value = -1;

Next, create a string-valued parameter whose initial value is "off". The values that the parameter can during optimization are "off", "low", "med", and "high". You can optionally specify the initial value and allowed values all at once when you create the parameter object.

q = param.Discrete("K2","off",["off","low","med","high"])
 
q =
 
        Name: 'K2'
       Value: "off"
    ValueSet: ["off"    "low"    "med"    "high"]
        Free: 1
        Info: [1x1 struct]

 
1x1 param.Discrete
 

Construct a param.Discrete object and set its initial value to the identity matrix.

p = param.Discrete('K',eye(2)) 
 
p =
 
        Name: 'K'
       Value: [2x2 double]
    ValueSet: [1 0]
        Free: [2x2 logical]
        Info: [2x2 struct]

 
1x1 param.Discrete
 

Set the values that all entries in p can take during optimization. The value set must include the initial values. Note that the allowed values do not need to be integers or evenly spaced.

p.ValueSet = [0,0.5,1,10,15,25];

Suppose instead that you want the diagonal elements of p to remain fixed at 1 during optimization, but let the off-diagonal elements vary. First, confirm that the current value has diagonal elements equal to 1. Then, use the Free property to fix the diagonal elements at this value.

p.Value
ans = 2×2

     1     0
     0     1

p.Free = [0 1; 1 0];

Version History

Introduced in R2022b