[关闭]
@newton2ndlaw 2016-04-10T17:48:00.000000Z 字数 2105 阅读 612

Homework7 code1-2

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