fmincon error: Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
Show older comments
Hi,
I am a beginner using Matlab and trying to solve a constrained nonlinear optimization problem. I got the followings error msg:
??? Error using ==> @(x)SqErr1(x)+SqErr2(x)
Too many input arguments.
Error in ==> fmincon at 517
initVals.f = funfcn{3}(X,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
But if I try to compute the objective function directly with a feasible x0, Matlab always returns a correct scalar answer. I am really confused... Could you please help me with it? Thank you.
Below is my code, the objective function is a bit complicated...
for i=1:12
fhNames{i}=['DeltaEu',num2str(i)];
fhNames2{i}=['DeltaEu2',num2str(i)];
end
fhArray=cellfun(@str2func, fhNames, 'UniformOutput',false);
fhArray2=cellfun(@str2func, fhNames2, 'UniformOutput',false);
Eu=@(x,s) (exp(-x(2)*i)/x(1)*mean((s(:,1).^x(3)-s(:,2)).^x(1)-(s(:,1).^x(3)-...
s(:,3)).^x(1)));
for i=1:12
fhArray{i}=@(x) Eu(x,S{i}(:,1:3));
fhArray2{i}=@(x) Eu(x,S{i}(:,[1,3,4]));
end
SqErr1=@(x) (fhArray{1}(x)+fhArray{2}(x)+fhArray{3}(x)+fhArray{4}(x)+...
fhArray{5}(x)+fhArray{6}(x)+fhArray{7}(x)+fhArray{8}(x)+...
fhArray{9}(x)+fhArray{11}(x)+fhArray{10}(x)+fhArray{12}(x))^2;
SqErr2=@(x) (fhArray2{1}(x)+fhArray2{2}(x)+fhArray2{3}(x)+fhArray2{4}(x)+...
fhArray2{5}(x)+fhArray2{6}(x)+fhArray2{7}(x)+fhArray2{8}(x)+...
fhArray2{9}(x)+fhArray2{11}(x)+fhArray2{10}(x)+fhArray2{12}(x))^2;
ObjFun=@(x) SqErr1(x)+SqErr2(x);
nonlcon=@(x) PositiveCon(x,MaxCost,MinVol');
function [c, ceq]=PositiveCon(x, MaxCost, MinVol)
c = MaxCost-MinVol.^x(3);
ceq = [];
end
[x, fval, exit, output]=fmincon(ObjFun, x0, [], [], [], [],lb, ub,[],...
nonlcon, optimset('FunValCheck','on'));
Accepted Answer
More 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!