Two trains and a bird

Question:

I have to create a code for the classic physics problem. link to the problem’s explanation: Link
but in my code, the user inputs the speed of the trains, the distance between them and the speed of the bird.It needs to output the total distance traveled by the bird and the trains and the birds’ position each 0,01 second. Below is my code and I want some help on improving it.

t=0
v1= input ("Insert the trains' speed= ")
d= input ("Insert the distance between the trains= ")
va= input ("Insert the bird's speed= ")

v1= float(v1)
d=float(d)
va=float(va)

s1=0
s2=d

while s1<=s2:
 s1= v1*t
 s2=d-v1*t
 sa=va*t
 t1=d/(2*v1)
 da=t1*va
 tx1= ("Position of train 1= %sm")
 tx2= ("Position of train 2= %sm")
 tx3= ("Bird's position= %sm")
 print(tx1 % s1)
 print(tx2 % s2)
 print(tx3 % sa)
  t=t+0.01


if s1==s2:
 print ("The bird died")
 txt4=("Total distance traveled by the bird= %sm")
 print (txt4 % da)
Asked By: Emily

||

Answers:

In general, when doing comparisons between floating point numbers you want to avoid testing for exact equality.

For example, here you probably would like s1==s2 to return True when comparing, say, 2.001010191012393 == 2.001010191012394.

In this case, you could solve this by using if s1>=s2 instead of if s1==s2.

You could also use an else in the while like this,

x, y = 1, 5
while x<y:
    x = x + 1
    print x
else:
    print "done"

More generally (that is, when the comparison is not one sided) you can use an expression like abs(s2-s1)<epsilon, where epsilon is an appropriately small number. In your case, this is a bit difficult because epsilon will be a function of the parameters in your simulation, and probably much larger than the machine precision but related instead to your timestep (0.01, in your case). Therefore, the one-sided inequality is better and easier.

Answered By: tom10

Finally the bird is dead. Here is how it dies. The main modification is if abs(s1-s2) < 1e-8: to make your code work. The problem was that when s1 and s2 were getting equal, the condition if s1==s2: was not working because it was comparing numbers which were something like 14.9999999 and 15.0000000 which are though equal but represented in marginally different floating points. The simple solution for this is to check if the absolute difference between these two numbers is below some very small number such as 1e-8.

I also added a break in your code when the bird dies to come out of the loop. In case you don’t want this, just remove it. I also shortened your code by removing unnecessary lines required for printing.

while s1<=s2:
    s1 = v1*t
    s2 = d-v1*t
    sa = va*t
    t1 = d/(2*v1)
    da = t1*va
    print("Position of train 1 = %s m"%s1)
    print("Position of train 2 = %s m"%s2)
    print("Bird's position = %s m"%sa)
    t=t+0.01

    if abs(s1-s2) < 1e-8:
        print ("The bird died")
        print("Total distance traveled by the bird= %s m" %da)
        break 

Output

Insert the trains' speed= 30
Insert the distance between the trains= 30
Insert the bird's speed= 20

Position of train 1 = 0.0 m
Position of train 2 = 30.0 m
Bird's position = 0.0 m
.
.
.
Bird's position = 10.000000000000004 m
The bird died
Total distance traveled by the bird= 10.0 m
Answered By: Sheldore
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.