Nonlinear constraint optimization gradient
Show older comments
Hello community, reached the end of the line with this... would appreciate your advice with this optimization.
Functions: One nonlinear to maximize and one nonlinear step function as a constraint
1) max: u(y,x) = y-0.6x^2
2) Constraints:
max(0,1-3(x-1)^2); if x<1
2-(x-2)^2; if 1<=x<=2
2; if 2<x
Objective function:
function [f,gradf] = objfungrad(x)
f = 0.6*x.^2;
% Gradient of the objective function:
if nargout>1
gradf = [f + 1.2*x];
end
Constraint:
function [c,ceq,DC,DCeq] = confungrad(x)
c1 = -1+(3*(x-1).^2); % Inequality constraints
c2 = -2+(x-2).^2;
%nonlinear equality constraints
ceq=[];
% Gradient of the constraints:
if nargout > 0
DC= [6*x+6;
2*x+4];
DCeq = [];
end
Solution:
x0 = [0.1 0.1]; % Starting guess
options = optimoptions(@fmincon,'Algorithm','sqp');
options = optimoptions(options,'SpecifyObjectiveGradient',true,'SpecifyConstraintGradient',true);
lb = []; ub = [];
[x,fval] = fmincon(@objfungrad,x0,[],[],[],[],[],[],...
@confungrad,options);
Errors:
>> Solution
Error using fmincon (line 608)
Supplied objective function must return a scalar value.
Error in Solution (line 5)
[x,fval] = fmincon(@objfungrad,x0,[],[],[],[],[],[],...
Your guidance is greatly appreciated. I have tried this a myriad of ways and specifications and am at a loss. Thank you!!
Graph of objective and constraint functions. Note: x==z; q==y

%
1 Comment
Hendrik van Gils
on 8 Mar 2022
Your objective function returns a vector, you could try to use:
function [f,gradf] = objfungrad(x)
f = x.'*x; % the 0.6 doesn't change the location of the optimum, but you can add it if you want.
% Gradient of the objective function:
if nargout>1
gradf = [f + 1.2*x];
end
Answers (0)
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!