[关闭]
@zy-0815 2016-10-23T11:12:38.000000Z 字数 10015 阅读 1003

计算物理第六次作业

计算物理


摘要

本作业为L2的相关解答,将对2.10题进行进一步升级,发展“超级辅助精确打击系统”(考虑炮弹初始发射的时候发射角度误差,速度有5%的误差,迎面风阻误差10%,以炮弹落点与打击目标距离差平方均值最小为优胜)

背景介绍

上次课我们通过练习,初步掌握了使用欧勒法并引入多个变量下的对炮弹运行轨迹的计算,但实际上我们还需要进一步考虑实际模型,比如之前的运算只考虑确定情况,即确定角度,确定风阻,确定速度。但实际的炮弹运动过程中,往往存在不同的影响因素使打击存在误差,因此我们在本次作业中便着重分析此类问题。

正文

1. 首先我们考虑标准情况,即截至上次作业中提到的考虑风阻的情况并引入绝热模型分析海拔对风阻影响,
我们已知绝热模型公式:


其中,我们很快写出此时的方程如下:

由此,我们可以写出程序果如下,注:为方便起见只做出的情况

  1. import pylab as pl
  2. import math
  3. class cannon :
  4. def __init__(self,i=0,air_resistance=0.00004,power=10,mass = 1,time_step=0.1,\
  5. initial_velocity=700,initial_x=0,initial_y=0,\
  6. initial_velocity_x=700*math.cos(math.pi/4),\
  7. initial_velocity_y=700*math.sin(math.pi/4)):
  8. self.x = [initial_x]
  9. self.y = [initial_y]
  10. self.vx = [initial_velocity_x]
  11. self.vy = [initial_velocity_y]
  12. self.v = [initial_velocity]
  13. self.t = [0]
  14. self.m = mass
  15. self.p = power
  16. self.B_2 = air_resistance
  17. self.dt = time_step
  18. def run(self):
  19. while(1):
  20. s1=self.x[-1] + self.vx[-1]*self.dt
  21. s2=self.y[-1] + self.vy[-1]*self.dt
  22. v1=self.vx[-1] - self.dt *pow((1-(0.0065*self.y[-1])/300),2.5)*\
  23. (self.B_2 / self.m) *self.v[-1] * self.vx[-1]
  24. v2=self.vy[-1] - 9.8*self.dt - self.dt *\
  25. pow((1-(0.0065*self.y[-1])/300),2.5)*(self.B_2 / self.m) *\
  26. self.v[-1] * self.vx[-1]
  27. self.x.append(s1)
  28. self.y.append(s2)
  29. self.v.append(math.sqrt(self.vx[-1]*self.vx[-1] + self.vy[-1]*self.vy[-1]))
  30. self.vx.append(v1)
  31. self.vy.append(v2)
  32. if(s2<0):
  33. break
  34. def show_results(self):
  35. pl.plot(self.x,self.y,'b',label='angle is 45 degree')
  36. pl.title('Precise guidance system')
  37. pl.xlabel('X')
  38. pl.ylabel('Y')
  39. pl.legend()
  40. pl.ylim(0,)
  41. pl.show()
  42. a = cannon()
  43. a.run()
  44. a.show_results()

此程序运行效果如图:
image_1avm2v9d81bpv1julha18pp1vs3m.png-40.2kB

2. 这时引入迎面风阻,我们根据(2.26)式可得:


其中,以及
所以我们修改部分程序如下:

  1. v1=self.vx[-1] - self.dt *pow((1-(0.0065*self.y[-1])/300),2.5)*\
  2. (0.0039+0.0058/(1+math.exp((self.v[-1]-35)/5)))*\
  3. self.v[-1] * self.vx[-1]
  4. v2=self.vy[-1] - 9.8*self.dt - self.dt *\
  5. pow((1-(0.0065*self.y[-1])/300),2.5)*(0.0039+\
  6. 0.0058/(1+math.exp((self.v[-1]-35)/5))) *\
  7. self.v[-1] * self.vx[-1]

运行程序我们可以得到两者的对比如图:
image_1avm77m2q8dpbboommjn4m991g.png-41kB
但是我们同时发现,由于我们设置的初始速度过大(700m/s),因此迎面风阻也相当大,以至于其最大射程远远减小。其实这也源于我们引入的风阻参数是基于2.3节里棒球的运动情况,因此必然存在严重误差。但在这里我们继续沿用这一设定。

3. 现在我们考虑目标位置的改变,比如目标处于比炮台更高的地方,亦或者处于更低的位置,此时作图的手段也会出现相应的调整。基于上一步引入风阻后炮弹射程将为600m,因此我们不妨将炮台的初始海拔设为100m,而低海拔目标位置仍为0m,高海拔位置为200m,同时,为了方便下一步改变初射速度及角度,我也修改了初始值设定的函数,具体修改程序如下:

  1. import pylab as pl
  2. import math
  3. class cannon :
  4. def __init__(self,i=0,air_resistance=0.00004,power=10,mass = 1,time_step=0.01,\
  5. total_time=100, initial_velocity=700,initial_x=0,initial_y=100,\
  6. initial_angle=45,angle_c=math.pi/180):
  7. self.x = [initial_x]
  8. self.y = [initial_y]
  9. self.v = [initial_velocity]
  10. self.vx= [initial_velocity*math.cos(initial_angle*angle_c)]
  11. self.vy= [initial_velocity*math.sin(initial_angle*angle_c)]
  12. self.t = [0]
  13. self.m = mass
  14. self.p = power
  15. self.B_2 = air_resistance
  16. self.dt = time_step
  17. self.time = total_time
  18. def run(self):
  19. while(1):
  20. s1=self.x[-1] + self.vx[-1]*self.dt
  21. s2=self.y[-1] + self.vy[-1]*self.dt
  22. v1=self.vx[-1] - self.dt *pow((1-(0.0065*self.y[-1])/300),2.5)*(0.0039+\
  23. 0.0058/(1+math.exp((self.v[-1]-35)/5))) *\
  24. self.v[-1] * self.vx[-1]
  25. v2=self.vy[-1] - 9.8*self.dt - self.dt *\
  26. pow((1-(0.0065*self.y[-1])/300),2.5)*(0.0039+\
  27. 0.0058/(1+math.exp((self.v[-1]-35)/5))) *\
  28. self.v[-1] * self.vx[-1]
  29. self.x.append(s1)
  30. self.y.append(s2)
  31. self.v.append(math.sqrt(self.vx[-1]*self.vx[-1] + self.vy[-1]*self.vy[-1]))
  32. self.vx.append(v1)
  33. self.vy.append(v2)
  34. if(v2<0 and s2<200):
  35. s1=self.x[-1] + self.vx[-1]*0.001
  36. s2=self.y[-1] + self.vy[-1]*0.001
  37. v1=self.vx[-1] - 0.001 *pow((1-(0.0065*self.y[-1])/300),2.5)*(0.0039+\
  38. 0.0058/(1+math.exp((self.v[-1]-35)/5))) *\
  39. self.v[-1] * self.vx[-1]
  40. v2=self.vy[-1] - 9.8*0.001 - 0.001 *\
  41. pow((1-(0.0065*self.y[-1])/300),2.5)*(0.0039+\
  42. 0.0058/(1+math.exp((self.v[-1]-35)/5))) *\
  43. self.v[-1] * self.vx[-1]
  44. self.x.append(s1)
  45. self.y.append(s2)
  46. self.v.append(math.sqrt(self.vx[-1]*self.vx[-1] + self.vy[-1]*self.vy[-1]))
  47. self.vx.append(v1)
  48. self.vy.append(v2)
  49. if(s2<200):
  50. break
  51. break
  52. def show_results(self):
  53. pl.plot(self.x,self.y,'b',label='45 degree with headwind')
  54. pl.title('Precise guidance system with higher target')
  55. pl.ylim(0,600)
  56. pl.xlabel('X')
  57. pl.ylabel('Y')
  58. pl.legend()
  59. pl.show()
  60. a = cannon()
  61. a.run()
  62. a.show_results()

以上所示程序为目标高度于200m时的情况,同样,修改循环终止的条件,即可得到位于海拔0m的目标时的图像,程序修改如下:

  1. if(s2<0):
  2. s1=self.x[-1] + self.vx[-1]*0.001
  3. s2=self.y[-1] + self.vy[-1]*0.001
  4. v1=self.vx[-1] - 0.001 *pow((1-(0.0065*self.y[-1])/300),2.5)*(0.0039+\
  5. 0.0058/(1+math.exp((self.v[-1]-35)/5))) *\
  6. self.v[-1] * self.vx[-1]
  7. v2=self.vy[-1] - 9.8*0.001 - 0.001 *\
  8. pow((1-(0.0065*self.y[-1])/300),2.5)*(0.0039+\
  9. 0.0058/(1+math.exp((self.v[-1]-35)/5))) *\
  10. self.v[-1] * self.vx[-1]
  11. self.x.append(s1)
  12. self.y.append(s2)
  13. self.v.append(math.sqrt(self.vx[-1]*self.vx[-1] + self.vy[-1]*self.vy[-1]))
  14. self.vx.append(v1)
  15. self.vy.append(v2)
  16. if(s2<200):
  17. break
  18. break

我们可以得到一组图像:
image_1avmdheb01mpu10lcqhonjh15u734.png-38.6kB
image_1avmfe073qea1djj1t43mn92b39.png-39.6kB
但其实我们也发现,在低海拔位置时,即图二情况,我们不需要使用上面所述的,当接近y=0时减小步长以得到精确节点的方式,而是可以通过限制图像y轴的取值域,即通过函数pl.ylim(0,)来实现自动滤掉x轴以下图像的功能。因此,我们只需当条件满足s2<0时终止循环即可,这样做会简便许多。

4. 当我们考虑实际应用时,莫过于能够通过计算,能够通过最小的发射速度,便可击中目标。为此,我们需要对程序进行进一步改进,即可以自动选择角度与速度,使炮弹能够击中目标。
 4.1
 首先我们需要通过循环,找出一确定初速度下,最远射程所对应的角度。

  1. import pylab as pl
  2. import math
  3. class cannon :
  4. def __init__(self,i=0,air_resistance=0.00004,power=10,mass = 1,time_step=0.01,\
  5. total_time=100, initial_velocity=700,initial_x=0,initial_y=0,\
  6. initial_angle=40,angle_c=math.pi/180):
  7. self.x = [initial_x]
  8. self.y = [initial_y]
  9. self.v = [initial_velocity]
  10. self.x0 = [initial_x]
  11. self.y0 = [initial_y]
  12. self.v0 = [initial_velocity]
  13. self.t = [0]
  14. self.m = mass
  15. self.p = power
  16. self.B_2 = air_resistance
  17. self.dt = time_step
  18. self.time = total_time
  19. self.a=[initial_angle]
  20. self.b=[angle_c]
  21. def run(self):
  22. a=45
  23. for a in range(0,90):
  24. self.x[-1]=self.x0[-1]
  25. self.y[-1]=self.y0[-1]
  26. self.v[-1]=self.v0[-1]
  27. self.vx= [self.v[-1]*math.cos(self.a[-1]*self.b[-1])]
  28. self.vy= [self.v[-1]*math.sin(self.a[-1]*self.b[-1])]
  29. while(1):
  30. s1=self.x[-1] + self.vx[-1]*self.dt
  31. s2=self.y[-1] + self.vy[-1]*self.dt
  32. v1=self.vx[-1] - self.dt *pow((1-(0.0065*self.y[-1])/300),2.5)*(0.0039+\
  33. 0.0058/(1+math.exp((self.v[-1]-35)/5))) *\
  34. self.v[-1] * self.vx[-1]
  35. v2=self.vy[-1] - 9.8*self.dt - self.dt *\
  36. pow((1-(0.0065*self.y[-1])/300),2.5)*(0.0039+\
  37. 0.0058/(1+math.exp((self.v[-1]-35)/5))) *\
  38. self.v[-1] * self.vx[-1]
  39. self.x.append(s1)
  40. self.y.append(s2)
  41. self.v.append(math.sqrt(self.vx[-1]*self.vx[-1] + self.vy[-1]*self.vy[-1]))
  42. self.vx.append(v1)
  43. self.vy.append(v2)
  44. if(s2<0):
  45. break
  46. self.a.append(self.a[-1]+1)
  47. if(self.a[-1]>50):
  48. break
  49. def show_results(self):
  50. pl.plot(self.x,self.y)
  51. pl.ylim(0,)
  52. pl.xlabel('X')
  53. pl.ylabel('Y')
  54. pl.legend()
  55. pl.show()
  56. a = cannon()
  57. a.run()
  58. a.show_results()

该程序可以绘出从的轨迹,关于这个范围的选择是运行几次后发现的,当角度大于时,距离会急剧减小,原因是整个下降过程都会变成自由落体而不再具有水平速度。程序中步长为,当然此精度在以后的步骤里会有所调整。整个程序运行结果如下:
image_1avo5iboshnubeg18n7v2a1k4sm.png-65.8kB
  
  4.2
  类似的,我们可以写出速度循环,当然,我们可以想象,速度越大,理应获得越远的距离,下面是程序运行后,700到1000m/s初速,间隔100m/s所花图像:
image_1avoe5i1p1iiq1uiv37b4us1d0l1g.png-42.7kB
  接下来,我们需要在程序中加入选择程序,即得到需要的最大距离。以改变出射角程序为基础,我们需要修改的部分程序如下:

  1. def run_angle(self):
  2. a0=0
  3. s0=0
  4. for a in range(0,90):
  5. self.x[-1]=self.x0[-1]
  6. self.y[-1]=self.y0[-1]
  7. self.v[-1]=self.v0[-1]
  8. self.vx= [self.v[-1]*math.cos(self.a[-1]*self.b[-1])]
  9. self.vy= [self.v[-1]*math.sin(self.a[-1]*self.b[-1])]
  10. while(1):
  11. s1=self.x[-1] + self.vx[-1]*self.dt
  12. s2=self.y[-1] + self.vy[-1]*self.dt
  13. v1=self.vx[-1] - self.dt *pow((1-(0.0065*self.y[-1])/300),2.5)*(0.0039+\
  14. 0.0058/(1+math.exp((self.v[-1]-35)/5))) *\
  15. self.v[-1] * self.vx[-1]
  16. v2=self.vy[-1] - 9.8*self.dt - self.dt *\
  17. pow((1-(0.0065*self.y[-1])/300),2.5)*(0.0039+\
  18. 0.0058/(1+math.exp((self.v[-1]-35)/5))) *\
  19. self.v[-1] * self.vx[-1]
  20. self.x.append(s1)
  21. self.y.append(s2)
  22. self.v.append(math.sqrt(self.vx[-1]*self.vx[-1] + self.vy[-1]*self.vy[-1]))
  23. self.vx.append(v1)
  24. self.vy.append(v2)
  25. if(s2<0):
  26. break
  27. self.a.append(self.a[-1]+1)
  28. s=self.x[-1]
  29. a=self.a[-2]
  30. if(s>s0):
  31. s0=s
  32. a0=a
  33. if(self.a[-1]>50):
  34. break
  35. print s0
  36. print a0

这样,程序就可以绘出图像的同时输出最大距离及对应的角度。
image_1avo6uan91q6gi4i189nosp1jgn13.png-4.4kB

  4. 3
  至此,我们需要的程序主体已经搭建出来,下一步的工作,就是根据目标的定位,修改结束循环的条件范围,从而确定出合适的速度和角度。不妨我们将目标位置定为坐标(500,0)。经过我们实验,可以大致确定相同初速的情况下,射程最远所需的角度在之间
主程序如下:

  1. #最后主程序
  2. import pylab as pl
  3. import math
  4. class cannon :
  5. def __init__(self,i=0,air_resistance=0.00004,power=10,mass = 1,time_step=0.01,\
  6. total_time=100, initial_velocity=350,initial_x=0,initial_y=0,\
  7. initial_angle=43,angle_c=math.pi/180):
  8. self.x = [initial_x]
  9. self.y = [initial_y]
  10. self.v = [initial_velocity]
  11. self.x0 = [initial_x]
  12. self.y0 = [initial_y]
  13. self.v0 = [initial_velocity]
  14. self.t = [0]
  15. self.m = mass
  16. self.p = power
  17. self.B_2 = air_resistance
  18. self.dt = time_step
  19. self.time = total_time
  20. self.a=[initial_angle]
  21. self.b=[angle_c]
  22. self.a0=[initial_angle]
  23. def run(self):
  24. a0=0
  25. s0=0
  26. v0=0
  27. d=100
  28. for v in range(350,361):
  29. self.x[-1]=self.x0[-1]
  30. self.y[-1]=self.y0[-1]
  31. self.v[-1]=self.v0[-1]
  32. for a in range(0,45):
  33. self.x[-1]=self.x0[-1]
  34. self.y[-1]=self.y0[-1]
  35. self.v[-1]=self.v0[-1]
  36. self.a[-1]=self.a0[-1]
  37. self.vx= [self.v[-1]*math.cos(self.a0[-1]*self.b[-1])]
  38. self.vy= [self.v[-1]*math.sin(self.a0[-1]*self.b[-1])]
  39. while(1):
  40. s1=self.x[-1] + self.vx[-1]*self.dt
  41. s2=self.y[-1] + self.vy[-1]*self.dt
  42. v1=self.vx[-1] - self.dt *pow((1-(0.0065*self.y[-1])/300),2.5)*(0.0039+\
  43. 0.0058/(1+math.exp((self.v[-1]-35)/5))) *\
  44. self.v[-1] * self.vx[-1]
  45. v2=self.vy[-1] - 9.8*self.dt - self.dt *\
  46. pow((1-(0.0065*self.y[-1])/300),2.5)*(0.0039+\
  47. 0.0058/(1+math.exp((self.v[-1]-35)/5))) *\
  48. self.v[-1] * self.vx[-1]
  49. self.x.append(s1)
  50. self.y.append(s2)
  51. self.v.append(math.sqrt(self.vx[-1]*self.vx[-1] + self.vy[-1]*self.vy[-1]))
  52. self.vx.append(v1)
  53. self.vy.append(v2)
  54. if(s2<0):
  55. break
  56. self.a.append(self.a[-1]+0.1)
  57. s=self.x[-1]
  58. a=self.a[-2]
  59. if(s>s0):
  60. s0=s
  61. a0=a
  62. if(self.a[-1]>45):
  63. break
  64. self.v0.append(self.v0[-1]+1)
  65. delta = 500.00-s0
  66. if(abs(delta)<d):
  67. d=delta
  68. V=self.v0[-2]
  69. A=a0
  70. print 'The proper veolcity is',V
  71. print 'The proper angle is',A
  72. print 'The erro range is',d
  73. def show_results(self):
  74. pl.plot(self.x,self.y,'b')
  75. pl.title('Precise guidance system')
  76. pl.ylim(0,)
  77. pl.xlabel('X')
  78. pl.ylabel('Y')
  79. pl.legend()
  80. pl.show()
  81. a = cannon()
  82. a.run()
  83. a.show_results()

我们通过运行程序可得如下图像及数据:
image_1avois3op15grkv41kth1ecflgt1t.png-31.8kB
image_1avoj7fq91h0h1tlqn2a9tdstp2a.png-6.7kB
至此,我们得到了对目标点(500,0)位置处的精确打击,理想射击初速为,角度为.

结论

1. 我们最终得到了确立精确打击的合适角度及初射速度,并且自动选择了最小的误差。

2.在写程序的时候发现了一个很有意思的小bug,即在解决第二阶段的问题时,偶然通过一个程序却得出了不同的运动轨迹,如图:
image_1avmas6d5osm1nr0178j1rctu8p2a.png-46.6kB
但当我检查的时候却发现,两者唯一的差别警示time step的选择,其中绿线代表的是0.01s的步长,而红线为0.1s的步长,可即使是这样的误差,在最终也达到了近100m的误差,而其实在无顶风的时候两者是完全重合的,这足以体现实际计算时小数点的重要性。但由于个人电脑的计算能力问题,最终我选择统一使用0.01s的步长。

3. 程序运行时需要先试运行之前的简单程序,方便确定最大射程的角度范围,然后再进一步通过4.1.3的主程序进行计算。这是因为计算量过大不得已而为之的方法,可即使这样做,运行程序时也会有一两秒的延时。

致谢

张梓桐同学提供了一个比较方便的函数pl.ylim(),其实在老师第二讲的ppt上也用过,即确定y轴的范围。在目标高度不高于火炮时,可用此方法将x轴以下的图像截掉。

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