problem with numbers in for loop that gives NaN
Show older comments
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
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.
Accepted Answer
More Answers (0)
Categories
Find more on NaNs 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!