robbyrob42

Untitled

Oct 4th, 2022
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import matplotlib.pyplot as plt
  2. import matplotlib.animation as anim
  3.  
  4. fig = plt.figure(figsize=(6,6))
  5. ax = plt.axes(xlim=(-1,1), ylim=(-1,1))
  6.  
  7. x=[0.15,-0.15,0.9]
  8. y=[0,0,0]
  9. dx=[0,0,0]
  10. dy=[0.8,-1.2,1.201]
  11. M=[1.2,0.8]
  12. dt=0.001
  13. cx=[[],[],[]]
  14. cy=[[],[],[]]
  15.  
  16.  
  17.  
  18. curve1,=ax.plot([],[],'r-')
  19. curve2,=ax.plot([],[],'g-')
  20. curve3,=ax.plot([],[],'b-')
  21.  
  22. obj1,=plt.plot([],[],'ro')
  23. obj2,=plt.plot([],[],'go')
  24. obj3,=plt.plot([],[],'bo')
  25.  
  26. def init():
  27.     curve1.set_data([],[])
  28.     curve2.set_data([],[])
  29.     curve3.set_data([],[])
  30.     obj1.set_data([],[])
  31.     obj2.set_data([],[])
  32.     obj3.set_data([],[])
  33.     return curve1,curve2,curve3,obj1,obj2,obj3,
  34.  
  35. def f(t):
  36.     global x,y,dx,dy,r
  37.     for i in range(2):
  38.         j=(i+1)%2
  39.         dx[i]+=dt*M[j]*(x[j]-x[i])/((x[j]-x[i])**2+(y[j]-y[i])**2)**1.5
  40.         dy[i]+=dt*M[j]*(y[j]-y[i])/((x[j]-x[i])**2+(y[j]-y[i])**2)**1.5
  41.         dx[2]+=dt*M[i]*(x[i]-x[2])/((x[i]-x[2])**2+(y[i]-y[2])**2)**1.5
  42.         dy[2]+=dt*M[i]*(y[i]-y[2])/((x[i]-x[2])**2+(y[i]-y[2])**2)**1.5
  43.     for i in range(3):
  44.         x[i]+=dt*dx[i]
  45.         y[i]+=dt*dy[i]
  46.         cx[i].append(x[i])
  47.         cy[i].append(y[i])
  48.     curve1.set_data(cx[0],cy[0])
  49.     curve2.set_data(cx[1],cy[1])
  50.     curve3.set_data(cx[2],cy[2])
  51.     obj1.set_data(x[0],y[0])
  52.     obj2.set_data(x[1],y[1])
  53.     obj3.set_data(x[2],y[2])
  54.     return curve1, curve2, curve3, obj1, obj2, obj3,
  55.  
  56. meh = anim.FuncAnimation(fig, f, init_func=init, interval=1, blit=True,save_count=(9000))
  57. plt.show()
  58. #video_writer=anim.FFMpegWriter(fps=150)
  59. #meh.save('3_body.mp4',writer=video_writer)
Tags: python
Add Comment
Please, Sign In to add comment