fmincon and constraints inherant to the function

5 views (last 30 days)
Good evening,
I am currently stuck on a problem and till now couldn't find a solution to it.
I'm using fmincon to find the minimum of a cost function forecastSOC, with variables x(1) and x(2):
[z1,z2,z3,z4] = fmincon(@(x)forecastSOC(x,aConst),x,A,b,Aeq,beq,lb,ub);
Now, what I would like to do is add a constraint to a variable y (vector) that is calculated in my function (y depends x(1) and x(2)). For example let's say the values of y should always stay between 60 and 90.
I've tried to included y(x(1),x(2)) as a decision parameter, then add upper and lower boundaries; I also tried non linear constraints.. with no success.
I hope very much that you can help me. Thank you
kind regards

Accepted Answer

Matt J
Matt J on 29 May 2014
I also tried non linear constraints
If y is a nonlinear (and twice differentiable) function of x, there's no obvious reason this wouldn't have worked.
  3 Comments
Matt J
Matt J on 29 May 2014
Edited: Matt J on 29 May 2014
I don't fully understand the idea of "adding y as variables". Since y depends on the other variables the only way you can recast it as an independent variable in the optimization is to add nonlinear equality constraints,
x(3:27)=y(x(1),x(2))
which are again not differentiable because y() is not differentiable.
Incidentally, the constraints are not your only problem. Since y() is an intermediate quantity in the computation of your objective function, that will be non-differentiable as well. I think you need to find a better formulation of y() than one which uses ceil() operations.
John D'Errico
John D'Errico on 29 May 2014
Fmincon does NOT handle problems that are not differentiable. In fact, if you used ceil, then it is not even continuous.

Sign in to comment.

More Answers (1)

Alan Weiss
Alan Weiss on 29 May 2014
It depends on how you tried to include the nonlinear constraints. I believe the way to do so successfully is as follows:
function [c,ceq] = constrfun(x)
ceq = [];
% Put your code for calculating y here
c(1) = 60 - y; % ensures y >= 60
c(2) = y - 90; % ensures y <= 90
Be sure to pass @constrfun after the ub argument to fmincon.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 Comments
Christopher
Christopher on 29 May 2014
Edited: Christopher on 29 May 2014
But y has to be twice differentiable right ?
And in my case y is a vector (25x1). How can I have thoses two constraints on my y values since c cannot be a matrix ?
Matt J
Matt J on 29 May 2014
Edited: Matt J on 29 May 2014
c and ceq can be of any size and shape. They certainly do not have to be scalars.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!