problem with numbers in for loop that gives NaN

5 views (last 30 days)
function [s, a]= importfile(t, v)
N=length(t)
s(1)=v(1)*t(1);
for i=2:N
s(i)= s(i-1)+ v(i)*(t(i)-t(i-1))
end
This loop works for the 16 first iterations after that I get NaN:
s =
Columns 1 through 8
0 0 9.7223 15.3335 21.3891 24.5002 27.6391 32.6669
Columns 9 through 16
56.3337 62.5004 85.0560 90.7227 107.9451 131.2785 139.1119 142.9730
Columns 17 through 24
NaN NaN NaN NaN NaN NaN NaN NaN
I have checked the numbers in t and they are defined as the first column in the file running.txt that I uploaded and are all normal numbers and not infinity. I have to deliver an assignment so I am a bit lost. Anyone who can help out? Thanks! :)
EDIT: I added the whole script just in case but it should not matter function
[s, a]= importfile(t, v)
N=length(t) for i=1:N-1
a(i)=(v(i+1)-v(i))/(t(i+1)-t(i));
end
s(1)=v(1)*t(1);
for i=2:N
s(i)= s(i-1)+ v(i)*(t(i)-t(i-1))
end
ta=t([1:1217]); sa=s([1:1217]); plot( ta, sa, 'linewidth', 4)
xlabel('tid')
ylabel('posisjon')
title('posisjon')
figure(2)
ta=t([1:1218]);
plot(ta,a, 'linewidth', 4)
title('akselerasjon')
xlabel('tid')
ylabel('akselerasjon')
  2 Comments
Guillaume
Guillaume on 5 Nov 2015
Can you show the values of t and v that give you this result?
Note that NaN is nothing to do with infinity, it means not a number and is the result of an undefined operation such as dividing 0 by 0, or Infinity by Infinity, etc.
Stephen23
Stephen23 on 5 Nov 2015
Edited: Stephen23 on 5 Nov 2015
Please format your code properly by selecting it and clicking the {} Code button above the text box. And don't put an empty line as every alternate line, it just makes it harder to read.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 5 Nov 2015
Edited: Stephen23 on 5 Nov 2015
Although the code is very inefficient (see below), it does not generate NaN's for me:
>> s(1:24)
ans =
Columns 1 through 7:
0.00000 0.00000 9.72230 15.33350 21.38910 24.50020 27.63910
Columns 8 through 14:
32.66690 56.33370 62.50040 85.05600 90.72270 107.94510 131.27850
Columns 15 through 21:
139.11190 142.97300 146.72300 163.94540 168.27870 172.61200 190.61200
Columns 22 through 24:
202.86190 206.72300 210.58410
Because what you originally uploaded did not import that data file, I had to import it using my own code:
% My code to import data file:
fid = fopen('running.txt','rt');
C = textscan(fid,'%f%f','Delimiter',',');
fclose(fid);
t = C{1};
v = C{2};
% Your original code:
N = length(t)
s(1) = v(1)*t(1);
for k=2:N
s(k) = s(k-1)+ v(k)*(t(k)-t(k-1));
end
Better Code
That code is extremely inefficient, because it increments an array with every loop iteration. This means MATLAB has to expand its memory allocation on every iteration. SLOW!
The solution is to stop using low-level loops to solve every small problem, and learn how to make use of MATLAB's much more powerful code vectorization abilities. This one line is much faster and more memory-efficient that your loop, and creates exactly the same output:
>> X = cumsum([v(1)*t(1);v(2:end).*diff(t)])
X =
0.00000
0.00000
9.72230
15.33350
21.38910
24.50020
27.63910
32.66690
56.33370
62.50040
85.05600
90.72270
107.94510
131.27850
... etc
And here is a timing comparison of your loop vs. this vectorized code, for 100 repetitions:
Elapsed time is 55.9212 seconds. % your loop
Elapsed time is 0.00800109 seconds. % vectorized code

More Answers (0)

Categories

Find more on Parallel Computing in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!