MATLAB: Using an output from a function as a constraint in an optimisation problem

optimization

Hi all, thanks in advance for any help received.
I am using optimtool toolbox and have a function that has 2 outputs. For my optimisation i would like the first output of my function to be less than a value and the second output of my function to be minimised?
How would I go about solving this?
Regards Alim

Best Answer

Let's suppose that your function is
[c,fval] = myobj(x)
You want c to be less than 5, say, and fval to be minimized.
Write a nonlinear constraint function
function [c,ceq] = nonlcon(x)
ceq = [];
[c,~] = myobj(x);
c = c - 5;
Your objective function is
function f = fun(x)
[~,f] = myobj(x);
Now this might strike you as wasteful, having to call myobj twice and throwing away half the information each time. To get around this inefficiency, see Objective and Nonlinear Constraints in the Same Function.
Alan Weiss
MATLAB mathematical toolbox documentation
Related Question