Advertisement
silver2row

Projectile Guessing!

May 11th, 2021
1,592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. # From "Programming for Computations, (Svein Linge, Hans Petter Langtangen 2018)."
  2.  
  3. """
  4. Module for computing vertical motion
  5. characteristics for a projectile.
  6. """
  7. def y(v0, t):
  8.     """
  9.    Compute vertical position at time t, given the initial vertical
  10.    velocity v0. Assume negligible air resistance.
  11.    """
  12.     g = 9.81
  13.     return v0*t - 0.5*g*t**2
  14.  
  15. def time_of_flight(v0):
  16.     """
  17.    Compute time in the air, given the initial vertical
  18.    velocity v0. Assume negligible air resistance.
  19.    """
  20.     g = 9.81
  21.     return 2*v0/g
  22.  
  23. def max_height(v0):
  24.     """
  25.    Compute maximum height reached, given the initial vertical
  26.    velocity v0. Assume negligible air resistance.
  27.    """
  28.     g = 9.81
  29.     return v0**2/(2*g)
  30.  
  31. def application():
  32.     import numpy as np
  33.     import matplotlib.pyplot as plt
  34.     import sys
  35.     print("""This program computes vertical motion characteristics for a
  36.    projectile. Given the intial vertical velocity, it computes height
  37.    (as it develops with time), maximum height reached, as well as time
  38.    of flight.""")
  39.  
  40.     try:
  41.         v_initial = float(input('Give the initial velocity: '))
  42.     except:
  43.         print('You must give a valid number!')
  44.         sys.exit(1)
  45.  
  46.     H = max_height(v_initial)
  47.     T = time_of_flight(v_initial)
  48.     print('Maximum height: {:g} m, \nTime of flight: {:g} s'.format(H, T))
  49.  
  50.     # compute and plot position as function of time
  51.     dt = 0.001 # just pick a "small" time step
  52.     N = int(T/dt) # number of time steps
  53.     t = np.linspace(0, N*dt, N+1)
  54.     position = y(v_initial, t) # compute all positions (over T)
  55.     plt.plot(t, position, 'b--')
  56.     plt.xlabel('Time (s)')
  57.     plt.ylabel('Vertical position (m)')
  58.     plt.show()
  59.     return
  60.  
  61. if __name__ == '__main__':
  62.     application()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement