[关闭]
@OrionPaxxx 2016-11-15T12:48:56.000000Z 字数 3083 阅读 720

computationalphysics

exercise_06_L1:problem_2.10

abstract

1.考虑风阻和高度随海拔的变化
2.找到精确打击某一空中目标所需的最小速度,并且输出相应的发射角
3.未考虑各种误差

background

参见[上一次作业的背景介绍](https://github.com/OrionPaxxx/computational_physics_N2014301020039/blob/master/exercise_05/exercise_05.md

mainbody

代码如下

  1. import pylab as pl
  2. import math
  3. class cannon_strike:
  4. def __init__(self,mass=10,altitude=0,initial_velocity=700,angle_of_departure=45,step_time=0.01,target_location_x=7000,target_location_y=5000):
  5. self.v=[initial_velocity]
  6. self.angle=angle_of_departure/180*math.pi
  7. self.vx=[initial_velocity*math.cos(self.angle)]
  8. self.vy=[initial_velocity*math.sin(self.angle)]
  9. self.x=[0]
  10. self.y=[0]
  11. self.t=[0]
  12. self.dt=step_time
  13. self.m=mass
  14. self.target_x=target_location_x
  15. self.target_y=target_location_y
  16. self.strike_point_y=0
  17. def trajectory(self):
  18. while (self.y[-1]>=0):
  19. temp_vx=self.vx[-1]-0.003233*math.exp(-self.y[-1]/10000)*(self.v[-1]**2)/self.m*self.vx[-1]/self.v[-1]*self.dt
  20. temp_vy=self.vy[-1]-0.003233*math.exp(-self.y[-1]/10000)*(self.v[-1]**2)/self.m*self.vy[-1]/self.v[-1]*self.dt-9.8*self.dt
  21. temp_v=math.sqrt(temp_vx**2+temp_vy**2)
  22. self.vx.append(temp_vx)
  23. self.vy.append(temp_vy)
  24. self.v.append(temp_v)
  25. temp_x=self.x[-1]+temp_vx*self.dt
  26. temp_y=self.y[-1]+temp_vy*self.dt
  27. self.x.append(temp_x)
  28. self.y.append(temp_y)
  29. temp_t=self.t[-1]+self.dt
  30. self.t.append(temp_t)
  31. if self.x[-1]>self.target_x:
  32. self.strike_point_y=(self.y[-2]-self.y[-1])/(self.x[-2]-self.x[-1])*(self.target_x-self.x[-1])+self.y[-1]
  33. break
  34. def show_result(self):
  35. pl.plot(self.x,self.y)
  36. pl.xlabel("x(m)")
  37. pl.ylabel("y(m)")
  38. pl.title("trajectory of the canon")
  39. class precise_strike(cannon_strike):
  40. def pre_stk(self):
  41. #input the target location
  42. _input=input("please input the strike location like this:'x,y'\n")
  43. target_location=_input.split(",")
  44. temp_target_x=float(target_location[0])
  45. temp_target_y=float(target_location[1])
  46. #find the best angle
  47. temp_angle=1
  48. temp_velocity=10
  49. a=cannon_strike(angle_of_departure=temp_angle,initial_velocity=temp_velocity,target_location_x=temp_target_x,target_location_y=temp_target_y)
  50. a.trajectory()
  51. while a.strike_point_y<temp_target_y:
  52. if temp_angle<89:
  53. temp_angle=temp_angle+1
  54. a=cannon_strike(angle_of_departure=temp_angle,initial_velocity=temp_velocity,target_location_x=temp_target_x,target_location_y=temp_target_y)
  55. a.trajectory()
  56. else:
  57. temp_angle=1
  58. temp_velocity=temp_velocity+1
  59. a=cannon_strike(angle_of_departure=temp_angle,initial_velocity=temp_velocity,target_location_x=temp_target_x,target_location_y=temp_target_y)
  60. a.trajectory()
  61. #output the result
  62. print("stike point:","x=",a.target_x,"y=",a.strike_point_y)
  63. print("velocity=",temp_velocity,"angle=",temp_angle)
  64. tra,=pl.plot(a.x,a.y)
  65. loc,=pl.plot([a.target_x],[a.target_y],"*")
  66. pl.xlabel("x(m)")
  67. pl.ylabel("y(m)")
  68. pl.title("trajectory of the canon")
  69. pl.legend([tra,loc],["trajectory","target location"],loc="upper left")
  70. b=precise_strike()
  71. b.pre_stk()

此程序允许用户输入打击的坐标,自动从V=10m/s,angle=1 开始枚举,找到在误差范围内的最优速度和角度后输出结果。

以输入的打击电坐标为(1000,1000)为例,其结果如下

发射速度 发射角 落点位置
193 64 (1000,1002.177)
192.60 65 (1000,1000.197)

可视化输出:
output.png?raw=true未知大小
如果图裂请点击链接(https://github.com/OrionPaxxx/computational_physics_N2014301020039/blob/master/exercise_06/output.png

conclusion

在不考虑误差的情况下,对于短程打击,此程序十分有效

acknownlegement

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