- #1
BiGyElLoWhAt
Gold Member
- 1,622
- 131
Homework Statement
A rocket can either fire or not fire for a duration of 1-100ms with equal probability of firing/not firing. Determine how far it will go in the first 2 seconds of flight.
Homework Equations
The Attempt at a Solution
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import random
fire = 0
duration = 0.0
distance = .0
velocity = .0
time = 0.0
while (time < 2.0):
...duration = (random.randint(1,100))/1000.0
...if (time + duration > 2.0):
...duration = 2.0 - time
...fire = random.randint(0,1)
...time += duration
...distance += (1/2)*2*fire*(duration**2) + velocity*duration
...velocity += 2*fire*duration
print("distance: " , distance)
4. Questions about this:
So one of my first questions is why is duration an integer? I've tried several things, putting decimal places in differently etc, to make it a float, but I can't seem to get it. I'm assuming it is changing types with the randint() function, but what should I use otherwise? Also, shouldn't dividing by 1000.0 change it to a float?
Also, I'm getting litterally nothing as an output, which makes me think it's getting caught in this loop here. I don't python much, or really code a whole lot anymore, so I'm probably pretty rusty and making noob mistakes. Is there something obvious in here that's not letting it leave the loop? I'll walk through my logic, in case it isn't obvious.
Initialize variables
while time elapsed is less than 2 seconds, do stuff:
stuff{
get a duration using rand numbers
if time elapsed plus duration is greater than 2 seconds, set duration = to the difference, so that time elapsed is exactly 2 seconds
determine whether the rocket fires over the duration or not.
Calculate the distance traveled using the initial velocity at the beginning of the fire/not fire phase, and the acceleration from that fire/not fire phase (the acceleration function was given to us)
calculate the new velocity to be used for the next calculation
repeat
spit out distance traveled.
end.
Worth noting:
mass lost from fuel burning is negligible
air resistance is negligible.
the variable 'a' in the console in the attached image is just a variable that I used to make sure that I could print like I thought I could. It worked. It's irrelevant to the rockets code.