- G_ineq - Gradient array for the inequality constraints
- G_eq - Gradient array for the equality constraints
Each column of G_ineq and G_eq contains the gradients for one constraint, and the
order of the columns matches the order of the rows in the corresponding constraint
vector. The number of rows in both G_ineq and G_eq is equal to the total number of
states, inputs, and outputs in x, u, and y. Each column contains gradients with respect to
the states in x, followed by the inputs in u, then the outputs in y.
For this example, add gradients to the constraint function that uses the custom mapping.
You do not have to specify a custom mapping when using gradients. However, defining
gradients is simpler when using mapped subsets of states, inputs, and outputs.
function [c_ineq,c_eq,G_ineq,G_eq] = myConstraintsGrad(x,u,y)
c_ineq = [];
c_eq = [x(1)-x(2); % Tank1 pressure - Tank2 pressure
x(2)-x(3)+1]; % Tank2 pressure - Tank3 pressure + 1
G_ineq = [];
G_eq = [1 0;
-1 1;
0 -1];
end
In this function, row i of G_eq contains gradients with respect to state x(i).
Similarly, to add gradients to your custom objective function, specify an additional output
argument G, which contains the gradients of F. G is returned as a column vector with the
same format as the columns of G_ineq and G_eq.
function [F,G] = myObjectiveGrad(x,u,y)
F = max(x(3)-20, 0) + max(16-x(3), 0);
if x(3) >= 20
G = [0 0 1]';
elseif x(3) <= 16
G = [0 0 -1]';
else
G = [0 0 0]';
end
Compute Operating Points Using Custom Constraints and Objective Functions