MATLAB: Fmincon stop criterion for function with multiple outputs

fminconMATLABoptimizationoutput

I have a large complex function that takes in several (15+) arguments, calls local functions to perform many long calculations, and finally outputs several results. Now I want to vary 5 of the input arguments to find the minimum of one output under the constraint that another output is greater than or equal to some value. Here's a simplified example (these equations are just for illustrative purposes, the solution should not depend on them):
function out = myLongFunction(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
[x, y]=localFunc1(a, b, c);
out=localFunc2(x,y);
end
function [x,y]=localFunc1(a,b,c)
x=a*b*c;
y=e^(x/sqrt(8));
end
function out=localFunc2(x,y)
out1=(x/y)^1.4;
out2=x/out1;
out3=sqrt(x*y)*1.4;
out =[out1, out2, out3];
end
In a separate file I have the following code to minimize out1 while out2 is greater than some value under a nonlinear constraint that depends on out3:
function output = optimization(M, ~, ~, ~, ~, ~, f, g, h, i, j, k, l, m, n) % im varying a, b, c, d, and e
ub=[2 2 2 2 2];
lb=[0 0 0 0 0];
X0=[1 1 1 1 1];
A=[];
b=[];
Aeq=[];
beq=[];
function out1=singleOut(x(1), x(2), x(3), x(4), x(5), f, g, h, i, j, k, l, m, n)
out=myLongFunction(x(1), x(2), x(3), x(4), x(5), f, g, h, i, j, k, l, m, n);
out1=out(1);
end
function stop = outfun(x,optimValues,state)
stop=false;
out= myLongFunction(x(1), x(2), x(3), x(4), x(5), f, g, h, i, j, k, l, m, n);
if out(2)<=M % M would be a global variable
stop=true;
end
end
options = optimoptions(@fmincon,'OutputFcn', @outfun);
function [c, ceq]=nonlcon1(x)
out= myLongFunction(x(1), x(2), x(3), x(4), x(5), f, g, h, i, j, k, l, m, n);
c=f*x(1)^2 - g/h*sqrt(x(3))*x(2) + out(3);
ceq=[];
end
function [c2, ceq2]= nonlcon2(x)
out= myLongFunction(x(1), x(2), x(3), x(4), x(5), f, g, h, i, j, k, l, m, n);
c2=i*x(1)^2 - j/k*sqrt(x(3))*x(3) + out(3);
ceq2=[];
end
output=fmincon(func,x0,A,b,Aeq,beq,lb,ub,{@nonlcon1, @nonlcon2},options); % 'output' should be a list of the five variables that result in minimum.
end
I was hoping someone could help me get this working and be as cheap as possible since I have no idea how to use fmincon on a function with multiple outputs and multiple nonlinear constraints. One error I was receiving was something like 'the output function must return a scalar value' which doesn't make sense to me. Any help would be appreciated. Sorry about any spelling/grammar/formatting errors (im on my phone).
See this repo on Github under the "optimization" branch.

Best Answer

I think that you mainly need to realize that optimization toolbox generally asks that you put all of your control variables into one input argument, typically called x. See Writing Scalar Objective Functions. Also, since you have other data or variables that the solver is not supposed to change, include these extra parameters using the techniques of Passing Extra Parameters.
I suggest that you break up your variables into the x = control variables (the five-dimensional x variable that you want fmincon to change) and the extra parameters, and rewrite your functions in terms of x and the others explicitly. Something like
function f = myfun(x,extra)
a = x(1);
b = x(2);
c = x(3);
d = x(4);
e = x(5);
% put the rest of your computations here
Your subsidiary functions, the ones that help compute the objective, can have any inputs and outputs that you like, but for consistency they should use the same x as well as any other variables as inputs.
f is necessarily a scalar output of myfun.
Your nonlinear constraint function similarly needs to be in the correct syntax, such as
function [c,ceq] = nonlcon(x,extra)
c(1) = fun2(x,extra);
c(2) = fun3(x,extra); % Note: no cell arrays here
ceq = [];
Call the functions properly, such as
fun = @(x)myfun(x,extra)
nlc = @(x)nonlcon(x,extra);
[solution,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nlc,options)
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation