When you use
optimize model parameters (design variables),
you must provide a MATLAB® function as an input to
This function, also called a cost function,
must evaluate the cost and constraint values for the design variable
values for an iteration. (The cost and constraint functions are collectively
referred to as requirements.)
this function at every optimization iteration and use the function
output to decide the optimization direction.
The cost function can also be used for global sensitivity analysis.
You generate samples of the model parameters and evaluate the cost
function for each sample using
The cost function must have:
params , a vector
of the design variables (
param.Continuous objects) to be optimized.
vals , a structure with
one or more fields that specify the values of the cost and constraint
derivs, a structure
with one or more fields that specify the values of the gradients of
the cost and constraint violations.
You perform the following tasks within the function:
Extract the current design variable values from
If the simulated response is required for evaluating the requirements, then simulate the model using the current design variable values.
Evaluate the requirements.
Specify the requirement values as fields of
To use a cost function with
[param_opt,opt_info] = sdo.optimize(@myCostFunc,param)
myCostFunc is the name of the MATLAB function
param is a vector of the design variables.
Similarly, to use a cost function with
[y,info] = sdo.evaluate(@myCostFunc,param)
The software provides you with the following convenience objects that can you can use in the cost function:
configure the simulator to log the signals needed to evaluate requirements
and use the
Use these requirements objects to specify time- and frequency-domain costs or constraints on the design variables.
You configure the properties of the object
and then use the object's
You update the design variable values associated
with the experiment using the
The function must take as input a vector of model parameter
param.Continuous objects) and, optionally,
initial-state objects (
param.State objects). These
objects represent the design variables of the
optimization problem. You obtain these objects by using the
To access a design variable value, use:
param_val = p(1).Value;
p is a vector of
p(1) is either a model parameter or an initial-state
sdo.optimize requires that the cost function
accept only one input argument,
you might want to use additional inputs. For instance, you could make
the model name an input argument and configure the function to be
used for multiple models. To call
use a function that accepts more than one input argument, you use
an anonymous function. For example, suppose
a cost function that takes
arg2 as inputs. Then, assuming that all input
arguments are variables in the workspace, you enter:
myCostFunc = @(param) myCostFunc_mult_inputs(param,arg1,arg2); [param_opt,opt_info] = sdo.optimize(@myCostFunc,param);
Additional inputs can also help reduce code redundancy and computation
cost, given that the function is called repeatedly by
optimization. For instance, if you use a convenience object in your
function, you can create it once, before calling
Then, you can modify the convenience object's properties as
required within the function for each iteration.
The core of the function is where you evaluate how well the current design variables satisfy the design requirements. You can use MATLAB functions to do so. You can also use the requirements objects that the Simulink® Design Optimization™ software provides. These objects enable you to specify requirements such as step-response characteristics, gain/phase margin bounds, Bode magnitude bounds, etc.
Parameter-only requirements — Extract the design variable values and compute the requirement values.
For example, you can minimize the cylinder cross-sectional area, a design variable, in a hydraulic cylinder. See Design Optimization to Meet a Custom Objective (Code).
Model response-based requirements — Simulate the model using the current design variable values, extract the model response, and compute the requirement values.
There are multiple ways to simulate the model, including:
sdo.SimulationTest object. You update the model parameter values
using the simulator's
Then, you use the
sim method to simulate the
model and extract the logged signals from the simulator that are of
interest. For an example, see Design Optimization to Meet a Custom Objective (Code).
In parameter estimation, you can use the
to create the simulator. Before creating the simulator, you update
the experiment with the current design variable values using the
For an example, see Estimate Model Parameters Per Experiment (Code)
Linear model-based requirements — Update the model with the current design variable values, linearize the model, and compute the requirement values.
Your function must return a structure containing the cost and constraint values for the current design variables. This structure must have one or more of the following fields, as required by your optimization problem:
F — Cost value.
Nonlinear constraint values. The solver satisfies
Linear constraint values. The solver satisfies
If you have multiple constraints of one type, concatenate the
values into a vector, and specify this vector as the corresponding
field value. For instance, if you have a hydraulic cylinder, you can
specify nonlinear inequality constraints on the piston position (
and cylinder pressure (
Cleq2). In this case, specify
Cleq field of the output structure,
vals.Cleq = [Cleq1; Cleq2];
For an example, see Design Optimization to Meet a Custom Objective (Code).
By default, the software computes the cost and constraint gradients using numeric perturbation. However, you can specify the gradients and return them as an additional output. This output must be a structure with one or more of the following fields, as required by your optimization problem:
F — Cost derivatives.
Cleq — Nonlinear inequality
Ceq — Nonlinear equality
You must also set the
GradFcn property of
the optimization option set to
Simulink Design Optimization does not support multi-objective
optimization. However, you can return the cost value (
as a vector, representing the multiple objective values. Using this
approach does not halt the optimization. Instead, the software sums
the elements of the vector and minimizes this sum. The exception to
this behavior is if you are using the nonlinear least squares (
optimization method. The nonlinear least squares method, used for
parameter estimation, requires that you return the error residuals
as a vector. In this case, the software minimizes the sum square of
If you are tracking multiple signals and using
then you must concatenate the error residuals for the different signals
into one vector. Specify this vector as the
For an example of single objective optimization using the gradient descent method, see Design Optimization to Meet a Custom Objective (Code).
For an example of multiple objective optimization using the nonlinear least squares method, see Estimate Model Parameters Per Experiment (Code).