I started with following simple optimization problem:
F=@(L) max(-[log(L(1)/(L(1)+1)) log(L(2)/(L(2)+1))+log(1-L(1)) log(L(3)/(L(3)+1))+log(1-L(1))+log(1-L(2))]);
[L,fval]=fmincon(F,[0.5 0.5 0.5],[],[],[],[],[0;0;0],[1;1;1])
This gives answers: L = 0.2554 0.3759 0.7785
and fval = 1.5925
However, I need variable number of input functions for Obj
in max(-[Obj])
. In above example, I have three functions, i.e.,
Obj=log(L(1)/(L(1)+1)) log(L(2)/(L(2)+1))+log(1-L(1)) log(L(3)/(L(3)+1))+log(1-L(1))+log(1-L(2))
I used following code to generate Obj
in which the number of functions depends on M
.
M = 3;
for i = 1:M
L(i) = sym(['L(' num2str(i) ')'])
end
tempL = log(1-L);
for m=1:M
Obj(1,m) = log((L(m))/(1+L(m))) + sum(tempL(1:m-1));
end
Obj
This exactly gives the same Obj
as above example, but when I pass this Obj
to following optimization function, it does not support.
F=@(L) max(-[Obj]);
[L,fval]=fmincon(F,[0.5 0.5 0.5],[],[],[],[],[0;0;0],[1;1;1])
Can someone help me to fix this issue? Because M
can vary more than 20. It is difficult to enter all functions manually.