[关闭]
@2014301020054 2016-10-16T16:55:18.000000Z 字数 2270 阅读 1312

Exercise_5:The Trajectory of a cannon shell

physics python


Abstract

Use the Euler method to calculate cannon shell trajectories ignoring both air drag and the effect of air density(actually, ignoring the former automatically rules out the latter). Compare your results with those in Figure 2.4, and with the exact solution.


Background

The Euler method we used to treat the bicycle problem can easily be generalized to deal with motion in two spatial dimensions. To be specific, we consider a projectile such as a shell shot by a cannon. We have a very large cannon in mind, and the large size will determine some of the important physics...


Main

Exercise_2.6

Use the Euler method to calculate cannon shell trajectories ignoring both air drag and the effect of air density(actually, ignoring the former automatically rules out the latter). Compare your results with those in Figure 2.4, and with the exact solution.

Solution

  • Store position as and velocity as .

  • For each time step calculate position and velocity at step :

  • Stop when or

Codes

  1. import pylab as pl
  2. import math as mt
  3. class cannon_shells:
  4. def __init__(self, initial_velocity = 0.7, g = 0.0098, range = 0, height = 0,\
  5. time_step = 0.01, initial_angle = 30.0, da = 5.0):
  6. self.v = [initial_velocity]
  7. self.a = [initial_angle]
  8. self.g = g
  9. self.dt = time_step
  10. self.da = da
  11. self.x = [range]
  12. self.y = [height]
  13. def run(self):
  14. _a = self.a[0]
  15. while(_a <= 60):
  16. _a += self.da
  17. self.a.append(self.a[-1] + self.da)
  18. t = 2 * mt.sin(_a / 180.0 * mt.pi) * self.v[0]/self.g
  19. vx = mt.cos(_a / 180.0 * mt.pi) * self.v[0]
  20. vy = mt.sin(_a / 180.0 * mt.pi) * self.v[0]
  21. _time = 0
  22. xi = [0]
  23. yi = [0]
  24. while(_time < t):
  25. xi.append(xi[-1] + self.dt * vx)
  26. yi.append(yi[-1] + self.dt * vy)
  27. vx = vx
  28. vy = vy - self.g * self.dt
  29. _time += self.dt
  30. self.x = self.x + xi
  31. self.y = self.y + yi
  32. def show_results(self):
  33. font = {'family': 'serif',
  34. 'color': 'darkred',
  35. 'weight': 'normal',
  36. 'size': 16,
  37. }
  38. pl.title('The trajectory of a cannon shell')
  39. pl.text(0.95 * self.x[-1], 0.95 * max(self.y),\
  40. 'No drag', fontdict=font)
  41. pl.plot(self.x,self.y)
  42. pl.xlabel('x($km$)')
  43. pl.ylabel('y($km$)')
  44. pl.show()
  45. a = cannon_shells()
  46. a.run()
  47. a.show_results()

Running result_1

Running resut_2

Conclusion

Acknowledgment

Lesson plan of Cai Hao:Chapter 2

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