Pulling data from text file and the results are being shown as NaN
Ältere Kommentare anzeigen
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 Kommentare
José-Luis
am 21 Okt. 2014
The easiest to get help is to post a piece of code that people can copy/paste and run, and that reproduces your problem.
Just dumping some text, without explicitly showing the error, is likely to elicit fewer responses.
Chetan Rawal
am 21 Okt. 2014
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.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu NaNs finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!