[关闭]
@Guoguo0605 2016-04-10T23:38:00.000000Z 字数 4881 阅读 1251

Max Angle and Max Range

code trajectory_of_cannon_shell


  1. # -*- coding: utf-8 -*-
  2. import math
  3. import numpy as np
  4. from matplotlib import pyplot as plt
  5. from matplotlib import animation
  6. g = 9.8
  7. b2m = 4e-5
  8. r = []
  9. X = []
  10. Y = []
  11. class flight_state:
  12. def __init__(self, _x = 0, _y = 0, _vx = 0, _vy = 0, _t = 0):
  13. self.x = _x
  14. self.y = _y
  15. self.vx = _vx
  16. self.vy = _vy
  17. self.t = _t
  18. class cannon:
  19. def __init__(self, _fs = flight_state(0, 0, 0, 0, 0), _dt = 0.01):
  20. self.cannon_flight_state = []
  21. self.cannon_flight_state.append(_fs)
  22. self.dt = _dt
  23. def next_state(self, current_state):
  24. global g
  25. next_x = current_state.x + current_state.vx * self.dt
  26. next_vx = current_state.vx
  27. next_y = current_state.y + current_state.vy * self.dt
  28. next_vy = current_state.vy - g * self.dt
  29. #print next_x, next_y
  30. return flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)
  31. def shoot(self):
  32. while not(self.cannon_flight_state[-1].y < 0):
  33. self.cannon_flight_state.append(self.next_state(self.cannon_flight_state[-1]))
  34. r = - self.cannon_flight_state[-2].y / self.cannon_flight_state[-1].y
  35. self.cannon_flight_state[-1].x = (self.cannon_flight_state[-2].x + r * self.cannon_flight_state[-1].x) / (r + 1)
  36. self.cannon_flight_state[-1].y = 0
  37. def show_trajectory(self):
  38. x = []
  39. y = []
  40. for fs in self.cannon_flight_state:
  41. x.append(fs.x)
  42. y.append(fs.y)
  43. X.append(x)
  44. Y.append(y)
  45. # if n == 1:
  46. # plt.subplot(111)
  47. # line, = plot(x,y, label = labe)
  48. # xlabel(r'$x(m)$', fontsize=16)
  49. # ylabel(r'$y(m)$', fontsize=16)
  50. # text(40767, 14500, 'initial speed: 700m/s\n' + 'firing angle: 45' + r'$^{\circ}$', color='black')
  51. # title('Trajectory of cannon shell')#
  52. # ax = gca()
  53. # ax.spines['right'].set_color('none')
  54. # ax.spines['top'].set_color('none')
  55. # ax.xaxis.set_ticks_position('bottom')
  56. # ax.yaxis.set_ticks_position('left')
  57. # ax.set_xlim(0, 60000)
  58. # ax.set_ylim(0, 18000)
  59. # #show()
  60. class drag_cannon(cannon):
  61. def next_state(self, current_state):
  62. global g, b2m
  63. v = math.sqrt(current_state.vx * current_state.vx + current_state.vy * current_state.vy)
  64. next_x = current_state.x + current_state.vx * self.dt
  65. next_vx = current_state.vx - b2m * v * current_state.vx * self.dt
  66. next_y = current_state.y + current_state.vy * self.dt
  67. next_vy = current_state.vy - g * self.dt - b2m * v * current_state.vy * self.dt
  68. #print next_x, next_y
  69. return flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)
  70. class adiabatic_drag_cannon(cannon):
  71. def next_state(self, current_state):
  72. global g, b2m
  73. factor = (1 - 6.5e-3 * current_state.y / 288.15) ** 2.5
  74. v = math.sqrt(current_state.vx * current_state.vx + current_state.vy * current_state.vy)
  75. next_x = current_state.x + current_state.vx * self.dt
  76. next_vx = current_state.vx - factor * b2m * v * current_state.vx * self.dt
  77. next_y = current_state.y + current_state.vy * self.dt
  78. next_vy = current_state.vy - g * self.dt - factor * b2m * v * current_state.vy * self.dt
  79. #print next_x, next_y
  80. return flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)
  81. class isothermal_drag_cannon(cannon):
  82. def next_state(self, current_state):
  83. global g, b2m
  84. factor = math.exp(-current_state.y / 1e4)
  85. v = math.sqrt(current_state.vx * current_state.vx + current_state.vy * current_state.vy)
  86. next_x = current_state.x + current_state.vx * self.dt
  87. next_vx = current_state.vx - factor * b2m * v * current_state.vx * self.dt
  88. next_y = current_state.y + current_state.vy * self.dt
  89. next_vy = current_state.vy - g * self.dt - factor * b2m * v * current_state.vy * self.dt
  90. #print next_x, next_y
  91. return flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)
  92. speed = 700
  93. theta = np.arange(30., 56., 0.1)
  94. v_x = [speed * math.cos(i * math.pi / 180) for i in theta]
  95. v_y = [speed * math.sin(i * math.pi / 180) for i in theta]
  96. def wahaha():
  97. b = []
  98. for i in range(len(theta)):
  99. b.append(adiabatic_drag_cannon(flight_state(0, 0, v_x[i], v_y[i], 0)))
  100. #labe2 = str(theta[i]) + r'$^{\circ}$'
  101. b[i].shoot()
  102. # xx = [b[i].cannon_flight_state[j].x for j in range(len(b[i].cannon_flight_state))]
  103. # yy = [b[i].cannon_flight_state[j].y for j in range(len(b[i].cannon_flight_state))]
  104. # X.append(xx)
  105. # Y.append(yy)
  106. r.append(b[i].cannon_flight_state[-1].x)
  107. b[i].show_trajectory()
  108. #legend(loc='upper left', frameon=False)
  109. wahaha()
  110. p = r.index(max(r))
  111. print theta[p], max(r),type(max(r))
  112. fig = plt.figure()
  113. ax = plt.axes(xlim=(0, 40000), ylim=(0, 18000))
  114. #labe = str(theta[i]) + r'$^{\circ}$'
  115. line, = ax.plot([], [],lw = 2,label = 'adiabatic model' ,color = 'red')
  116. angle_text = ax.text(24167, 14400, '')
  117. maxrange_text = ax.text(24167, 12400, '')
  118. plt.xlabel(r'$x(m)$', fontsize=16)
  119. plt.ylabel(r'$y(m)$', fontsize=16)
  120. plt.title('Trajectory of cannon shell')
  121. plt.legend(loc='upper left', frameon=False)
  122. # initialization function: plot the background of each frame
  123. def init():
  124. line.set_data([], [])
  125. angle_text.set_text('')
  126. maxrange_text.set_text('')
  127. return line, angle_text, maxrange_text
  128. # animation function. This is called sequentially
  129. # note: i is framenumber
  130. def animate(i):
  131. x = X[i]
  132. y = Y[i]
  133. line.set_data(x, y)
  134. pct = float(X[i][-1])/float(max(r))*100
  135. angle_text.set_text('initial speed: 700m/s\n' + 'firing angle: %s' % theta[i] + r'$^{\circ}$' + '\nrange: %f %%' % pct)
  136. if i > p:
  137. maxrange_text.set_text('max range: %s' % max(r))
  138. maxrange_text.set_color(color='red')
  139. return line, angle_text, maxrange_text
  140. fram = len(theta)
  141. # call the animator. blit=True means only re-draw the parts that have changed.
  142. anim=animation.FuncAnimation(fig, animate, init_func=init, frames=fram, interval=30, blit=True)
  143. #anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
  144. plt.show()
  145. #show()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注