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).
Best Answer