MATLAB Answers

FMINCON with multiple constraints

6 views (last 30 days)
Víctor García
Víctor García on 20 May 2021
Commented: Alan Weiss on 20 May 2021
Hello, I am new with the optimization tool of MATLAB and I have two question regarding my optimization function. I have a function with a vector P[1x6] as input and only one scalar output which I want to optimize. Before setting the constraints, I already have a error:
FMINCON requires all values returned by functions to be of data type double.
But my output it's an scalar, so I'm a bit confused. Here my code:
fun = @facturade;
A = []; b = []; Aeq = []; beq = [];
lb = [0 0 0 0 0 0];
ub = [150 150 150 150 150 150];
x0 = [100 100 100 100 100 100];
%nonlcon = @const;
[x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Then, I would like to know how to write multiple constrains for the problem, such as:
P(1)<=P(2); P(2)<=P(3); P(3)<=P(4); P(4)<=P(5); P(5)<=P(6);
For what I've seen, it's posible with nonlcon function, but as P it's my input vector for my function fun, I'm not sure how to write the function.

Sign in to comment.

Accepted Answer

Alan Weiss
Alan Weiss on 20 May 2021
I think that your fracturade function is returning data type SINGLE. Perhaps the quickest fix is to include the following call just before the end of the function:
y = double(y);
A better fix would be to determine why the output is single to begin with and fix that. But calling double will enable you to get on with your work.
Alan Weiss
MATLAB mathematical toolbox documentation
Alan Weiss
Alan Weiss on 20 May 2021
Torsten's comment indicates the reason why: the finite difference steps are too small to get a nonzero gradient estimate. You really need more precision in your calculation.
Barring that, set larger finite difference steps as explained here.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by