[关闭]
@Guoguo0605 2016-04-08T01:24:12.000000Z 字数 3217 阅读 1169

No drag&With drag

code trajectory_of_cannon_shell


  1. # -*- coding: utf-8 -*-
  2. from pylab import *
  3. from math import *
  4. g = 9.8
  5. b2m = 4e-5
  6. class flight_state:
  7. def __init__(self, _x = 0, _y = 0, _vx = 0, _vy = 0, _t = 0):
  8. self.x = _x
  9. self.y = _y
  10. self.vx = _vx
  11. self.vy = _vy
  12. self.t = _t
  13. class cannon:
  14. def __init__(self, _fs = flight_state(0, 0, 0, 0, 0), _dt = 0.1):
  15. self.cannon_flight_state = []
  16. self.cannon_flight_state.append(_fs)
  17. self.dt = _dt
  18. def next_state(self, current_state):
  19. global g
  20. next_x = current_state.x + current_state.vx * self.dt
  21. next_vx = current_state.vx
  22. next_y = current_state.y + current_state.vy * self.dt
  23. next_vy = current_state.vy - g * self.dt
  24. #print next_x, next_y
  25. return flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)
  26. def shoot(self):
  27. while not(self.cannon_flight_state[-1].y < 0):
  28. self.cannon_flight_state.append(self.next_state(self.cannon_flight_state[-1]))
  29. r = - self.cannon_flight_state[-2].y / self.cannon_flight_state[-1].y
  30. self.cannon_flight_state[-1].x = (self.cannon_flight_state[-2].x + r * self.cannon_flight_state[-1].x) / (r + 1)
  31. self.cannon_flight_state[-1].y = 0
  32. def show_trajectory(self, labe, n):
  33. x = []
  34. y = []
  35. for fs in self.cannon_flight_state:
  36. x.append(fs.x)
  37. y.append(fs.y)
  38. if n == 1:
  39. subplot(121)
  40. plot(x,y, label = labe)
  41. xlabel(r'$x(m)$', fontsize=16)
  42. ylabel(r'$y(m)$', fontsize=16)
  43. text(45367, 15000, 'No Drag', fontsize=16)
  44. title('Trajectory of cannon shell')
  45. ax = gca()
  46. ax.spines['right'].set_color('none')
  47. ax.spines['top'].set_color('none')
  48. ax.xaxis.set_ticks_position('bottom')
  49. ax.yaxis.set_ticks_position('left')
  50. ax.set_xlim(0, 60000)
  51. ax.set_ylim(0, 18000)
  52. elif n == 2:
  53. subplot(122)
  54. plot(x,y, label = labe)
  55. xlabel(r'$x(m)$', fontsize=16)
  56. text(35367, 15000, 'With Air Drag', fontsize=16)
  57. title('Trajectory of cannon shell')
  58. ax = gca()
  59. ax.spines['right'].set_color('none')
  60. ax.spines['top'].set_color('none')
  61. ax.xaxis.set_ticks_position('bottom')
  62. ax.yaxis.set_ticks_position('left')
  63. ax.set_xlim(0, 60000)
  64. ax.set_ylim(0, 18000)
  65. #show()
  66. class drag_cannon(cannon):
  67. def next_state(self, current_state):
  68. global g, b2m
  69. v = sqrt(current_state.vx * current_state.vx + current_state.vy * current_state.vy)
  70. next_x = current_state.x + current_state.vx * self.dt
  71. next_vx = current_state.vx - b2m * v * current_state.vx * self.dt
  72. next_y = current_state.y + current_state.vy * self.dt
  73. next_vy = current_state.vy - g * self.dt - b2m * v * current_state.vy * self.dt
  74. #print next_x, next_y
  75. return flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)
  76. class adiabatic_drag_cannon(cannon):
  77. def next_state(self, current_state):
  78. global g, b2m
  79. factor = (1 - 6.5e-3 * current_state.y / 288.15) ** 2.5
  80. v = sqrt(current_state.vx * current_state.vx + current_state.vy * current_state.vy)
  81. next_x = current_state.x + current_state.vx * self.dt
  82. next_vx = current_state.vx - factor * b2m * v * current_state.vx * self.dt
  83. next_y = current_state.y + current_state.vy * self.dt
  84. next_vy = current_state.vy - g * self.dt - factor * b2m * v * current_state.vy * self.dt
  85. #print next_x, next_y
  86. return flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)
  87. speed = 700
  88. theta = range(30,56,5)
  89. v_x = [speed * cos(i * pi / 180) for i in theta]
  90. v_y = [speed * sin(i * pi / 180) for i in theta]
  91. def nodrag():
  92. a = []
  93. b = []
  94. for i in range(len(theta)):
  95. a.append(cannon(flight_state(0, 0, v_x[i], v_y[i], 0), _dt = 0.1))
  96. labe = str(theta[i]) + r'$^{\circ}$'
  97. a[i].shoot()
  98. a[i].show_trajectory(labe,1)
  99. legend(loc='upper left', frameon=False)
  100. b.append(drag_cannon(flight_state(0, 0, v_x[i], v_y[i], 0), _dt = 0.1))
  101. b[i].shoot()
  102. b[i].show_trajectory(labe,2)
  103. legend(loc='upper left', frameon=False)
  104. nodrag()
  105. show()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注