Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from scipy.integrate import odeint
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. from mpl_toolkits.mplot3d import Axes3D
  7. from numpy.linalg import norm
  8.  
  9.  
  10. ############################################################
  11. # Trabajamos en un sistema de unidades en el que G=1.0
  12.  
  13. G=1.0
  14. m1=1.0
  15. m2=0.001
  16. mu=G*(m1+m2)
  17.  
  18.  
  19. ############################################################
  20. # Vectores de condiciones iniciales y de tiempo
  21.  
  22. y0=[1.0, 0.0, 0.0, 1.2]
  23. t=np.linspace(0.0,1000.0,1000.0)
  24.  
  25.  
  26. ############################################################
  27. # Definicion de la funcion que devuelve la derivada temporal
  28. # del vector de estado 'y'
  29.  
  30. def func(y,t):
  31. r=y[:2]
  32. v=y[2:]
  33. return [v[0], v[1], -mu*r[0]/norm(r)**3, -mu*r[1]/norm(r)**3]
  34.  
  35.  
  36. ############################################################
  37. # Integracion con 'odeint'
  38.  
  39. solucion=odeint(func,y0,t)
  40.  
  41. r=solucion[:, :2] # vector de posicion para cada t
  42. v=solucion[:, 2:] # vector de velocidad para cada t
  43.  
  44. ############################################################
  45. # Calculo de la energia especifica
  46.  
  47. Es=[]
  48. for i,j in zip(r,v):
  49. Es+=[0.5*norm(j)**2-mu/norm(i)]
  50.  
  51. ############################################################
  52. # Graficacion
  53.  
  54. plt.figure()
  55. plt.plot(t,Es)
  56. plt.xlabel("t")
  57. plt.ylabel("Energy")
  58. plt.title("Energy vs time")
  59. plt.savefig('EnergyvsTime.png')
  60.  
  61. plt.figure()
  62. plt.subplot(121)
  63. plt.plot(r[:,0],r[:,1])
  64. plt.xlabel('x')
  65. plt.ylabel('y')
  66. plt.gca().set_aspect('equal', adjustable='box') # Equal size ratio
  67. plt.subplot(122)
  68. plt.plot(r[:,0],v[:,0])
  69. plt.xlabel('x')
  70. plt.ylabel('v_x')
  71. plt.savefig('SpaceAndPhase.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement