필터 지우기
필터 지우기

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

조회 수: 1 (최근 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개)

Community Treasure Hunt

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

Start Hunting!

Translated by