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

1 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get 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