fmincon is giving error
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi everyone, I have a very simple problem to solve 3 linear and one non linear equation using fmincon. I am using fmincon as I need to constrain my solution between lower and upper bound.
I write a function file as,myfun.m
______________________________________________________________________
function F=myfun(x,H2O,alpha,O2,K)
F=[x(2)+x(3)+x(4)-1;
2*x(1)+4*x(4)-H2O-2*alpha;
x(2)+2*x(3)-O2-alpha;
K*x(4)-x(1)^2*(x(1)+x(2)+x(3)+x(4));
];
________________________________________________________________________
and the main file as, model.m, ______________________________________________________________________
H2O=0.8137;
O2=0.0785;
alpha=0.1;
K=1000;
x0=[.1,.1,.1,.1];
f = @(x)myfun(x,H2O,alpha,O2,K);
[x,fval]=fmincon(f,x0,[],[],[],[],[0;0;0;0],[1;1;1;1])
______________________________________________________________________
but fmincon is continuously giving error,as
Error using fmincon (line 618)
User supplied objective function must return a scalar value.
Error in model (line 10)
[x,fval]=fmincon(f,x0,[],[],[],[],[0;0;0;0],[1;1;1;1]);
I am using Matlab R2014b version.
Any help with be highly appreciable.
Thanks
댓글 수: 0
채택된 답변
Torsten
2014년 11월 11일
Define your linear and nonlinear equations as constraints for fmincon, not as parts of the objective function.
For the objective function, simply use f=@(x)0
Best wishes
Torsten.
댓글 수: 0
추가 답변 (3개)
pietro
2014년 11월 11일
fmincon must be used for monoobjective optimization so it expect a scalar value instead in your code he gets four different values. you should use any multiobjective optimization solver or converting to a monobjective problem by summing all elements of vectore F, like the following:
function F=myfun(x,H2O,alpha,O2,K)
F=[x(2)+x(3)+x(4)-1;
2*x(1)+4*x(4)-H2O-2*alpha;
x(2)+2*x(3)-O2-alpha;
K*x(4)-x(1)^2*(x(1)+x(2)+x(3)+x(4));
];
F=sum(F);
end
Torsten
2014년 11월 12일
As general solution for your above linear system I get
x1 arbitrary
x2=x1+1.31285
x3=-0.5*x1-0.567175
x4=-0.5*x1+0.254325
As you can see: whatever positive number you choose for x1 - the variable x3 will always be negative. So your problem has no feasible solution.
By the way:
If you modify your problem, choose the objective function to be zero - it will make things easier for the solver.
Best wishes
Torsten.
댓글 수: 2
Torsten
2014년 11월 12일
No.
I mean that for the linear constraints you defined above, there is no feasible solution with nonnegative components. Adding the nonlinear constraint won't heal this.
You will have to go back to the roots and check your problem definition again.
Best wishes
Torsten.
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear Least Squares에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!