Taking part in the boundary calculation from the function

Dear All,
I have a question regarding optimization calculations. I would like one of the boundary variables (M) to be calculated from the main function. Specifically, I have a program where I want to determine the value of M using the main function , and I would like to set this value as the upper boundary. Additionally, this value of M will change in each iteration based on the suggested value of x.
function [EFFT]=optimizpandy(x)
A1=x(1);
A2=x(2);
A3=x(3);
M=A1+A2;
EFFT=A1+A2^2+A3;
end
x0=[0.0019998 0.023 0.0019998 ];
lb=[0.001 0.02 0.001 ];
ub=[0.004 0.08 (M)-0.0001 ];
x= fmincon(@optimizpandy,x0,[],[],[],[],lb,ub);

 Accepted Answer

So you want to ensure that x(3) <= x(1)+x(2)-0.0001? That's not a bound, that's a linear constraint.
Write it as -x(1)-x(2)+x(3) <= -0.0001 and specify it using the A and b inputs to fmincon.

4 Comments

Thank you for your reply and suggestion . I want M to be calculated during the optimization process. My program is more complex than this example. Although this example may seem easy, I use it only to explain my problem.
lb and ub for x(3) must be constants and mustn't depend on solution variables. As correctly noted by @Steven Lord, you have to formulate this restriction as a linear constraint:
x0 = [0.0019998 0.023 0.0019998 ];
lb = [0.001 0.02 0.001 ];
ub = [0.004 0.08 Inf ];
A = [-1 -1 1];
b = -0.0001;
x = fmincon(@optimizpandy,x0,A,b,[],[],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×3
0.0010 0.0200 0.0010
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function [EFFT]=optimizpandy(x)
A1=x(1);
A2=x(2);
A3=x(3);
EFFT=A1+A2^2+A3;
end
Matt J
Matt J on 24 Jun 2026 at 17:39
Edited: Matt J on 24 Jun 2026 at 18:48
I want M to be calculated during the optimization process. My program is more complex than this example.
If you mean that M(x) is, more generally, a nonlinear function of x, then the constraints are nonlinear, and you must express them using the nonlcon argument,
I will try to use this note. Thank you!

Sign in to comment.

More Answers (0)

Products

Release

R2025a

Asked:

on 24 Jun 2026 at 8:29

Edited:

on 24 Jun 2026 at 18:48

Community Treasure Hunt

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

Start Hunting!