Fmincon - Variable dependent constraints

Hello,
I'm sorry if this question has already been answered.
I want to find the minimum of a function of three variables f(x,y,z), where the constraints of the variables are defined as follows (for example) :
  • 1 < x < 2
  • x/20 < y < x/10
  • 2y < z < x/5
I would be greatful if someone could help me about how to define this by using the fmincon function.
Thanks in advance!

 Accepted Answer

lb = [1, 1/20, 2/20]; ub = [2, 2/10, 2/5];
A = [
1/20, -1, 0
-1/10, 1, 0
0, 2, -1
-1/5, 0, 1
]
b = [
0
0
0
0
]
Aeq = []; beq = [];
xyz = fmincon(@OBJECTIVE, A, b, Aeq, beq, lb, ub)
Unless, that is, you need your endpoints to be strict inequalities. If you do, b would end up containing some +/- eps(realmin)

4 Comments

BraF
BraF on 12 May 2021
Edited: BraF on 12 May 2021
Hi Walter,
Thanks a lot ! Concerning the inequalities, it's okay if they're not strict so that's all good !
I've got some trouble understanding how the matrix A was constructed, and since the constraints above where just an example, I've got some difficulties re-creating it.
I have (omitting strict inequalities) :
  • 0.05 < x < 0.5
  • x/30 < y < x/10
  • 5y < z < x/4
What does A become ? Thanks again!
P.S. Lb and ub become : lb = [0.05, 0.05/30, 5/30], ub = [0.5, 0.5/10, 0.5/4], right ?
x/30 < y < x/10
rewrite that into two parts,
x/30 < y
y < x/10
Now move the right sides to the left by subtraction, and reorder into consistent < order
x/30 - y < y - y
y - x/10 < x/10 - x/10
so
1/30*x - 1*y < 0
-1/10*x + 1*y < 0
and read off the x y z coefficients to get the A matrix entries
1/30, -1, 0
-1/10, 1, 0
and the b entries are the constants to the right of the < -- so [0; 0]
Everything is clear now, thanks a lot to both of you!
And just to make sure, lb and ub are correctly defined now (with these new constraints) ?
I've got lb = [0.05, 0.05/30, 5/30], ub = [0.5, 0.5/10, 0.5/4].
You have x/30 < y < x/10 so if the lower bound for x is 0.05 then the lower bound for y is (as you calculated) 0.05/30 . But then you have 5y < z < x/4 so you should take 5 times the y lower bound as the z lower bound, which would get you to 5*0.05/30
Upper bound, x is max 0.5, y is up to x/10 which would be 0.5/10 (as you calculated). z upper bound is up to x/4 and x is up to 0.5, so z upper bound would be 0.5/4 (as you calculated)
So you were mostly right, but your lower bound on z was wrong.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 12 May 2021
Edited: Matt J on 12 May 2021
If you wish, you can download prob2matrices() and use the problem-based framework to help set up the linear constraints.
x=optimvar('x','LowerBound',1, 'UpperBound',2);
y=optimvar('y');
z=optimvar('z');
con.xyleft= x/20<=y;
con.xyright= y<=x/10;
con.yzleft= 2*y <= z;
con.yzright= z<=x/5;
[S,idx]=prob2matrices({x,y,z}, 'Constraints',con);
xyz=fmincon(@objective, [x0,y0,z0], S.Aineq,S.bineq,S.Aeq,S.beq,S.lb,S.ub)

Community Treasure Hunt

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

Start Hunting!