Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. def ode_solve(y):
  2. r=sqrt(y[1]^2 + y[2]^2 + y[3]^2)
  3. n=1/(r^3+r^2+r)
  4.  
  5. dy[1] = y[4]
  6. dy[2] = y[5]
  7. dy[3] = y[6]
  8. dy[4] = -y[1]*n/r
  9. dy[5] = -y[2]*n/r
  10. dy[6] = -y[3]*n/r
  11.  
  12. return dy
  13.  
  14. def o_slv(y, t):
  15. x, v = y.reshape(2, -1)
  16.  
  17. rsq = (x**2).sum()
  18. r = np.sqrt(rsq)
  19.  
  20. a = -3*(x/r)/(r + rsq + rsq*r) # use (x/r) to get the normal vector
  21.  
  22. dy = np.hstack((v, a))
  23.  
  24. return dy
  25.  
  26. import numpy as np
  27. import matplotlib.pyplot as plt
  28.  
  29. from scipy.integrate import odeint as ODEint
  30.  
  31. velocities = np.linspace(0.7, 1.3, 7)
  32. times = np.linspace(0, 10, 1001)
  33.  
  34. answers = []
  35. for v in velocities:
  36.  
  37. Y0 = np.array([1, 0, 0, 0, v, 0])
  38.  
  39. answer, info = ODEint(o_slv, Y0, times, full_output=True)
  40. answers.append(answer)
  41.  
  42. if True:
  43. plt.figure()
  44. plt.subplot(2, 1, 1)
  45. for a in answers:
  46. x, y, z = a.T[:3]
  47. plt.plot(x, y)
  48. plt.plot([0], [0], 'ok')
  49. plt.subplot(2, 1, 2)
  50. for i, name in enumerate(('x', 'y', 'z')):
  51. plt.subplot(6, 1, i+1+3)
  52. for a in answers:
  53. plt.plot(times, a.T[i])
  54. plt.title(name, fontsize=16)
  55. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement