-1

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.

Frey
  • 315
  • 4
  • 11

1 Answers1

0

I'd prefer to write some function like this:

Instead you should write some MATLAB function which takes in a vector and returns a scalar, where the value of the returned scalar is the value of your function. Create a separate matlab file (eg. myfunction.m) and do something like:

function result = myfunction(L)

M = length(L);

log_one_minus_L = log(1 - L);
log_L           = log(L);
log_one_plus_L  = log(1 + L);


sumsum_templ    = (M-1:-1:1) * log_one_minus_L(1:end-1);
result = sum(log_L - log_one_plus_L) + sumsum_templ;

Make sure your function myfunction works by testing on various values. Eg. myfunction([0; 1]) and see if it returns the right value.

To optimize, then you can call fmincon(myfunction, ...) fmincon requires a pointer to a function that takes in a vector and returns a scalar.

Matthew Gunn
  • 4,451
  • 1
  • 12
  • 30
  • Can you please tell me (directly) how this can be applied to my case since I have variable `M`? – Frey Nov 20 '15 at 22:25