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

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Chetan Rawal
Chetan Rawal il 21 Ott 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.
Gino
Gino il 21 Ott 2014
Modificato: Gino il 21 Ott 2014
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!

Accedi per commentare.

Risposta accettata

Roger Stafford
Roger Stafford il 21 Ott 2014
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'.

Più risposte (0)

Categorie

Scopri di più su Statistics and Machine Learning Toolbox in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by