I keep getting NaN

84 views (last 30 days)
Daniel Hodgson
Daniel Hodgson on 15 Sep 2020
Commented: Daniel Hodgson on 15 Sep 2020
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 Comments
Adam Danz
Adam Danz on 15 Sep 2020
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 on 15 Sep 2020
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.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!