Generate random samples of uncertain variables in a Simulink model
Use this function to generate random samples of uncertain variables in a Simulink® model that contains Uncertain State Space blocks. Such sampling lets you perform Monte Carlo analysis of the model response.
To generate random samples from uncertain elements and models such as
genfrd models, use
generates a random sample of the uncertain variables in the structure
uvals = usample(
uvars. This structure lists the uncertain variables in a Simulink model that contains Uncertain State Space blocks. To obtain the
uvars, for a particular Simulink model, use
ufind. The resulting structure
uvals has fields whose names are the uncertain variable names and
values are the corresponding samples. To apply the sampled values when you simulate the
model, set the Uncertainty value parameter of the Uncertain
State Space block to
Sample Uncertain Variables in Simulink Model
Generate random samples of uncertain variables in a Simulink model.
Open the model.
The model contains three Uncertain State Space blocks named
Unmodeled Plant Dynamics,
Sensor Gain. Each of these blocks is preconfigured with an uncertain system variable that depends on an uncertain element, as follows.
Unmodeled plant dynamics: a
Sensor gain: a
Create a structure of these uncertain elements using
ufind. This command finds all Uncertain State Space blocks and uncertain variables in the model.
uvars = ufind('usim_model'); uvars
uvars = struct with fields: input_unc: [1x1 ultidyn] sensor_gain: [1x1 ureal] unc_pole: [1x1 ureal]
The field names and values of
uvars correspond to the uncertain elements in the three Uncertain State Space blocks. To simulate the model at random values of these variables, use
usample to generate the values in a structure.
uval = usample(uvars)
uval = struct with fields: input_unc: [1x1 ss] sensor_gain: 0.2853 unc_pole: -8.3290
Each field of
uval contains the corresponding sample value. The randomly chosen values of the
ureal elements are real scalar values. The random sample of the
ultidyn element is a state-space model.
To simulate the model using these random values, set the Uncertainty value parameter of each Uncertain State Space block to
usim_model this parameter is already set. Therefore, when you simulate the model, Simulink applies the values in
uval to the uncertain variables in the model.
For Monte Carlo analysis, you can use a
for loop to repeatedly sample the variables and simulate the model.
for i=1:10; uval = usample(uvars); sim('usim_model',10); end
The MultiPlot Graph block displays the simulated responses.
uvars — Uncertain variables to sample
Uncertain variables to sample, specified as a structure whose field names are the
names of uncertain variables and whose field values are the corresponding uncertain
elements. For instance,
uvars might have a field
a whose value is a
ureal element and a field
Delta whose value is a
To sample uncertain variables in a Simulink model that contains Uncertain State Space blocks, use the
ufind command to construct
uvars. This command finds the uncertain variables referenced in
the Uncertain State Space blocks in the model and populates the names and
values of the structure
N — Number of samples
1 (default) | positive integer
Number of samples to take, specified as a positive integer.
wmax — Maximum magnitude of poles in sampled dynamics
Maximum magnitude of poles in sampled dynamics, specified as a positive scalar
value. When you sample uncertain dynamics (
ultidyn) or gain and phase
umargin), use this input to limit the frequency of poles
in the resulting system.
ultidyn variables with
Type set to
If variable is continuous time (
Ts = 0), then each pole of the sampled dynamics has a natural frequency less than or equal to
If the variable is discrete time (
Ts != 0), then the natural frequency of each pole is less than
ultidyn variables with
Type set to
usample samples the variables as
GainBounded case and performs a bilinear transform on the
The value of
wmax overrides the
SampleMaxFrequency property of the
To control the order of the sampled dynamics, use the
SampleStateDimension property of the
Introduced in R2009b