fmincon is giving error

5 ビュー (過去 30 日間)
rabbahs
rabbahs 2014 年 11 月 11 日
コメント済み: Torsten 2014 年 11 月 12 日
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

採用された回答

Torsten
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.

その他の回答 (3 件)

pietro
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
  1 件のコメント
rabbahs
rabbahs 2014 年 11 月 12 日
Thanks Pietro for the reply. Please look at my reply below

サインインしてコメントする。


rabbahs
rabbahs 2014 年 11 月 12 日
Thanks Pieito and Torson for the reply, and I really appreciate you both to guide me.
so what I did
1) make an objfun.m file to define a funtion
function f=objfun(x,K)
f=(x(1)^2*(x(1)+x(2)+x(3)+x(4)))-(K*x(4));
2) make an main.m file to write the script,
L=5000;
xx=0.8173;
yy=0.0785;
alpha=0.1;
f=@(x)objfun(x,L);
x0=[.1 0.2 0.3 0.4];
Aeq=[0 1 1 1
2 0 0 4
0 1 2 0
];
beq=[1
xx+2*alpha
yy+alpha
];
lb=[0 0 0 0];
options = optimoptions('fmincon');
options = optimoptions(options,'Display', 'iter');
options = optimoptions(options,'Algorithm', 'active-set');
[x,fval,exitflag]=fmincon(f,x0,[],[],Aeq,beq,lb,[],[],options)
but it is still unable to find the feasible solution and give return the following msg,
No feasible solution found.
fmincon stopped because the size of the current search direction is less than twice the default value of the step size tolerance but constraints are not satisfied to within the default value of the constraint tolerance.

Torsten
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 件のコメント
rabbahs
rabbahs 2014 年 11 月 12 日
Hi Torsten,
Should I put (x(1)^2(x(1)+x(2)+x(3)+x(4)))-(K*x(4)) as a nonlinear constraint ? and put f=0 in objfun.m ? it that what you mean ?
best wishes
Torsten
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 ExchangeSolver Outputs and Iterative Display についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by