How can I use outputs of the objective function as the equality constraints in fmincon

8 views (last 30 days)
I have a function: function [power,imbalance]=obj(trim) which I shan't include as it's very long.
trim=[rotorspeed bladepitch diskangle] and these parameters (along with some constants) are used to calculate the amount of power required (power); the difference between vertical thrust and weight (imbalance(1)) and the difference between horizontal thrust and drag (imbalance(2)).
What I want to do is minimise the power subject to imbalance=[0 0] but I'm struggling to implement it with fmincon. What I currently have is a very bad bodge. I have 2 copies of the function: 1 only outputs power and is the objective function and the other is used for the @nonlcon option where imbalance is used as equality constraint (ceq).
Ideally I'd like to have just one master function for both but as fmincon uses the first output of the objective function to minimise and it uses the first 2 outputs of the @nonlcon function for the contraints I can't; unless I have a way of differentiating between whether the function is being used as an objective function or as a @nonlcon function.
  1 Comment
jgg
jgg on 19 Jan 2016
I don't know if I can completely solve your problem, but you could do the following:
function [power] = objective(trim)
[power,~] = obj(trim);
end
function [imbalance] = constraint(trim)
[~,imbalance] = obj(trim);
end
(You can also wrap this in a function that selects the output, but I think that's un-necessary and hurts readability)
From here, you can call as usual. While this will certainly reduce the amount of code hanging around, it won't really save you all that much computational overhead.
A suggestion I thought might work is to set
global power imbalance
in your obj function then do the following:
function [imbalance] = constraint(trim)
global imbalance
end
I'm not sure if this will work, since it depends when the constraint is called in the solver; but basically, you can exploit the fact that you can store variables globally to get the value of imbalance out of the objective function without evaluating it again.

Sign in to comment.

Answers (1)

Alan Weiss
Alan Weiss on 19 Jan 2016
It is possible that the documentation of Objective and Nonlinear Constraints in the Same Function has the information you seek.
Alan Weiss
MATLAB mathematical toolbox documentation

Categories

Find more on Get Started with Optimization Toolbox 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!