fmincon and constraints inherant to the function
Show older comments
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
More Answers (1)
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
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!