[关闭]
@Zemel-Yang 2016-10-16T16:20:52.000000Z 字数 4954 阅读 982

Chapter 2 problem 2.9: The cannon

python homework


Abstract

  1. Use Euler method to calculate cannon shell trajectories with different firing angles.
  2. Considering both air drag and the reduced air density at high altitude

Content

  1. problem 2.9
    Calculate the trajectory of our cannon shell including both air drag and the reduced air density at high altitude so that you can reproduce the results. Perform your calculation for different firing angles and determine the value of the angle that gives the maximum range.
  2. Ideas
    The air density changes with altitude can be described by this formula:

    , for air, is the sea level temperature. Here we take .
    So , we have:

    From Newtow's second law ,we have:


    v is instantaneous velocity.
    We rewrite (1) and (2) as two first-order differential equations:




    and
    Use Euler method:



  3. Realization

  1. #-*-coding:utf-8-*-
  2. """
  3. Created on Sun Oct 16 15:56:50 2016
  4. @author: admin12
  5. """
  6. import pylab as pl
  7. import math
  8. class cannon:
  9. def __init__(self, x=0, y=0, v=700, time_step=0.1, B_m=4*10**-5, T=273, a=6.5*10**-3, alpha=2.5):
  10. self.vx1 = v*math.cos(math.pi/4)
  11. self.vy1 = v*math.sin(math.pi/4)
  12. self.vx2 = v*math.cos(math.pi/3)
  13. self.vy2 = v*math.sin(math.pi/3)
  14. self.vx3 = v*math.cos(math.pi/6)
  15. self.vy3 = v*math.sin(math.pi/6)
  16. self.vx4 = v*math.cos(math.pi/180*50)
  17. self.vy4 = v*math.sin(math.pi/180*50)
  18. self.vx5 = v*math.cos(math.pi/180*40)
  19. self.vy5 = v*math.sin(math.pi/180*40)
  20. self.x1 = [x]
  21. self.y1 = [y]
  22. self.x2 = [x]
  23. self.y2 = [y]
  24. self.x3 = [x]
  25. self.y3 = [y]
  26. self.x4 = [x]
  27. self.y4 = [y]
  28. self.x5 = [x]
  29. self.y5 = [y]
  30. self.dt = time_step
  31. self.B_m = B_m
  32. self.T = T
  33. self.a = a
  34. self.alpha = alpha
  35. def run(self):
  36. while(self.y1[-1] >= 0):
  37. Newv1 = math.sqrt(self.vx1**2 + self.vy1**2)
  38. self.vx1 = self.vx1 - ((1 - self.a*self.y1[-1]/self.T)**self.alpha)*self.B_m*Newv1*self.vx1*self.dt
  39. self.vy1 = self.vy1 - 9.8*self.dt - ((1 - self.a*self.y1[-1]/self.T)**self.alpha)*self.B_m*Newv1*self.vy1*self.dt
  40. self.x1.append(self.x1[-1] + self.vx1*self.dt)
  41. self.y1.append(self.y1[-1] + self.vy1*self.dt)
  42. r1 = -self.y1[-2]/self.y1[-1]
  43. self.x1[-1] = (self.x1[-2] + r1*self.x1[-1])/(r1 + 1)
  44. self.y1[-1] = 0
  45. while(self.y2[-1] >= 0):
  46. Newv2 = math.sqrt(self.vx2**2 + self.vy2**2)
  47. self.vx2 = self.vx2 - ((1 - self.a*self.y2[-1]/self.T)**self.alpha)*self.B_m*Newv2*self.vx2*self.dt
  48. self.vy2 = self.vy2 - 9.8*self.dt - ((1 - self.a*self.y2[-1]/self.T)**self.alpha)*self.B_m*Newv2*self.vy2*self.dt
  49. self.x2.append(self.x2[-1] + self.vx2*self.dt)
  50. self.y2.append(self.y2[-1] + self.vy2*self.dt)
  51. r2 = -self.y2[-2]/self.y2[-1]
  52. self.x2[-1] = (self.x2[-2] + r2*self.x2[-1])/(r2 + 1)
  53. self.y2[-1] = 0
  54. while(self.y3[-1] >= 0):
  55. Newv3 = math.sqrt(self.vx3**2 + self.vy3**2)
  56. self.vx3 = self.vx3 - ((1 - self.a*self.y3[-1]/self.T)**self.alpha)*self.B_m*Newv3*self.vx3*self.dt
  57. self.vy3 = self.vy3 - 9.8*self.dt - ((1 - self.a*self.y3[-1]/self.T)**self.alpha)*self.B_m*Newv3*self.vy3*self.dt
  58. self.x3.append(self.x3[-1] + self.vx3*self.dt)
  59. self.y3.append(self.y3[-1] + self.vy3*self.dt)
  60. r3 = -self.y3[-2]/self.y3[-1]
  61. self.x3[-1] = (self.x3[-2] + r3*self.x3[-1])/(r3 + 1)
  62. self.y3[-1] = 0
  63. while(self.y4[-1] >= 0):
  64. Newv4 = math.sqrt(self.vx4**2 + self.vy4**2)
  65. self.vx4 = self.vx4 - ((1 - self.a*self.y4[-1]/self.T)**self.alpha)*self.B_m*Newv4*self.vx4*self.dt
  66. self.vy4 = self.vy4 - 9.8*self.dt - ((1 - self.a*self.y4[-1]/self.T)**self.alpha)*self.B_m*Newv4*self.vy4*self.dt
  67. self.x4.append(self.x4[-1] + self.vx4*self.dt)
  68. self.y4.append(self.y4[-1] + self.vy4*self.dt)
  69. r4 = -self.y4[-2]/self.y4[-1]
  70. self.x4[-1] = (self.x4[-2] + r4*self.x4[-1])/(r4 + 1)
  71. self.y4[-1] = 0
  72. while(self.y5[-1] >= 0):
  73. Newv5 = math.sqrt(self.vx5**2 + self.vy5**2)
  74. self.vx5 = self.vx5 - ((1 - self.a*self.y5[-1]/self.T)**self.alpha)*self.B_m*Newv5*self.vx5*self.dt
  75. self.vy5 = self.vy5 - 9.8*self.dt - ((1 - self.a*self.y5[-1]/self.T)**self.alpha)*self.B_m*Newv5*self.vy5*self.dt
  76. self.x5.append(self.x5[-1] + self.vx5*self.dt)
  77. self.y5.append(self.y5[-1] + self.vy5*self.dt)
  78. r5 = -self.y5[-2]/self.y5[-1]
  79. self.x5[-1] = (self.x5[-2] + r5*self.x5[-1])/(r5 + 1)
  80. self.y5[-1] = 0
  81. def show_results(self):
  82. font = {'family': 'serif',
  83. 'color': 'black',
  84. 'weight': 'normal',
  85. 'size': 15,
  86. }
  87. plot1, = pl.plot(self.x1, self.y1, 'r')
  88. plot2, = pl.plot(self.x2, self.y2, 'b')
  89. plot3, = pl.plot(self.x3, self.y3, 'g')
  90. plot4, = pl.plot(self.x4, self.y4, 'c')
  91. plot5, = pl.plot(self.x5, self.y5, 'y')
  92. pl.legend([plot1, plot2, plot3, plot4, plot5],['45$\degree$', '60$\degree$', '30$\degree$','50$\degree$', '40$\degree$'], loc = "best")
  93. pl.title('Both air drag and air density', fontdict = font)
  94. pl.xlabel('x asix(m)')
  95. pl.ylabel('y asix(m)')
  96. pl.savefig('chapter2_2.6.png',dpi=144)
  97. pl.show()
  98. c = cannon()
  99. c.run()
  100. c.show_results()

Conclusion

  1. I saw the trajectories of cannon shell including both air drag and the air density at high latitudes with different firing angles.And the shell fly farthest when the firing angle is nearly 45°.
  2. There are still many things to learn about "class". The method I use here seems naive. So I will keep studying python hard.
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注