[关闭]
@newton2ndlaw 2016-04-09T01:32:53.000000Z 字数 2103 阅读 603

Homewrok7 code 1-1

  1. import matplotlib as mpl
  2. from mpl_toolkits.mplot3d import Axes3D
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. from math import *
  6. print 'Exercise 2.19 The effect of backspin on the range of a batted ball V1.0'
  7. print 'Designed by Roach'
  8. t = 0
  9. c_x = []
  10. c_y = []
  11. c_z = []
  12. c_vx = []
  13. c_vy = []
  14. c_vz = []
  15. c_v = []
  16. Constant_vd = 35.0
  17. Constant_delta= 5.0
  18. Constant_g = 9.794
  19. Constant_S0m = 4.1*(10**(-4))
  20. dt = 0.0001
  21. omega = 2000.0
  22. Initial_vx = 0.0
  23. Initial_vy = 0.0
  24. Initial_vz = 0.0
  25. Initial_z = 2.0
  26. def initialize(Initial_vx,Initial_vy,Initial_vz):
  27. global c_vx,c_vy,c_vz,c_x,c_y,c_z
  28. Initial_vx = Initial_vxs[m1]
  29. Initial_vy = Initial_vys[m2]
  30. Initial_vz = Initial_vzs[m3]
  31. c_vx.append(Initial_vx)
  32. c_vy.append(Initial_vy)
  33. c_vz.append(Initial_vz)
  34. c_v.append((Initial_vx**2+Initial_vy**2+Initial_vz**2)**0.5)
  35. c_x.append(0.0)
  36. c_y.append(0.0)
  37. c_z.append(Initial_z)
  38. print 'Initial position x =',c_x[0],'m '
  39. print 'Initial position y =',c_y[0],'m '
  40. print 'Initial position z =',c_z[0],'m '
  41. print 'Initial velocity x =',c_vx[0],'m/s '
  42. print 'Initial velocity y =',c_vy[0],'m/s '
  43. print 'Initial velocity z =',c_vz[0],'m/s '
  44. return 0
  45. def calculate(c_vx,c_vy,c_vz,c_x,c_y,c_z,c_v,dt,Constant_vd,Constant_delta,Constant_g,omega):
  46. i = 0
  47. while c_z[i]>=0:
  48. B_ratio = 0.0039 + 0.0058/( 1 + exp( (c_v[i]-Constant_vd) /Constant_delta) )
  49. c_vx.append( c_vx[i] - B_ratio * c_v[i] * c_vx[i] * dt )
  50. c_vy.append( c_vy[i] - Constant_g * dt)
  51. c_vz.append( c_vz[i] - Constant_S0m * c_vx[i] * omega * dt)
  52. c_v.append((c_vx[i+1]**2 + c_vy[i+1]**2 + c_vz[i+1]**2)**0.5)
  53. c_x.append( c_x[i] + c_vx[i+1] * dt)
  54. c_y.append( c_y[i] + c_vy[i+1] * dt)
  55. c_z.append( c_z[i] + c_vz[i+1] * dt)
  56. i=i+1
  57. print 'Landing point x =',c_x[-1],'m ',
  58. print 'y =',c_y[-1],'m ',
  59. print 'z =',c_z[-1],'m ',
  60. print 'Total steps =',i,' ',
  61. print 'dt =',dt,'s'
  62. return 0
  63. Initial_vxs=[20]
  64. Initial_vys=[0]
  65. Initial_vzs=[0]
  66. m2=0
  67. m3=0
  68. for m1 in range(1):
  69. c_x = None
  70. c_y = None
  71. c_z = None
  72. c_vx = None
  73. c_vy = None
  74. c_vz = None
  75. c_v = None
  76. c_x = []
  77. c_y = []
  78. c_z = []
  79. c_vx = []
  80. c_vy = []
  81. c_vz = []
  82. c_v = []
  83. initialize(Initial_vx,Initial_vy,Initial_vz)
  84. calculate(c_vx,c_vy,c_vz,c_x,c_y,c_z,c_v,dt,Constant_vd,Constant_delta,Constant_g,omega)
  85. fig = plt.figure()
  86. ax = fig.gca(projection='3d')
  87. ax.plot(c_x, c_y, c_z, label='trajectories')
  88. ax.set_xlabel('x (m)')
  89. ax.set_ylabel('y (m)')
  90. ax.set_zlabel('z (m)')
  91. ax.set_title('Exercise 2.19 The effect of backspin on the range of a batted ball ')
  92. plt.show()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注