fmincon and constraints inherant to the function

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

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

Comment by Christopher:
Thanks for your quick answer Matt.
I'm not sure but I think my function y is indeed not differentiable (I used some "ceil approximation" in it) . If it is not, do you see another way I could add that interval constraint?
What do you think of the idea of adding y as variables of the function ? My x(1) and x(2) are the parameters I want to optimize. Then I added x(3) to x(27) (25 values of y(which I repeat is calculated with x(1) and x(2) )) and tried to simply constrain those values between my desired interval (lb and ub). I cannot figure out if something is wrong or not with this procedure..
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.
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)

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

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.

Asked:

on 29 May 2014

Edited:

on 29 May 2014

Community Treasure Hunt

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

Start Hunting!