Pulling data from text file and the results are being shown as NaN

조회 수: 1 (최근 30일)
Gino
Gino 2014년 10월 21일
편집: Gino 2014년 10월 21일
I am loading a text file but some of my results are being shown as NaN. This is my code:
clear all;
close all;
test=0.001;
imax=13;
kmax=4;
load('hcrdat.txt');
q=hcrdat(:,1);
d=hcrdat(:,2);
r=hcrdat(:,3);
%q=[250 250 100 50 100 25 25 25 45 70 80 30 20];
%d=[.4,.4,.3,.3,.3,.2,.2,.2,.3,.3,.3,.2,.2];
%r=[96.8,96.8,306,306,306,3098,3098,3098,306,312,306,9295,3098];
m=zeros(kmax,imax);
iter=0;
for n=1:500
iter=iter+1;
difmax=0;
%DISCHARGE CORRECTIONS IN EACH LOOP
for k=1:kmax
sum1=0;
sum2=0;
for i=1:imax
t=m(k,i);
sum1=sum1+t*r(i)*q(i)^2;
sum2=sum2+2*abs(t)*q(i)*r(i);
end
dq=-sum1/sum2;
if abs(dq)>difmax
difmax=abs(dq);
end
for i=1:imax
q(i)=q(i)+m(k,i)*dq;
%CHANGE OF ASSUMED DISCHARGE DIRECTION AND PROPER CORRECTIONS
if q(i)<0
q(i)=-q(i);
end
for kk=1:kmax
m(kk,i)=-m(kk,i);
end
end
end
it=n;
diff(it)=difmax;
%CHECK FOR CONVERGENCE TO FINAL DISCHARGE VALUES
if difmax<test
break
end
end
for i=1:imax
% FLOW VELOCITIES IN THE VARIOUS BRANCHES
u(i)=q(i)/1000*1.273/d(i)^2;
% HEAD LOSSES IN EACH BRANCH
dh(i)=r(i)*q(i)^2/1000000;
end
= = = = = = = = = = = = = = = = = = =
The data in the text file (hcrdat.txt) is:
250, .4, 96.8
250, .4, 96.8
100, .3, 306
50, .3, 306
100, .3, 306
25, .2, 3098
25, .2, 3098
25, .2, 3098
45, .3, 306
70, .3, 612
80, .3, 306
30, .2, 9295
20, .2, 3098
= = = = = = = = = = = = = = = = = = =
My results for sum1, sum2 and dq are NaN and the problem is because q is being loaded as NaN.
  댓글 수: 3
Chetan Rawal
Chetan Rawal 2014년 10월 21일
I didn't try to replicate the problem, but it may be due to the syntax you are using. Look at the documentation for LOAD to make sure, or use the import wizard (UIIMPORT) and generate a function from it.
Gino
Gino 2014년 10월 21일
편집: Gino 2014년 10월 21일
Jose-Luis, the whole code was there but I didn't know how to post it as code (now I know), that is why I numbered listed each line of code. Thanks anyway!
P.D.: Problem solved thanks to Roger!

댓글을 달려면 로그인하십시오.

채택된 답변

Roger Stafford
Roger Stafford 2014년 10월 21일
I see places in your code that could produce NaNs. In the section
sum1=0;
sum2=0;
for i=1:imax
t=m(k,i);
sum1=sum1+t*r(i)*q(i)^2;
sum2=sum2+2*abs(t)*q(i)*r(i);
end
dq=-sum1/sum2;
the 'm' matrix is all zeros and apparently never changes from that. Consequently all 't' values would also be zero, with the result that 'sum1' and 'sum2' would also be zero. When you do the division
dq=-sum1/sum2;
NaNs are going to be the result and these are placed in 'difmax'.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by