Why I receive a NaN value

1 view (last 30 days)
BOWEN LI
BOWEN LI on 2 Aug 2019
Commented: BOWEN LI on 2 Aug 2019
Hi everyone, i met a problem when i try to check my following code which is a function file, when i try with some inputs, Matlab generates NaN which made me confused.
%set objective functions
function f =totalcost(y,yi,sij)
%set constants for the equations
r1=0.05;%annual demand growth rate
r2=0.1;%annual demand growth rate caused by the completion of rail routes
d=[0 20 30 35].';%di distance beween links 1-4. d1=0 since link 0-1 does not exist.
ub=10;%cost of buses in $/hour
nc=6;%number of cars per train
uc=50;%cost of rail cars in $/car
L=500;%rail maintenance cost in $/mile
k=1000;%rail construction cost in $/mile
td=0.08;%dwell time for bus and rail in hours
vb=40;% bus operating speed
vr=60;% train operating speed
u=10; %user value of time in dollars per hour
N=4;
qijTemp=randi(10,4);
qij0=tril((qijTemp-diag(diag(qijTemp))),-1)+((tril(qijTemp-diag(diag(qijTemp)),-1))).' ;
qij=zeros(4,4,4);
hb1=zeros(4,1);
hb2=zeros(4,1);
hb=zeros(4,1);
hr=zeros(4,1);
cu=zeros(4,1);
ci=zeros(4,1);
rb=zeros(4,1);
rr=zeros(4,1);
cv=zeros(4,1);
cm=zeros(4,1);
cc=zeros(4,1);
f=zeros(4,1);
for t=1:N
if t==1
qij(:,:,t)=qij0;
yi(:,:,t)=zeros(4);
sij(:,:,t)=zeros(4);
cc(t)=y(:,1,t).'*d*k;
else
qij(:,:,t)=(qij(:,:,t-1).*(1+r1).^t).*((1/8)*(1+sij(:,:,t-1).*r2).^t);
cc(t)=(y(:,1,t)-y(:,1,t-1)).'*d*k;%construction cost
end
hb1(t)=sum(2*(1-yi(1,:,t))*d./vb)+sum((1-yi(1,:,t))*td)*ub; %bus headway
hb2(t)=sum(sum((1-yi(:,:,t)).*qij(:,:,t)));
hb(t)=2*sqrt(hb1(t)+hb2(t));
hr(t)=2*sqrt(((2*yi(1,:,t)*d)/vr+sum(yi(1,:,t)*td))*nc*uc/sum(sum((0.1+y(:,:,t)).*qij(:,:,t))));
cu(t)=sum(sum(qij(:,:,t).*(1-yi(:,:,t))))*(hb(t)/2)*(u/4)+sum(sum(qij(:,:,t).*yi(:,:,t)))*(hr(t)/2)*(u/4); %user wait cost
ci1(t)=((diag((1-yi(:,:,t))*qij(:,:,t))).'*(d+td))*(u/vb);
ci2(t)=((diag(yi(:,:,t)*qij(:,:,t))).'*(d+td)).*(u+vb);
ci(t)=ci1(t)+ci2(t);
rb(t)=2*(1-yi(1,:,t))*d/vb+2*sum((1-yi(1,:,t))*td);%bus round trip time
rr(t)=2*((yi(1,:,t)*d)/vr)+sum((yi(1,:,t))*td); %rail round trip time
cv(t)=(rb(t)/hb(t))*ub+(rr(t)/hr(t))*nc*uc;%vehicle operating speed
cm(t)=(yi(1,:,t)*d)*L;%maintenance cost
f(t)=cu(t)+ci(t)+cv(t)+cm(t)+cc(t);
end
f = sum(f);
end
The input i tried with is
totalcost(zeros(4,1,4),zeros(4,4,4),zeros(4,4,4))
Thanks for helping me!
  10 Comments
BOWEN LI
BOWEN LI on 2 Aug 2019
Yeah, you are right. Can't thank you enough.

Sign in to comment.

Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!