Clear Filters
Clear Filters

How to use more than one variable as an input for a NonLinear Constraint function with FMINCON

9 views (last 30 days)
Hello,
I have an optimization problem where the number of constraints are subjected to the length of one variable. I'm writing the constraints with the OR instead of AND constraints, as I need to define several lower and upper bounds. When I explicitly write the constraint function for one single case, the optimization works very well:
function [c,ceq] = ConstraintsOR1 (x)
ceq=[abs(x(2)-x(1))-0.85];
Fx1(1)=minDist1(x(1),0.20,0.55);
Fx1(2)=minDist1(x(1),0.95,1.42);
Fx1(3)=minDist1(x(1),1.78,2.30);
c(1)=min(Fx1);
Fx2(1)=minDist1(x(2),0.20,0.55);
Fx2(2)=minDist1(x(2),0.95,1.42);
Fx2(3)=minDist1(x(2),1.78,2.30);
c(2)=min(Fx2);
But if instead of typing all the values I want to call another variable like this:
function [c,ceq] = ConstraintsOR1 (x,e,t)
ceq=[abs(x(2)-x(1))-e];
u=1:2:length(t);
for i = u
Fx1(i)= minDist1(x(1), t(i), t(i+1));
Fx2(i)= minDist1(x(2), t(i), t(i+1));
end
Fx1=Fx1(u); Fx2=Fx2(u);
c(1)=min(Fx1);
c(2)=min(Fx2);
The fmincon does not run as I'm adding two extra input variables to the constraint function. I don't know how to call these variables without explicitly call them, I also try to add them into the minDist1 function that I used inside the constraint function, but it doesn't work neither.
Any help or suggestion will be very much appreciated. Thank you in advance,
Martha

Accepted Answer

Torsten
Torsten on 17 Feb 2016
Pass your constraint function as
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,@(x)ConstraintsOR1(x,e,t),options)
to fmincon.
Best wishes
Torsten.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!