ERROR: FMINCON requires all values returned by functions to be of data type double.

20 次查看(过去 30 天)
I have this function evalQ_test:
function Q = evalQ_test(x)
%%%
%%% Q_1 = x(1)+x(2);
%%%
Q=double(-(Q_1+Q_2+Q_3));
end
I omitted most of what the real content of the function here by %%%.
I have this objective function:
function [f, g, H] = evalQ_1(x)
% Calculate objective f
f = @(x) evalQ_test(x);
if nargout > 1 % gradient required
g = [f([x(1)+delta;x(2)])-f([x(1);x(2)]);f([x(1);x(2)+delta])-f([x(1);x(2)])];
if nargout > 2 % Hessian required
H = [x(1),0;0,x(2)];
end
end
And Hessian function:
function Hout = hessianfcn(x,lambda)
delta = 10^(-6);
% Hessian of objective
H = [x(1),0;0,x(2)];
% Hessian of nonlinear inequality constraint
Hg = 2*eye(2);
Hout = H + lambda.ineqnonlin*Hg;
Suppose the gradient and hessian are calaulated correctly here.
Then I run:
fun = @evalQ_1;
x0 = [1;1];
options = optimoptions('fmincon','Algorithm','interior-point',...
'SpecifyObjectiveGradient',true,'SpecifyConstraintGradient',true,...
'HessianFcn',@hessianfcn);
[x,fval,exitflag,output] = fmincon(fun,x0,[],[],[],[],[],[],[],options);
However, I have the error:
"Error using fmincon (line 694)
FMINCON requires all values returned by functions to be of data type double."
Is it because I am using a nested objective function? Or anything else?
Thanks in advance!!

采纳的回答

Ameer Hamza
Ameer Hamza 2020-3-29
Your objective function evalQ_1 is returning a function handle
f = @(x) evalQ_test(x);
whereas fmincon expect the first output to be a numeric value. Try something like this
function [f_, g, H] = evalQ_1(x)
% Calculate objective f
f = @(x) evalQ_test(x);
f_ = f(x);
if nargout > 1 % gradient required
g = [f([x(1)+delta;x(2)])-f([x(1);x(2)]);f([x(1);x(2)+delta])-f([x(1);x(2)])];
if nargout > 2 % Hessian required
H = [x(1),0;0,x(2)];
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by