why is this "if" statement is never true?

Can someone please explain why this "if" statement is never true? It is supposed to allow me to take the elements at every even integer of Time (i.e. 2 4 6...) while Time is increasing by 0.1 every time through the while statement.
I have gone through the program step by step and have actually viewed my j = 2, and Time = to 2.0000 and it still goes to the "else" command. I've also written the same if statement outside of this script and it works.
Any ideas? Your help is appreciated!
dt = 0.1;
i = 1;
j = 0;
Time = 0;
TimeMax = 20000;
while Time < TimeMax
Mass0 = ...
Altitude0 = ...
Velocity0 = ...
Gamma0 = ...
Radius0 =...
Nu0 = ...
Time = Time + dt;
j = 2+j;
if j == Time
i = i+1;
Mass(i)= Mass0;
Altitude(i) = Altitude0;
Velocity(i) = Velocity0;
Gamma(i) = Gamma0;
Radius(i)=Radius0;
Nu(i) = Nu0;
else
j = j-2;
end
end

1 Kommentar

Youssef  Khmou
Youssef Khmou am 13 Feb. 2013
is the variables Mass, Velocity... are constant then make them outside the "while" loop to gain memory .

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Walter Roberson
Walter Roberson am 13 Feb. 2013

2 Stimmen

5 Kommentare

Brooke
Brooke am 13 Feb. 2013
actually, this is what I needed. the mod doesn't handle the equivalence properly. I accepted his answer to soon. my apologies. thank you for your kind help!
Youssef  Khmou
Youssef Khmou am 13 Feb. 2013
ok, we can work on it until it work correctly, no problem !
Walter Roberson
Walter Roberson am 13 Feb. 2013
The basic problem was floating point comparisons and round-off, that adding 10 copies of 0.1 does not give you exactly 1. If you know your dt is 1/P for an integer P, then add 1 to your counter each instance instead of adding 1/P, and if you need the time the counter represents, divide the counter by P; even then better to compare the integer counter to (time_limit * P) rather than comparing (counter / P) to time_limit
Youssef  Khmou
Youssef Khmou am 13 Feb. 2013
Correct !, we found that error , thanks
Image Analyst
Image Analyst am 14 Feb. 2013
Go ahead and mark it Accepted then.

Melden Sie sich an, um zu kommentieren.

Brooke
Brooke am 13 Feb. 2013

0 Stimmen

thank you so much! I was getting so frustrated over here. Many thanks!

1 Kommentar

Youssef  Khmou
Youssef Khmou am 13 Feb. 2013
how is the equivalence you want? if my answer is not the right one, delete it to get better solution !

Melden Sie sich an, um zu kommentieren.

Youssef  Khmou
Youssef Khmou am 13 Feb. 2013
Bearbeitet: Youssef Khmou am 13 Feb. 2013

0 Stimmen

Hi, i saw that the answer is not satisfactory, because we evaluate for 20000 points in Time and get only 20 values in Mass, Veloc... the reason is because i made mistake putting Mod(j,Time) while i should put " Mod(Time,j)" or Mod(Time,2) , with this alteration we get the logical lenght for Mass perxample which is 10001, and also changing dt to 1 :
dt = 1;
i = 1;
j = 0;
Time = 0;
TimeMax = 20000;
Mass0 =2;
Altitude0 =58;
Velocity0 = pi;
Gamma0 =77;
Radius0 =7.25;
Nu0 = 77;
while Time < TimeMax
Time = Time + dt;
j = 2+j;
if mod(Time,2)==0 % if mod(time,j)==0
i = i+1;
Mass(i)= Mass0;
Altitude(i) = Altitude0;
Velocity(i) = Velocity0;
Gamma(i) = Gamma0;
Radius(i)=Radius0;
Nu(i) = Nu0;
else
j = j-2;
end
end
Is that the equivalence you talked about ?

Kategorien

Gefragt:

am 13 Feb. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by