Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import math
  4. g=9.81
  5. dt=1e-3
  6. v0=40
  7. angle=math.pi/4
  8.  
  9. #time axis
  10. time=np.arange(0,10,dt)
  11. def traj(angle,v0):
  12.     vx0=math.cos(angle)*v0
  13.     vy0=math.sin(angle)*v0
  14.     #initialize x,y arrays
  15.     x=np.zeros(len(time))
  16.     y=np.zeros(len(time))
  17.     # projectile starts at (0,0)
  18.     x[0],y[0]=0,0
  19.     #second elements of x and y
  20.     x[1],y[1]=x[0]+vx0*dt,y[0]+vy0*dt
  21.     i=1
  22.     #continuous loop continuous until projectile hits ground
  23.     while y[i] >=0:
  24.         #fin x[i+1] and y[i+1]
  25.         x[i+1]=(2*x[i]-x[i-1])
  26.         y[i+1]=(2*y[i]-y[i-1])-g*dt**2
  27.         #increments i for next iteration
  28.         i=i+1
  29.     #truncate x and y arrays
  30.     x=x[0:i+1]
  31.     y=y[0:i+1]
  32.     #return x,y,flight time, range of projectile
  33.     return x,y, (dt*i), x[i]
  34. x, y, duration, distance = traj(angle,v0)
  35. print( 'Distance:' ,distance)
  36. print( 'Duration:' ,duration)
  37.  
  38. #part 2: range
  39. n=40
  40. x,y,duration,distance = traj(math.pi/4, 30)
  41. velocities=np.linspace(0,40,num=n)
  42. maxrange=np.zeros(n)
  43. for i in range(n) :
  44.     x,y,duration,maxrange[i] = traj(math.pi/4, velocities)
  45. plt.plot(velocities,maxrange)
  46. plt.xlabel("x")
  47. plt.ylabel("y")
  48. plt.xlim(0,400)
  49. plt.ylim(0,360)
  50. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement