필터 지우기
필터 지우기

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