Problem with optimization using fmincon

I need to optimize function by finding the maximum of this function: J = F [kg/min] – 0,007 [kg/(min K)] * T [K], where F=[0:4], T=[300:360]. I think that its need to be done by using fmincon. I tried to use it but despite of changing starting point the result its not changing.
fun = @(x) -x(1)+0.007*(-x(2));
lb = [0, 300];
ub = [4, 360];
A = [];
b = [];
Aeq = [];
beq = [];
% x0 = (lb + ub)/2;
x0 = [0, 300];
% x0 = x0/5;
[x, fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x = 1×2
4.0000 359.9999
fval = -6.5200
fun = @(x) -x(1)+0.007*(-x(2));
lb = [0, 300];
ub = [4, 360];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = (lb + ub)/2;
%x0 = [0, 300];
% x0 = x0/5;
[x, fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x = 1×2
4.0000 359.9999
fval = -6.5200
Maybe i should try another function? or i was doing something wrong?

2 Comments

Torsten
Torsten on 6 Jun 2022
Edited: Torsten on 6 Jun 2022
It's the unique minimum of the function to be minimized.
So the solution does not change when you change the initial guess.
By the way: Your problem is a linear optimization problem. You should use "linprog" to solve.
There is one additional constraint thats why i thought about using initial point. The constraint its Cb=0.5 (Ca=Cb). Where
0=((1-Ca)*F/W)-k1*Ca
and
0=((-Cb*F)/W)+(k1*Ca)-(k2*Cb).
W=[0:0.1:100], k1=9000, k2=35000
So probably if i need to use linprog i need to transform equation so i will put initial point in it?

Sign in to comment.

 Accepted Answer

It looks like there are supposed to be some additional, more interesting constraints. Otherwise, if you have only bounds, the minimization of a linear function is trivial and can be done by inspection. You don't need any fancy iterative solvers.

3 Comments

There is one additional constraint thats why i thought about using initial point. The constraint its Cb=0.5 (Ca=Cb). Where
0=((1-Ca)*F/W)-k1*Ca
and
0=((-Cb*F)/W)+(k1*Ca)-(k2*Cb).
W=[0:0.1:100], k1=9000, k2=35000
also i tried now with linprog but in this case i cant use initial point
f = [1 -0.007];
% fun = @(x) -x(1)+0.007*(-x(2));
lb = [0, 300];
ub = [4, 360];
A = [];
b = [];
Aeq = [];
beq = [];
% x0 = (lb + ub)/2;
x0 = [0, 300];
% x0 = x0/5;
[x, fval] = linprog(-f, A, b, Aeq, beq, lb, ub)
Optimal solution found.
x = 2×1
4 300
fval = -1.9000
As you can see, linprog is giving you the same solution as fmincon. The reason is that there is again only a unqiue and trivial solution. If you have additional constraints, you should incorporate them.
fun = @(x) -x(1)+0.007*(+x(2));
lb = [0, 300];
ub = [4, 360];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [0, 300];
[x, fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x = 1×2
4.0000 300.0001
fval = -1.9000
Torsten
Torsten on 7 Jun 2022
Edited: Torsten on 7 Jun 2022
Your additional constraints are not clear to me, especially what the unknowns are and what they have to do with x(1) and x(2).

Sign in to comment.

More Answers (1)

Sam Chak
Sam Chak on 7 Jun 2022
Edited: Sam Chak on 7 Jun 2022
Sometimes, the optimization problem can be understood better if you can visualize the objective function:
If the function is merely a planar surface in this case, all you need to do is to inspect the 4 corners and find the maximum of this function.
[X, Y] = meshgrid(0:4/40:4, 300:60/40:360);
Z = X - 0.007*Y;
surf(X, Y, Z)
Also, the first equality constraint simplified to
0 = ((1 - 0.5)*F/W) - 9000*0.5
and the second equality constraint simplified to
0 = ((-0.5*F)/W) + (9000*0.5) - (35000*0.5)
where W is from 0 to 100. Since F is bounded in [0, 4], can you see a way to achieve that?

2 Comments

If the function is merely a planar surface in this case, all you need to do is to inspect the 4 corners and find the maximum of this function.
Even simpler, the planar function is additively separable into two 1D linear functions, f(x)= f1(x1)+f2(x2). So, for each of f1 and f2, it is enough to inspect just the 2 endpoints of their domains. This leads to the purely analytical solution,
f = -[1 -0.007];
lb = [0, 300];
ub = [4, 360];
x=lb;
x(f<0)=ub(f<0),
x = 1×2
4 300
Thanks @Matt J. Learned a simpler approach today. 👍

Sign in to comment.

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!