I keep getting NaN

90 次查看(过去 30 天)
Daniel Hodgson
Daniel Hodgson 2020-9-15
For some reason my T values keeps giving me NaN when putting greater values. I dont understand why the error occurs.
clc, clear all
T=0
summa=0;
x=60;%distance x=60 whole distance
s=0;
n=1%amount of steps (change this depending on the x)
route=input('1 or 2:')
%check approx anwser
fun= @(x) 1./(velocity(x,route));
check=60.*integral(fun,0,x) %in min
T=60.*time_to_destination(x,route,n)
while abs(check-T)>=1 %+-0.5 will give a span of 1
T=60.*time_to_destination(x,route,n); %in min
n=n+1;
end
svar=n %antal steg
t=T %integral approx värde (in min)
function T= time_to_destination(x,route,n)
s=0; %start vilkkor
summa=0;
h=x./n;
for i=0:h:x;
v=1./(velocity(s,route));
v2=1./(velocity((s+h),route));
summa=(h./2).*(v+v2)+summa; %trapets def
s=s+h;
end
T=summa;
end
  6 个评论
Adam Danz
Adam Danz 2020-9-15
That's helpful, Daniel Hodgson, but we need to be able to reproduce the NaN values on our end before we can trace back to their origins.
If there are any NaN values in the "v" output, please attach the two files.
If there aren't any NaN values in "v" you could produce v, save it to a mat file, and attach that mat file (or share the two files).
To determine if an array has any NaNs,
any(isnan(v),'all')
Daniel Hodgson
Daniel Hodgson 2020-9-15
i really appreaciate the help. I beleive i have fixed the issue for the for loop i needed to change it to i=0:h:x-h as i was trying to calculate an integral using the trapezoidal method and i beleive i had exceeded the boundries of my integral by including x at to my understanding the x also breaks the methods rules.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by