MATLAB Answers

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

조회 수: 3(최근 30일)
艳晖 刘
艳晖 刘 2021년 7월 8일
댓글: Walter Roberson 2021년 7월 9일
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
Fangjun Jiang
Fangjun Jiang 2021년 7월 8일
@Steven Lord, thank you for the note! I didn't know it and learned this today!

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

Community Treasure Hunt

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

Start Hunting!

Translated by