[关闭]
@flyboy1995 2016-10-24T15:25:29.000000Z 字数 2834 阅读 21

第六次作业


作业2.10强化版(引入迎面风阻)


摘要

在上次作业的基础上,本次作业需要考虑风阻和海拔高度的影响,同时,要求找出精确打击某一目标的最小速度及角度。


背景

题目原文:
2.10:Generalize the program developed for the previous problem so that it can deal with situations in which the target is at a different altitude than the cannon.Consider cases in which the target is higher and lower than the cannon.Also investigate how minimum firing velocity required to hit the target varies as the altitude of the target is varied.


正文

代码如下:

  1. import pylab as pl
  2. import math
  3. class cannon_problem:
  4. def __init__(self,mass=500,altitude=0,initial_velocity=600,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.0*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.00004*(self.v[-1])*(self.vx[-1])*(1-0.000022*self.y[-1])**2.5*self.dt
  20. temp_vy=self.vy[-1]-0.00004*(self.v[-1])*(self.vy[-1])*(1-0.000022*self.y[-1])**2.5*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_problem(cannon_problem):
  40. def zuixiaov(self):
  41. _input=input("please input the strike location like this:'x,y'\n")
  42. target_location=_input.split(",")
  43. temp_target_x=float(target_location[0])
  44. temp_target_y=float(target_location[1])
  45. temp_angle=1
  46. temp_velocity=50
  47. a=cannon_problem(angle_of_departure=temp_angle,initial_velocity=temp_velocity,target_location_x=temp_target_x,target_location_y=temp_target_y)
  48. a.trajectory()
  49. while a.strike_point_y<temp_target_y:
  50. if temp_angle<89.0:
  51. temp_angle=temp_angle+1
  52. a=cannon_problem(angle_of_departure=temp_angle,initial_velocity=temp_velocity,target_location_x=temp_target_x,target_location_y=temp_target_y)
  53. a.trajectory()
  54. else:
  55. temp_angle=1
  56. temp_velocity=temp_velocity+0.1
  57. a=cannon_problem(angle_of_departure=temp_angle,initial_velocity=temp_velocity,target_location_x=temp_target_x,target_location_y=temp_target_y)
  58. a.trajectory()
  59. print("stike point:","x=",a.target_x,"y=",a.strike_point_y)
  60. print("velocity=",temp_velocity,"angle=",temp_angle)
  61. tra,=pl.plot(a.x,a.y)
  62. loc,=pl.plot([a.target_x],[a.target_y],"d")
  63. pl.xlabel("x(m)")
  64. pl.ylabel("y(m)")
  65. pl.title("trajectory of the canon")
  66. pl.legend([tra,loc],["trajectory","target location"],loc="upper left")
  67. b=precise_problem()
  68. b.zuixiaov()

结论

将目标坐标设置为'300,300'
此处输入图片的描述
此处输入图片的描述


致谢

感谢秦大粤同学的分享

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