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

조회 수: 2 (최근 30일)
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)

채택된 답변

Walter Roberson
Walter Roberson 2021년 7월 8일
fun=@nonlcon;
  댓글 수: 4
Walter Roberson
Walter Roberson 2021년 7월 9일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2021년 7월 8일
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
Steven Lord
Steven Lord 2021년 7월 8일
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.)
Fangjun Jiang
Fangjun Jiang 2021년 7월 8일
@Steven Lord, thank you for the note! I didn't know it and learned this today!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Solver-Based Nonlinear Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by