[关闭]
@oliver1995 2016-10-24T14:04:22.000000Z 字数 2654 阅读 721

Exercise_06: The Trajectory of a Cannon Shell(2)

Abstract

  • use python to do homework 2.10
  • add the air resistance and the wind velocity

Background

  • Genenralize 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 the minimum firing velocity required to hit the target varies as the altitude of the target is varied.(consider the wind velocity)

Main body

First considering the wind velosity

  1. import pylab as pl
  2. from math import *
  3. class cannon:
  4. def __init__(self,init_v=100,B_m=0.00004,theta = 60,
  5. init_x=0,init_y=0,time_step=0.1,wind_velocity=-10):
  6. self.vx = [init_v*cos(theta*2*pi/360)]
  7. self.vy = [init_v*sin(theta*2*pi/360)]
  8. self.x = [init_x]
  9. self.y = [init_y]
  10. self.t = [0]
  11. self.B_m = B_m
  12. self.win = wind_velocity
  13. self.dt = time_step
  14. def run(self):
  15. while self.y[-1]>=0:
  16. self.x.append(self.x[-1] + self.dt * self.vx[-1])
  17. self.y.append(self.y[-1] + self.dt * self.vy[-1])
  18. f=sqrt((self.vx[-1]-self.win)**2+self.vy[-1]**2) * self.B_m
  19. self.vx.append(self.vx[-1] - self.dt * f *(self.vx[-1]-self.win))
  20. self.vy.append(self.vy[-1] - self.dt * (9.8 + f *self.vy[-1]))
  21. def show_results(self):
  22. pl.plot(self.x, self.y,color="black",
  23. linestyle="-.",label="headwind")
  24. pl.xlabel('x(m)')
  25. pl.ylabel('y(m)')
  26. pl.legend(loc='upper right')
  27. pl.ylim(0)
  28. pl.show()
  29. a = cannon()
  30. a.run()
  31. a.show_results()

image

Then considering the altitude and height
when height =-100:
code:

  1. import pylab as pl
  2. import math
  3. class cannon:
  4. def __init__(self,init_v=100,B_m=0.00004,theta = 35,
  5. init_x=0,init_y=0,time_step=0.1,wind_velocity=10,
  6. a_0=0.0065, alpha_0=2.5, temp_0=298.0,height=-100):
  7. self.vx = [init_v*math.cos(theta*2*math.pi/360)]
  8. self.vy = [init_v*math.sin(theta*2*math.pi/360)]
  9. self.x = [init_x]
  10. self.y = [init_y]
  11. self.t = [0]
  12. self.B_m = B_m
  13. self.a =a_0
  14. self.alpha=alpha_0
  15. self.temp=temp_0
  16. self.win = wind_velocity
  17. self.height=height
  18. self.dt = time_step
  19. def run(self):
  20. while self.y[-1]>=self.height:
  21. self.x.append(self.x[-1] + self.dt * self.vx[-1])
  22. self.y.append(self.y[-1] + self.dt * self.vy[-1])
  23. rho = (1- self.a *self.y[-1]/ self.temp ) ** self.alpha
  24. f=math.sqrt((self.vx[-1]-self.win)**2+self.vy[-1]**2) * self.B_m * rho
  25. self.vx.append(self.vx[-1] - self.dt * f *(self.vx[-1]-self.win))
  26. self.vy.append(self.vy[-1] - self.dt * (9.8 + f *self.vy[-1]))
  27. def show_results(self):
  28. pl.plot(self.x, self.y,color="blue",
  29. linestyle="-", label="35")
  30. pl.xlabel('x(m)')
  31. pl.ylabel('y(m)')
  32. pl.legend(loc='upper right')
  33. pl.ylim(-100)
  34. pl.show()
  35. a = cannon()
  36. a.run()
  37. a.show_results()

image2

image2

when height =100m:
image3
image4

Conclusion

  • From the picture, we can easily see that when height is -100, it is that the farthermost the cannon hit,a little smaller than 45°. And when the heigtt is 100, hit the farthermost, a little biger than 45°.
  • Adding some realistic factor make the code more comflex, but the model will be more accurate

Acknowledgement

  • Thanks to teacher cai's ppt.
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注