Optimizing a trajectory in Python
Callista "Cherry Bomb" Davidson is a world-famous stunt woman, and she is trying to achieve the long distance world record human cannonball. She needs your help, though, because she has no idea what angle is best to aim her cannon. You will need to simulate firing the cannon at all integer angles from 1 to 90 inclusive.
Her cannon will shoot her at a velocity of 70ms70ms at a starting height of 5m5m. We will assume an acceleration of gravity g=−9.8ms2g=−9.8ms2. We will simulate her trajectory for 10 seconds in 1,000 steps (so 1,001 states including the initial state.)
Angles
We will simulate 90 different angles from 1∘−90∘1∘−90∘. You will need to find the horizontal xx and vertical yy components of her initial velocity at each angle to simulate in two dimensions.
vx0=v0cos(θ)vy0=v0sin(θ).vx0=v0cos(θ)vy0=v0sin(θ).
(Note that you will need to convert from degrees to radians before using the numpy sin and cos functions.)
Drag
To ensure Callista goes as far as possible, we cannot neglect drag in both the vertical and horizontal dimensions. To simplify things a bit, we will assume her surface area is equal in both dimensions. We will need to make use of the drag equation,
FD=−12ρv2CDA.FD=−12ρv2CDA.
Callista has a surface area of A=0.8m2A=0.8m2 and a mass of mass=65kgmass=65kg. Her costume gives her a drag coeffienct of CD=1.4CD=1.4. The mass density of the air is ρ=1.225kgm3ρ=1.225kgm3. This force will always oppose her travel, so she will experience an acceleration of
axdrag=−ρCDA2massv2∣∣∣vxv∣∣∣.axdrag=−ρCDA2massv2|vxv|.
Remember that you will need to simulate this acceleration in both the xx and yy dimensions (both vertical and horizontal.) To simplify things a bit, compute this acceleration in the xx and yy dimensions independently.
Simulation
When Callista hits the ground, she stops. To simulate this, if her altitude is ever less than 0, set her velocity in all directions to zero, her height to zero, and her xx location to its previous value. Her xx location should then remain the same until the end of the simulation. In other words, when she hits the ground, she stops moving until the 10 seconds we are simulating are over.
For each angle, you will need to simulate all 1,000 time steps. Each time you update the state variables, first compute her acceleration (using her previous speed to compute the drag,) then her velocity (using her current acceleration,) then her location (using her current velocity.)
Final answer
After the simulation is complete, we are not yet finished. Callista needs to know the optimum angle in integer degrees (being a cannonball not a mathematician).
Help
To help you debug your program, we are supplying you with a file 45.csv containing the simulation output for a 45 degree angle. If you run into trouble, you can compare your results to these values.
CODE
import time #importing time module
import math #importing math module
pi = math.pi #defining the value of pi
u = 70 #initial velocity
g = 9.8 #gravitational acceleration
time_step = 0.01 #time step of 0.01 secs
time_elapsed = 0.0
maxAngle = 0
angle = 1
h = 5.0 #setting the initial height to to 5m
x = 0.0
maxRange = 0.0
for angle in range(1,91): #loop which angle takes value from 1 to 90
ux = u * math.cos((pi / 180) * angle) #velocity in x axis
uy = u * math.sin((pi / 180) * angle) #velocity in y axis
for i in range(1000): #running for 1000 steps
dragx = 0.5 * 1.225 * 1.4 * 0.8 * ux * ux #calculating the drag in x direction
dragy = 0.5 * 1.225 * 1.4 * 0.8 * uy * uy #calculating the drag in y direction
drag_ax = dragx / 65.0 #calculating drag acceleration in x axis
drag_ay = dragy / 65.0 #calculating drag acceleration in y axis
accelx = drag_ax #resultant acceleration in x axis
accely = g + drag_ay #resultant acceleration in y axis
vx = ux - accelx * time_step #velocity in the x axis at any instant
vy = uy - accely * time_step #velocity in the y axis at any instant
h += uy * time_step - 0.5 * accely * time_step * time_step #height at any instant of the projectile
x += ux * time_step - 0.5 * accelx * time_step * time_step #range at any instant in the x axis of the projectile
print("Height " + str(h) + " Range " + str(x)) #printing the current position
time.sleep(time_step) #waiting for 0.01 secs
time_elapsed += time_step #calculating the time elapsed
ux = vx #setting the final velocitites to new initial velocities
uy = vy
if h <= 0.0: #if a height 0 is reached
if x > maxRange:
maxRange = x #max angles are noted
maxAngle = angle
h = 5.0 #variables are initialized
x = 0.0
u = 70.0
vx = 0.0
vy = 0.0
print("Wait till the next simulation starts.....")
time.sleep(10.0 - time_elapsed) #wait for 10 secs
time_elapsed = 0.0
break
print("The optimum angle for for Callista is {} degrees which will give her a range of {} metres".format(str(maxAngle),str(maxRange))) #the optimum angle is printed
OUTPUT
//I have not waited for 10 secs after each loop, to get the
output quickly I had got rid of the wait time, because it would
have taken 15 minutes to execute the code but in the code provided
above it will run for the actual 15 minutes.
The optimum angle came out to
be 35 degrees. To demonstrate the actual code I have uploaded this
for one angle where it shows the real time position of the object.
For angle 1 degree


Thank You! Keep Chegging!
Optimizing a trajectory in Python Callista "Cherry Bomb" Davidson is a world-famous stunt woman, and she...
Learning Goal To understand how the trajectory of an object depends on its initial velocity, and to understand how air resistance affects the trajectory. For this problem, use the PhET simulation Projectiie Motion. This simulation allows you to fire an object from a cannon, see its trajectory, and measure its height, range, and hang time (the amount of time in the air) 驵瘦. พี่ PHET Piojectile Motion Start the simulation by selecting the option labeled "Intro". Press the red Fire...
EXPLORE A projectile is launched with a launch angle of 30° with respect to the horizontal direction and with an initial speed of 40 m/s. (A) How do the vertical and horizontal components of the projectile's velocity vary with time? (B) How long does it remain in flight? (C) For a given launch speed, what launch angle produces the longest time of flight? CONCEPTUALIZE Consider the projectile to be a point mass that starts with an initial velocity, upward and...