how to define if statement for fmincon function / optimization problem

8 views (last 30 days)
Hello, Can anyone help me with below problem please?
I am using fmincon to do the optimization by Matlab. I have a 'price' array p (1x24) that has some zero elements. I want to define a condition that if an element of the price array p is zero, then the same element of solution array x be zero too. I define it in the objective function file as:
function f = objfun(x,p)
x1=x(1:24);
for i=1:24
if p(i)==0
x(i)=0;
end
end
f=x1*p';
end
However, when I run my main file, this condition is not observed in the answer. In other words, p(2) is zero, but x(2) has a non-zero value. I also defined this condition in the main file, but the answers are the same (the condition is not observed)
How can I define this condition for fmincon?
Thanks a lot!

Accepted Answer

Matt J
Matt J on 17 Nov 2017
Edited: Matt J on 17 Nov 2017
If you know certain x(i) are zero in advance, then they are not unknowns, and you should remove them from the problem. So, it should look like
idx=(p~=0);
z0=x0(idx); %discard irrelevant initial guess parameters
z=fmincon(@(z) z*p(idx).' , z0, A, b, Aeq,beq,...);
x=zeros(size(p));
x(idx)=z;

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!