[关闭]
@rfhongyi 2016-10-24T04:14:47.000000Z 字数 5500 阅读 736

Exercise_06 : Enhanced version exercise 2.10

To hit the target with the minimum firing velocity

冉峰 弘毅班 2014301020064


Abstract


Background

From the Computational Physics we know the method of Euler.Now I want to take the Euler method into the projectile movement calculation:
Newton’s second law in two spatial dimensions :


Write each of these second-order equations as two firest-order differential equations:


Finite difference form:


For the with the effect of the wind we know :


Also we consider the air density with the adiabatic approximation:

So we have:


where


The Main Body

  1. import math
  2. xx = 1000 #x position of the target,unit:m
  3. yy = 100 #y position of the target,unit:m
  4. g = 9.8
  5. a = (6.5)*(math.pow(10,-3))
  6. Bm = 4*math.pow(10,-5) #B2/m,unit:m^(-1)
  7. T0 = 300
  8. vw = -20 #velocity of wind, unit:m/s
  9. va = 1000 #initial_velocity,unit:m/s
  10. sv = [va]
  11. sa = [0]
  12. s = xx
  13. for i in range(21):
  14. v = va
  15. v0 = 0
  16. angle = 25 + i*0.001 #initial_angle=25°,add 2° each time
  17. for j in range(20):
  18. x = [0] #initial_x,unit:m
  19. y = [0] # initial_y,unit:m
  20. dt = 0.1 #time_step,unit:s
  21. vx = [v*math.cos(math.pi*angle/180)]
  22. vy = [v*math.sin(math.pi*angle/180)]
  23. while (x[-1]<=xx):
  24. if y[-1] >100000000:
  25. c = 0
  26. else:
  27. c = math.pow((1-(a*y[-1]/T0)),2.5)
  28. Fx = -Bm*math.sqrt((vx[-1]-vw)*(vx[-1]-vw)+vy[-1]*vy[-1])*(vx[-1]-vw)*c
  29. Fy = -Bm*math.sqrt((vx[-1]-vw)*(vx[-1]-vw)+vy[-1]*vy[-1])*vy[-1]*c
  30. vx.append(vx[-1]+Fx*dt)
  31. vy.append(vy[-1]+(-g+Fy)*dt)
  32. x.append(x[-1]+vx[-2]*dt)
  33. y.append(y[-1]+vy[-2]*dt)
  34. r = (xx-x[-2])/(x[-1]-xx)
  35. y[-1] = (y[-2]+r*y[-1])/(r+1)
  36. x[-1] = xx
  37. if y[-1]>yy:
  38. vt = v
  39. v = v0+(vt-v0)/2
  40. if y[-1]<yy:
  41. v0 = v
  42. v = v0+(vt-v0)/2
  43. sv.append(v)
  44. sa.append(angle)
  45. vmin = min(sv)
  46. l = sv.index(vmin)
  47. amin = sa[l]
  48. print('The x position of the target',xx)
  49. print('The y position of the target',yy)
  50. print('velocity of wind',vw)
  51. print ('The minimum initial velocity:'+str(vmin)+'m/s')
  52. print('angle:'+str(amin)+'°')

Result

The x position of the target 1000
The y position of the target 100
velocity of wind -20
The minimum initial velocity:129.1036605834961m/s
angle:25.02°


The x position of the target 1000
The y position of the target -100
velocity of wind -20
The minimum initial velocity:104.19178009033203m/s
angle:25.019°


The x position of the target 100
The y position of the target -100
velocity of wind -20
The minimum initial velocity:20.12920379638672m/s
angle:25.0°


The x position of the target 100
The y position of the target 100
velocity of wind -20
The minimum initial velocity:20.131091947405366m/s
angle:25.0°


  1. import math
  2. import pylab as pl
  3. import math
  4. import time
  5. class cannon:
  6. def __init__(self, vel_0=1000, time_of_duration=1, vel_0_step=0.01, time_step=0.01, m=100, B=0.01, vel_wind=2, theta=0, step=0.01, value_of_x=0, value_of_y=0, g=10, T0=300):
  7. self.v0 = [vel_0]
  8. self.theta = [theta]
  9. self.vx = [self.v0[0]]
  10. self.vy = [self.v0[0]]
  11. self.x = [value_of_x]
  12. self.y = [value_of_y]
  13. self.t = [0]
  14. self.g = g
  15. self.dt = time_step
  16. self.time = time_of_duration
  17. self.nsteps = int(time_of_duration // time_step + 1)
  18. self.dtheta = step
  19. self.nsteps2 = int(90//step + 1)
  20. self.B = B
  21. self.m = m
  22. self.dv = vel_0_step
  23. self.vw = vel_wind
  24. self.T0 = T0
  25. self.n = 0
  26. def calculate(self):
  27. for k in range(100):
  28. tmpv0 = self.v0[k] + self.dv
  29. self.v0.append(tmpv0)
  30. self.theta = [0]
  31. self.vx = [0]
  32. self.vy = [0]
  33. for j in range(self.nsteps2):
  34. tmptheta = self.theta[j] + self.dtheta
  35. self.theta.append(tmptheta)
  36. self.x = [0]
  37. self.y = [0]
  38. self.vx[0] = self.v0[k] * math.cos(self.theta[j] * math.pi / 180)
  39. self.vy[0] = self.v0[k] * math.sin(self.theta[j] * math.pi / 180)
  40. for i in range(self.nsteps):
  41. tmpvx = self.vx[i] - self.dt * pow((1-0.0065 * self.y[i]/self.T0),2.5) * self.B * math.sqrt(float(self.vx[i]-self.vw) * float(self.vx[i]-self.vw) + self.vy[i] * self.vy[i]) * (self.vx[i] - self.vw)/self.m
  42. tmpvy = self.vy[i] - self.g * self.dt + self.dt * pow((1-0.0065 * self.y[i]/self.T0),2.5) * self.B * \
  43. math.sqrt(float(self.vx[i]-self.vw) * float(self.vx[i]-self.vw)+ self.vy[i] * self.vy[i]) * self.vy[i]/self.m
  44. tmpx=self.x[i] + self.vx[i] * self.dt
  45. tmpy=self.y[i] + self.vy[i] * self.dt
  46. self.vx.append(tmpvx)
  47. self.vy.append(tmpvy)
  48. self.x.append(tmpx)
  49. self.y.append(tmpy)
  50. self.t.append(self.t[i] + self.dt)
  51. if int(self.x[i]) == 10:
  52. if self.y[i] > 10:
  53. self.n = 1
  54. break
  55. if self.n == 1:
  56. break
  57. if self.n == 1:
  58. self.show_results()
  59. print("The minimum of velocity is:%f", self.v0[k])
  60. break
  61. def show_results(self):
  62. pl.plot(self.x, self.y)
  63. pl.xlabel('x ($m$)')
  64. pl.ylabel('y ($m$)')
  65. pl.show()
  66. a = cannon()
  67. a.calculate()

result

The x position of the target 10000
The y position of the target 10000
velocity of wind 2
The minimum initial velocity:1000.10999m/s


But the code have a problem which is too slowly .

Conclusion


Thanks For

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