how to define if statement for fmincon function / optimization problem

8 ビュー (過去 30 日間)
Sara
Sara 2017 年 11 月 17 日
コメント済み: Sara 2017 年 11 月 20 日
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!

採用された回答

Matt J
Matt J 2017 年 11 月 17 日
編集済み: Matt J 2017 年 11 月 17 日
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;
  1 件のコメント
Sara
Sara 2017 年 11 月 20 日
Thank you very much for your help Matt! The problem is now solved by using your method.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurrogate Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by