[关闭]
@Guoguo0605 2016-06-22T00:28:41.000000Z 字数 2503 阅读 1059

No 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):
  33. x = []
  34. y = []
  35. for fs in self.cannon_flight_state:
  36. x.append(fs.x)
  37. y.append(fs.y)
  38. plot(x,y, label = labe)
  39. #show()
  40. class drag_cannon(cannon):
  41. def next_state(self, current_state):
  42. global g, b2m
  43. v = sqrt(current_state.vx * current_state.vx + current_state.vy * current_state.vy)
  44. next_x = current_state.x + current_state.vx * self.dt
  45. next_vx = current_state.vx - b2m * v * current_state.vx * self.dt
  46. next_y = current_state.y + current_state.vy * self.dt
  47. next_vy = current_state.vy - g * self.dt - b2m * v * current_state.vy * self.dt
  48. #print next_x, next_y
  49. return flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)
  50. class adiabatic_drag_cannon(cannon):
  51. def next_state(self, current_state):
  52. global g, b2m
  53. factor = (1 - 6.5e-3 * current_state.y / 288.15) ** 2.5
  54. v = sqrt(current_state.vx * current_state.vx + current_state.vy * current_state.vy)
  55. next_x = current_state.x + current_state.vx * self.dt
  56. next_vx = current_state.vx - factor * b2m * v * current_state.vx * self.dt
  57. next_y = current_state.y + current_state.vy * self.dt
  58. next_vy = current_state.vy - g * self.dt - factor * b2m * v * current_state.vy * self.dt
  59. #print next_x, next_y
  60. return flight_state(next_x, next_y, next_vx, next_vy, current_state.t + self.dt)
  61. speed = 700
  62. theta = range(30,56,5)
  63. v_x = [speed * cos(i * pi / 180) for i in theta]
  64. v_y = [speed * sin(i * pi / 180) for i in theta]
  65. def nodrag():
  66. a = []
  67. for i in range(len(theta)):
  68. a.append(cannon(flight_state(0, 0, v_x[i], v_y[i], 0), _dt = 0.1))
  69. labe = str(theta[i]) + r'$^{\circ}$'
  70. a[i].shoot()
  71. a[i].show_trajectory(labe)
  72. legend(loc='upper left', frameon=False)
  73. nodrag()
  74. xlabel(r'$x(m)$', fontsize=16)
  75. ylabel(r'$y(m)$', fontsize=16)
  76. text(45367, 15000, 'No Drag')
  77. title('Trajectory of cannon shell')
  78. show()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注