Error with Fmincon nonlinear constraint

1 次查看(过去 30 天)
Hi! I encounter a problem trying to use fmincon to solve a nonlinear equation system. The message error:
Error in Untitled (line 12)
F=@(n) [n(1) + n(2) + n(3) + n(4) + n(5) + n(6) + n(7) + n(8) + w - n(9);...
Error in fmincon (line 639)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Error in Untitled (line 28)
n=fmincon(@(x)0,x0,[],[],[],[],lb,ub,[[],F],opts);
Caused by:
Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.
Hereunder you can find a simplified version of my code that returns that error as well. This being the first time I use fmincon, I assume I made a basic syntax error, can anyone help me with this?
nIt = 1;
for ii=1:nIt
% K01 = ;
% K02 = ;
% K03 = ;
% K04 = ;
%
% a = ; b = ; c = ;
% u = ; v = ; w = ; x = ; y = ; z = ;
F=@(n) [n(1) + n(2) + n(3) + n(4) + n(5) + n(6) + n(7) + n(8) + w - n(9);...
1 - (n(3) * n(2) / (n(1) * n(4)))/K01;...
1 - (n(7) / (n(8) * n(6)) * n(9) / u)/K02;...
1 - (n(2) / (n(5) * n(6)) * n(9) / u)/K03;...
1 - (n(6)^2 / n(4) * u / n(9))/K04;...
a*x - n(1) - n(3);...
4*(c) + a*y - 2*n(2) - 2*n(4) - n(5) - n(6) - n(7);...
c - n(7) - n(8);...
4*(c) + a*z - 2*n(1) - n(2) - n(3) - n(5) - 3*v];
lb = zeros(1,9); % Lower bound constraint
ub = 10*ones(1,9); %upper bound constraints
rng default % Reproducible initial point
x0 = exprnd(1,9); %initial values
opts = optimoptions(@fmincon,'Algorithm','interior-point','Display','off');
n=fmincon(@(x)0,x0,[],[],[],[],lb,ub,[[],F],opts);
end

采纳的回答

Alan Weiss
Alan Weiss 2020-3-30
Try this, as documented:
nlcon = @(x)deal([],F);
Alan Weiss
MATLAB mathematical toolbox documentation
  2 个评论
Brian Noiret
Brian Noiret 2020-3-30
Thank you very much, it works very well!
Alan Weiss
Alan Weiss 2020-3-30
Since it helped, please "accept" the answer.
Alan Weiss
MATLAB mathematical toolbox documentation

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by