why all of my variables become NaN or inf after I run my function

8 ビュー (過去 30 日間)
NooshinY
NooshinY 2017 年 11 月 11 日
回答済み: Walter Roberson 2017 年 11 月 11 日
Hi
I have this function when I run it it gives me the results, but all the variables become zero at the end. I mean when Matlab run it, it gives me the final value for f but I cannot find the value of t1 t2 Inspcost ...., my code is as follow:
function f=optim(x)
%%%%%x1 H2 for com 1, x2 for interval
H1=1.25;
CI=580;
Cp=3000;
Cr=2000;
t1=ones(1,5);
t2=ones(1,5);
for j=2:5 % number of intervals
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));
Inspcostall(j)=(j-1).*abs((Inspcost2(j)-Inspcost1(j)));
end
Inspcost=CI*sum(Inspcostall(j)); %%%%%first item in numerator
for j=2:5 % this is for Intervals
Downcost1(j)=abs(Inspcost2(j)-Inspcost1(j)); %%%%first part in downtime cost
Down211(j)=MyF(H1,t1(j));
Down212(j)=MyF(H1,t2(j));
Down21(j)= t1(j).*abs(Down212(j)-Down211(j)); %%%%(1) in second part in downtime cost
Down22(j)=abs(t1(j).*(1-Down211(j))-t2(j).*(1-Down212(j))); %%%%(2) in second part in downtime cost
for m=1:2
Down23each(j,m)=IntF(H1,m,t2(j),t1(j)); %%%%(3) in second part in downtime cost
end
end
for j=2:5
Down23(j)=sum(Down23each(j,:)); %%%%(3) in second part in downtime cost
Downcost2(j)=Down21(j)-Down22(j)+Down23(j); %%%%total second part in downtime cost
Downall1(j)=Downcost1(j).*Downcost2(j);
end
Downcost=Cp*sum(Downall1); %%%%%second item in numerator
for j=2:5
t1
Lencycle1(j)=t1(j).*abs((Inspcost2(j)-Inspcost1(j)));
end
Lenthcycle=sum(Lencycle1); %%%%%denominator
Totalcost=Inspcost+Downcost+Cr; %%%%total numerator
t1
t2
f=10*Totalcost/Lenthcycle;
|
  • for example for x=[1.1,1] I can find the value of f, but I get this error when I want to find the exact value of my variables, they are undefined or zero:|
>> t2
Undefined function or variable 't2'.
>> Inspcost
Undefined function or variable 'Inspcost'.
t1 =
0 0 0 0 0 0 0 0 0 0

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 11 月 11 日
Every function has its own "workspace". Variables defined in a function that are not specifically constructed as being permanent or somehow shared, are destroyed when the function returns.
In order to examine those values you need to do one of the following:

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by