How can I use my own function to optimize using fminunc?

4 ビュー (過去 30 日間)
NooshinY
NooshinY 2017 年 11 月 7 日
コメント済み: Star Strider 2017 年 11 月 7 日
Hi Can anybody help me solving my problem? I have a function in Matlab with two variables x1 and x2 and I wanna optimize my function and find the optimal value of x1 and x2 how can I use fminunc? this is may function :
function f=optim(x)
%%%%%x1 hlow for com 1, x2 for interval
%tau=5;
%H2=0.00125;
H1=0.000125;
CI=5800;
Cp=30000;
Cr=20000;
for j=2:10 % number of intervals
t1(j)=j*x2;
t2(j)=(j-1)*x2;
Inspcost1(j)=MyF(x1,t1(j));
Inspcost2(j)=MyF(x1,t2(j));
Inspcostall(j)=j.*abs((Inspcost2(j)-Inspcost1(j)));
end
Inspcost=CI*sum(Inspcostall(j))
f=Inspecost;
(MyF is another function) I don't know how to use matlab to optimize this?

採用された回答

Star Strider
Star Strider 2017 年 11 月 7 日
In the optimization routines, the parameters you want to optimise have to be in a single vector. In this instance, simply change them to subscripted veriables, ‘x(1)’ and ‘x(2)’:
t1(j)=j*x(2);
t2(j)=(j-1)*x(2);
Inspcost1(j)=MyF(x(1),t1(j));
Inspcost2(j)=MyF(x(1),t2(j));
  3 件のコメント
Walter Roberson
Walter Roberson 2017 年 11 月 7 日
fh=optim(x)
should be
fh = @optim;
Star Strider
Star Strider 2017 年 11 月 7 日
@Walter — Thank you.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with Optimization Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by