Specify Generic Cost Function
While traditional implicit MPC controllers optimize control actions to minimize a quadratic cost function, economic MPC controllers support generic cost functions. For example, you can specify your cost function as a combination of linear or nonlinear functions of the system states and inputs.
Using such a custom cost function can impose additional nonlinear constraints on your system. If this is the case for your application, specify these constraints in a separate custom function. For more information, seeSpecify Nonlinear Constraints.
To use a custom generic cost function:
Copy the custom cost function template file to your working folder or anywhere on the MATLAB®path, and rename it
mpcCustomCostFcn.m
.src = which('mpcCustomCostFcn.txt'); dest = fullfile(pwd,'mpcCustomCostFcn.m'); copyfile(src,dest,'f');
As an example, the template file implements a standard quadratic cost function.
Specify your generic cost function by modifying the template file.
Configure the controller
MPCobj
to use the custom cost function.MPCobj.Optimizer.CustomCostFcn = true;
Custom Cost Function
所示the template filempcCustomCostFcn.txt
, your custom cost function must have the following signature:
function[f,dfdy,dfdu,dfddu,dfdslack] =...mpcCustomCostFcn(y,yref,u,uref,du,v,slack,varargin)
The input arguments for this function are shown in the following table, where:
kis the current control interval.
pis the prediction horizon.
nyis the number of output variables.
nmvis the number of manipulated variables.
nmdis the number of measured disturbances.
The MPC controller generates these inputs and passes them to the custom function at each control interval.
Input Argument | Description | Specified As |
---|---|---|
y |
Predicted plant outputs, defined over the prediction horizon fromk+1 tok+p |
p——- - - - - -nyarray |
yref |
Previewed output references, defined over the prediction horizon fromk+1 tok+p |
p——- - - - - -nyarray |
u |
Optimal manipulated variable control sequences, defined over the prediction horizon fromktok+p-1 |
p——- - - - - -nmvarray |
uref |
Previewed manipulated variable targets, defined over the prediction horizon fromktok+p-1 While the software does not currently support manipulated variable previewing, this interface is general. |
p——- - - - - -nmvarray |
du |
Manipulated variable rates of change, defined over the prediction horizon fromktok+p-1 |
p——- - - - - -nmvarray |
v |
Previewed measured disturbances, defined over the prediction horizon fromktok+p |
(p+1)-by-nmdarray ([] if there are no measured disturbances) |
slack |
Global slack variable used by all soft constraints | scalar |
varargin |
Additional input arguments (for future use). | [] |
输出参数for this function are shown in the following table.
Output Argument | Description | Returned As |
---|---|---|
f |
Generic cost function value | scalar |
dfdy |
Gradients off with respect to the plant outputs |
p——- - - - - -nyarray ([] if unknown) |
dfdu |
Gradients off with respect to the manipulated variables |
p——- - - - - -nmvarray ([] if unknown) |
dfddu |
Gradients off with respect to the manipulated variable rates of change |
p——- - - - - -nmvarray ([] if unknown) |
dfdslack |
Gradients off with respect to the global slack variable |
scalar ([] if unknown) |
Typically, you optimize the controller to minimize the cost function across the prediction horizon. Since the cost function value must be a scalar, you compute the cost function at each prediction horizon step and add the results together. For example, suppose that the stage cost function is:
That is, you want to minimize the difference between the first output and its reference value, and the product of the first manipulated variable and the second output. To compute the total cost function across the prediction horizon, use:
f = sum(sum((y(:,1)-yref(:,1)).^2 + (u(:,1).*y(:,2)).^2));
While computing and returning gradients is optional, doing so improves the computational efficiency of the controller optimization. To find the gradients, compute the partial derivatives of the cost function at each step of the prediction horizon. For example, for the preceding cost function, the gradients are:
dfdy = zeros(p,ny); dfdy(:,1) = 2*(y(:,1)-yref(:,1)); dfdy(:,2) = 2*y(:,2).*u(:,1).^2; dfdu = zeros(p,nmv); dfdu(:,1) = 2*u(:,1).*y(:,2).^2; dfddu = zeros(p,nmv); dfdslack = 0;
Within your custom function, to obtain the:
Prediction horizon,
p
使用:p =大小(y, 1);
Number of outputs,
ny
使用:ny = size(y,2);
Number of manipulated variables,
nmv
使用:nmv = size(u,2);
Number of measured disturbances,
nmd
使用:nmd = size(v,2);
Previous manipulated variable values; that is,
u(k-1)
使用:u_prev = u(1,:) - du(1,:);