[关闭]
@ibilis 2016-10-23T06:19:57.000000Z 字数 3831 阅读 555

第6次作业

计算物理


1正文

1分析

根据牛顿第二定律,运动方程为:



对于绝热过程,考虑空气密度随高度变化

所以重写上面方程

2代码

  1. import time
  2. import math
  3. import matplotlib.pyplot as plt
  4. def correct(string,y_t): ##### delete all the data of (x,y) where y<0
  5. x_record = string[0][-1]
  6. y_record = y_t
  7. while True:
  8. if (string[1][-1] < y_t):
  9. if (string[1][-1] < y_t and string[1][-2] > y_t):
  10. x_record = string[0][-1]
  11. y_record = string[1][-1]
  12. del string[0][-1]
  13. del string[1][-1]
  14. else:
  15. string[0].append(string[0][-1]+(y_t - string[1][-1])*(string[0][-1] - x_record)/(string[1][-2] - y_record))
  16. string[1].append(y_t)
  17. break
  18. return string[0],string[1]
  19. def calculate(self.v,theta):
  20. self.vx = []
  21. self.vy = []
  22. self.vx.append( v * math.cos(theta*math.pi/180))
  23. sellf.vy.append( v * math.sin(theta*math.pi/180))
  24. self.dt = 0.1
  25. self.t = []
  26. self.x = []
  27. self.y = []
  28. self.t.append(0)
  29. self.x.append(0)
  30. self.y.append(0)
  31. i = 1
  32. while (y[-1] >= 0):
  33. self.x.append(x[i - 1] + vx[i - 1]*dt)
  34. self.vx.append(vx[i - 1] - dt*0.00004*math.sqrt(vx[i - 1]**2+vy[i - 1]**2)*vx[i - 1]*(1-(0.0065*y[i - 1])/280)**2.5)
  35. self.y.append(y[i - 1] + vy[i - 1]*dt)
  36. self.vy.append(vy[i - 1]-9.8*dt -dt*0.00004*math.sqrt(vx[i - 1]**2+vy[i - 1]**2)*vy[i - 1]*(1-(0.0065*y[i - 1])/280)**2.5)
  37. self.t.append(t[i - 1] + dt)
  38. i+= 1
  39. r = y[-2] / y[-1]
  40. x[-1] = (x[-2] + r * x[-1]) / (r + 1)
  41. y[-1] = 0
  42. return x, y
  43. def find_maxheight(string):
  44. for i in range(len(string[1])):
  45. if ( string[1][0] < string[1][i] ):
  46. string[1][0] = string[1][i]
  47. return string[1][0]
  48. def scan_angle(self,v,theta, x_t, y_t, degree):
  49. self.ran_a = [20, 5, 2, 0.8, 0.2]
  50. self.delta = [1, 0.5, 0.1, 0.05, 0.01]
  51. self.theta = self.theta - ran_a[degree]
  52. self.theta_record = []
  53. self.x = []
  54. self.data = [[],[]]
  55. for i in range(int(ran_a[degree]*2/delta[degree]+1)):
  56. data = calculate(v,theta)[:]
  57. if ( find_maxheight(data) < y_t):
  58. theta = theta + delta[degree]
  59. self.x.append(100000000)
  60. theta_record.append(theta)
  61. else:
  62. data = correct(data,y_t)[:]
  63. self.x.append(data[0][-1])
  64. self.theta_record.append(theta)
  65. self. theta = self.theta + self.delta[degree]
  66. for j in range(len(x)):
  67. if ( abs(x[0] - x_t) > abs(x[j] - x_t)):
  68. x[0] = x[j]
  69. self.theta_record[0] = self.theta_record[j]
  70. return theta_record[0],x[0] ##### debug
  71. def scan_v(self,v,theta, x_target, y_target, degree):
  72. self.ran_v = [100, 10, 2, 0.8, 0.2]
  73. self.delta = [10, 2, 0.5, 0.1, 0.02]
  74. self.v = self.v - self.ran_v[degree]
  75. self.x = []
  76. self.theta_record = []
  77. self.v_record = []
  78. for i in range(int(ran_v[degree]*2/delta[degree]+1)):
  79. self.record_v = self.scan_angle(v,theta, x_target, y_target, degree)[:]
  80. self.x.append(record_v[1])
  81. self.theta_record.append(record_v[0])
  82. self.v_record.append(v)
  83. self.v = v + delta[degree]
  84. for j in range(len(x)):
  85. if ( abs(x[0] - x_target) > abs(x[j] - x_target)):
  86. x[0] = x[j]
  87. v_record[0] = v_record[j]
  88. theta_record[0] = theta_record[j]
  89. print v_record
  90. print theta_record
  91. print x
  92. return v_record[0],theta_record[0]
  93. def deep_scan(v, theta, x_target, y_target, precision):
  94. degree = 0
  95. record = [[],[]]
  96. for i in range(precision):
  97. record = scan_v(v,theta,x_target,y_target,degree)[:]
  98. v = record[0]
  99. theta = record[1]
  100. degree = degree + 1
  101. return record
  102. def judge_hitting(string,x_t,y_t):
  103. alpha = 0
  104. for i in range(len(string[0])):
  105. if ( x_t - 8 < string[0][-1] < x_t + 8 ):
  106. alpha = 1
  107. if (alpha == 1):
  108. print 'Successfully hitted'
  109. return 1
  110. else:
  111. print 'Miss'
  112. return 0
  113. def main():
  114. start = time.clock()
  115. x_target = 15000
  116. y_target = 1000
  117. # theta0 = 180*math.atan(y_target/x_target)/math.pi
  118. data_record = deep_scan(700,60,x_target,y_target,5) ### 3 for grade 1, 4 for grade 2 and 5 for grade 3
  119. v = data_record[0]
  120. theta = data_record[1]
  121. cannon_record = correct(calculate(v, theta),y_target)
  122. x_max = cannon_record[0][-1]
  123. judge_hitting(cannon_record,x_target,y_target)
  124. print '%f ' % x_max
  125. print '%f ' % theta
  126. print '%f ' %v
  127. end = time.clock()
  128. print "read: %f s" % (end - start)
  129. plt.figure(figsize = (8,6))
  130. plt.title('Trajectory of cannon shell')
  131. plt.xlabel('x(m)')
  132. plt.ylabel('y(m)')
  133. plt.plot(x_target,y_target,'k*',linewidth = 10,label='Target')
  134. plt.plot(cannon_record[0],cannon_record[1],label= 'Trajectroy')
  135. plt.legend(loc = 'upper left')
  136. plt.savefig('chapter2.png',dpi = 144)
  137. plt.show()
  138. main()

3结果

20161023135044.png
20161023135116.png

2结论

考虑更多因素干扰下运动轨迹能更精确击中目标

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注