Error "Too many output arguments" from fmincon

2 Ansichten (letzte 30 Tage)
sxj
sxj am 18 Jun. 2019
Kommentiert: sxj am 19 Jun. 2019
Hallo, everyone, I am using fmincon to solve an optimization problem. The main code is shown as below:
x = fmincon(@(x)e1_funcOptimal(x), x_0,[], [], [], [], -10, 10, @(x)e1_consOptimal(x), options);
where e1_funcOptimal(x) is the function provided the objective function for the optimization, and e1_consOptimal(x) is the constraint conditions which x need to satisfy.
The e1_funcOptimal(x) function is shown as below:
tol = 1.0E-13;
options = optimset( ...
'Display', 'off', ...
'GradObj', 'on', ...
'GradConstr', 'on', ...
'DerivativeCheck', 'off', ...
'FinDiffType', 'central', ...
'TolFun', tol, ...
'TolX', tol, ...
'TolCon', tol, ...
'algorithm', 'active-set', ...
'MaxFunEvals', inf, ...
'MaxIter', 5000);
function obj = e1_funcOptimal(x)
obj = -x(1,10);
% or obj = -x(10)
% end
It means that I choose the negative value of one of the inputs as objective function.
The error messsage is that
Error using e1_funcOptimal
Too many output arguments.
Error in @(x)e1_funcOptimal(x)
Error in fmincon (line 561)
[initVals.f,initVals.g] = feval(funfcn{3},X,varargin{:});
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
How could I fix the problem. Thanks for reading.
  4 Kommentare
Walter Roberson
Walter Roberson am 18 Jun. 2019
Please show your options.
I predict that you configured in a way that requires a Jacobian to be output as the second output of your objective function.
sxj
sxj am 18 Jun. 2019
hey, Walter, I have just upload the options. Do you mean I choose 'on' for the option "GradObj'' and 'GradConstr' and need to switch them to 'off'?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 18 Jun. 2019
Bearbeitet: Matt J am 18 Jun. 2019
Do you mean I choose 'on' for the option "GradObj'' and 'GradConstr' and need to switch them to 'off'?
Either switch them off or provide the gradients, e.g.,
function [obj,grad] = e1_funcOptimal(x)
obj = -x(1,10);
if nargout>1
grad=zeros(size(x));
grad(1,10)=-1;
end
end
  1 Kommentar
sxj
sxj am 19 Jun. 2019
Thanks a lot, Matt. It works now. Have a good day

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by