fmincon is giving error

4 views (last 30 days)
rabbahs
rabbahs on 11 Nov 2014
Commented: Torsten on 12 Nov 2014
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

Accepted Answer

Torsten
Torsten on 11 Nov 2014
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.

More Answers (3)

pietro
pietro on 11 Nov 2014
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 Comment
rabbahs
rabbahs on 12 Nov 2014
Thanks Pietro for the reply. Please look at my reply below

Sign in to comment.


rabbahs
rabbahs on 12 Nov 2014
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 on 12 Nov 2014
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 Comments
rabbahs
rabbahs on 12 Nov 2014
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 on 12 Nov 2014
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.

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!