fmincon is giving error
5 ビュー (過去 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.
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!