-1

I'm working on an assignment where we have to run a simulation for a model rocket. I am given the formula for the position of the rocket and my assignment is to run the simulation until the model rocket returns back to earth and print the distance of the rocket for each second of flight.

The user will input it's initial velocity, which will randomize as to how long the rocket will be in flight. I'm having trouble as to how to stop the loop once the rocket reaches the earth again (0 ft). I've been using a while loop:

while not position == 0:

Which results in an infinite loop. Is there another solution for this?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Zyanaster
  • 29
  • 7
  • Can you post the code you have so far? – Jared Mackey Mar 03 '15 at 19:37
  • 2
    Wild guess: after re-entry, your rocket embeds itself slightly into the surface of the Earth, stopping at an altitude of negative 0.1 feet. Since this is not equal to 0 feet, your loop continues forever. – Kevin Mar 03 '15 at 19:39
  • possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Ignacio Vazquez-Abrams Mar 03 '15 at 19:40
  • 3
    Also, make sure your rocket does not achieve escape velocity, or else it will drift through space and your loop will continue forever. (although I think this is only a concern if you accurately model that the gravitational pull of Earth decreases as altitude increases) – Kevin Mar 03 '15 at 19:40
  • You could add `print(position)` to your loop and post some of the data here so that we can give a more educated answer based on what is actually happening. – MrAlexBailey Mar 03 '15 at 19:42

2 Answers2

1

The probability of the rocket return to exactly ground level exactly at a time-step is very low. Instead, try

while height > 0.:
    simulate()
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
-1

computing speed is very fast so maybe you can fudge the numbers near the end. you can split the process into two parts to achieve your goal. i'll provided the descending portion to give you an idea

 ...your code to reach max velocity before coming down
 while height > 0:
       kmh = kmh + randrange(0,1000)
       height = height-kmh
 height = 0
 print height
LampPost
  • 856
  • 11
  • 30