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

4 vues (au cours des 30 derniers jours)
NooshinY
NooshinY le 7 Nov 2017
Commenté : Star Strider le 7 Nov 2017
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?

Réponse acceptée

Star Strider
Star Strider le 7 Nov 2017
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 commentaires
Walter Roberson
Walter Roberson le 7 Nov 2017
fh=optim(x)
should be
fh = @optim;
Star Strider
Star Strider le 7 Nov 2017
@Walter — Thank you.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with Optimization Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by