Insufficient number of outputs from right hand side of equal sign to satisfy assignment

2 Ansichten (letzte 30 Tage)
I've seen other questions and answers on this topic, but could not find the answer to my problem.
function [c,ceq] = nonlcon(r)
s = [1 2 0.18 0.0997;
2 3 0.3 0.102;
3 4 0.5 0.1045;
4 5 0.5 0.1064];
vv = 6;
ceq(1) = (s(1,1)-r(1)).^2+(s(1,2)-r(2)).^2+(s(1,3)-r(3)).^2-vv.*(s(1,4)-r(4)).^2==0;
ceq(2) = (s(2,1)-r(1)).^2+(s(2,2)-r(2)).^2+(s(2,3)-r(3)).^2-vv.*(s(2,4)-r(4)).^2==0;
ceq(3) = (s(3,1)-r(1)).^2+(s(3,2)-r(2)).^2+(s(3,3)-r(3)).^2-vv.*(s(3,4)-r(4)).^2==0;
ceq(4) = (s(4,1)-r(1)).^2+(s(4,2)-r(2)).^2+(s(4,3)-r(3)).^2-vv.*(s(4,4)-r(4)).^2==0;
c = [] ;
end
r0 = [2.5 5 0 0];
ob =@(r)(r(1)-3).^2+(r(2)-4).^2+(r(3)-0.18).^2;
fun=@(r)@nonlcon;
[x,fval,exitflag,output] = fmincon(ob,r0,[],[],[],[],[],[],fun)
Why i am getting this error? Hope someone can enlighten me.
Error jisuan>@(r)@nonlcon (Line 10)
fun=@(r)@nonlcon;
Error fmincon (Line 654)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Error jisuan (Line 11)
[x,fval,exitflag,output] = fmincon(ob,r0,[],[],[],[],[],[],fun)

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 8 Jul. 2021
fun=@nonlcon;
  4 Kommentare
Walter Roberson
Walter Roberson am 9 Jul. 2021
Note that your constraints have four equations in four variables, so they resolve down to two particular solutions. The only question then is whether one of the sets of solutions is better than the other, and the answer to that is No, they are both the same.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Fangjun Jiang
Fangjun Jiang am 8 Jul. 2021
nonlcon() is a defined function so fun=@nonlcon should be enough. It might be easier if the function has only one return variable. The returned "c" is empty anyway so you might just delete "c" in nonlcon()
  2 Kommentare
Steven Lord
Steven Lord am 8 Jul. 2021
The returned "c" is empty anyway so you might just delete "c" in nonlcon()
No, that would not work. fmincon imposes some requirements on the nonlinear constraint function you pass into it. The nonlinear constraint function must return two outputs, c and ceq, containing the nonlinear inequality and nonlinear equality constraints on the problem respectively. Returning an empty c and a nonempty ceq indicates you have nonlinear equality constraints but no nonlinear inequality constraints.
If you omitted the c output fmincon would treat the nonlinear equality constraints as nonlinear inequality constraints. As one example solving a problem where the solution vector is required to have a norm of exactly 1 (on a unit circle) can have a different solution than one where the solution vector is required to have a norm no greater than 1 (inside a unit circle.)

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by