For loop returns all values as NaN
2 views (last 30 days)
Show older comments
When I run this for loop, all the values are returned as NaN. I was wondering if anyone could help me find where I went wrong.
for i=1:N
Pao(i)=(Vao(i)-Vao0)/Cao;
Pla(i)=(Vla(i)-Vla0)/Cla;
Psm(i)=(Vsm(i)-Vsm0)/Csm;
Pve(i)=(Vve(i)-Vve0)/Cve;
Phe(i)=(Vhe(i)-Vhe0)/Che(i);
Pca(i)=(Vca(i)-Vca0)/Cca;
if Phe(i)>Pao(i); Q1(i)=(Phe(i)-Pao(i))/Rha;
else Q1(i)=0;
end
Q3(i)=(Pao(i)-Pla(i))/Ral;
Q2(i)=Q1(i)-Q3(i);
Vao(i+1)=Vao(i)+Q2(i)*dt;
Q5(i)=(Pla(i)-Psm(i))/Rls;
Q4(i)=Q3(i)-Q5(i);
Vla(i+1)=Vla(i)+Q4(i)*dt;
Q7(i)=(Psm(i)-Pca(i))/Rsc;
Q6(i)=Q5(i)-Q7(i);
Vsm(i+1)=Vsm(i)+Q6(i)*dt;
Q9(i)=(Pca(i)-Pve(i))/Rcv;
Q8(i)=Q7(i)-Q9(i);
Vca(i+1)=Vca(i)+Q8(i)*dt;
if Pve(i)>Phe(i); Q11(i)=(Pve(i)-Phe(i))/Rvh;
else Q11(i)=0;
end
Q10(i)=Q9(i)-Q11(i);
Vve(i+1)=Vve(i)+Q10(i)*dt;
Q12(i)=Q11(i)-Q1(i);
Vhe(i+1)=Vhe(i)+Q12(i)*dt;
end
And this is my parameter file
%Volumes and residual volumes in L:
Vhe=.1; Vhe0=0;
Vao=.15; Vao0=.085;
Vla=.35; Vla0=.17;
Vsm=.05; Vsm0=.02;
Vca=.3; Vca0=.06;
Vve=3.25; Vve0=.65;
%Resistances in (mmHg*s)/L:
Rha=60.08; %
Ral=12.0;
Rls=380.4;
Rsc=241.2;
Rcv=279.6;
Rvh=26.90;
%Compliances in L/mmHg:
Cao=.0007;
Cla=.002;
Csm=.0005;
Cca=.006;
Cve=.156;
Chs=.0006;
Chd=.01;
s=30;
d=60;
N=800;
dt=1;
n=10;
0 Comments
Answers (1)
Star Strider
on 17 Nov 2015
A NaN result usually indicates an attempt to do a 0/0, Inf/Inf or a calculation involving NaN.
There must be more to your code than you’re telling us. When I run this:
Vao=.15; Vao0=.085;
Cao=.0007;
Pao(1)=(Vao(1)-Vao0)/Cao;
Pao(2)=(Vao(2)-Vao0)/Cao;
I get:
Index exceeds matrix dimensions.
Error in ... (line ...)
Pao(2)=(Vao(2)-Vao0)/Cao;
0 Comments
See Also
Categories
Find more on Discrete Events and Mode Charts 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!