Advertisement
tdcr

Untitled

Dec 14th, 2021
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. # three equations of lorenz system:
  2. # 1. x'= sigma(y-x)
  3. # 2. y'= x(rho-z)-y
  4. # 3. z'= xy - beta*z
  5. # we must choose proper sigma rho and beta so stimulate air
  6. #  rho=28
  7. #  sigma = 10
  8. #  beta = 8/3
  9.  
  10.  
  11. #import function
  12. from scipy.integrate import odeint
  13.  
  14. import numpy as np
  15. import matplotlib.pyplot as plt
  16. import matplotlib.animation as animation
  17. import mpl_toolkits.mplot3d.axes3d as p3
  18.  
  19. # lorenz system function
  20. def lorenz(loc,t,rho,beta,sigma):
  21.     (x,y,z) = loc    
  22.     return np.array([sigma*(y-x),x*(rho-z)-y,x*y-beta*z])
  23.  
  24.  
  25. #set t range
  26. t = np.arange(0, 30.0 , 0.01) #也可設定 0.1,比較快
  27.  
  28.  
  29. #use ode to solve lorenz system
  30. sol1 = odeint(lorenz,(0,0,0),t,args=(28,8/3,10))
  31. sol2 = odeint(lorenz,(2.0,2.0,8.0),t,args=(28,8/3,10))
  32. sol3 = odeint(lorenz,(2.0,2.0,8.1),t,args=(28,8/3,10))
  33. sol4 = odeint(lorenz,(1.0,1.0,10.0),t,args=(28,8/3,10))
  34.  
  35.  
  36. sol1 = sol1.T
  37. sol2 = sol2.T
  38. sol3 = sol3.T
  39. sol4 = sol4.T
  40.  
  41.  
  42. #draw pic
  43.  
  44. lorenzpic = plt.figure()
  45. ax = p3.Axes3D(lorenzpic)
  46.  
  47.  
  48. #set axes and range
  49. ax.set_xlabel('X')
  50. ax.set_ylabel('Y')
  51. ax.set_zlabel('Z')
  52. ax.set_xlim3d([-100,100])
  53. ax.set_ylim3d([-100,100])
  54. ax.set_zlim3d([0,100])
  55.  
  56. #set ball and line
  57. ball1, = ax.plot([],[],linestyle='None',marker='o',markersize=8,markeredgecolor='r',color='black',markeredgewidth=2)
  58. ball2, = ax.plot([],[],linestyle='None',marker='o',markersize=8,markeredgecolor='y',color='black',markeredgewidth=2)
  59. ball3, = ax.plot([],[],linestyle='None',marker='o',markersize=8,markeredgecolor='b',color='black',markeredgewidth=2)
  60. ball4, = ax.plot([],[],linestyle='None',marker='o',markersize=8,markeredgecolor='green',color='black',markeredgewidth=2)
  61. line1, = ax.plot([],[],color='r')
  62. line2, = ax.plot([],[],color='y')
  63. line3, = ax.plot([],[],color='b')
  64. line4, = ax.plot([],[],color='green')
  65.  
  66.  
  67. def init():
  68.     ball1.set_data(sol1[0:2,0])
  69.     ball1.set_3d_properties(sol1[2,0])
  70.     ball2.set_data(sol2[0:2,0])
  71.     ball2.set_3d_properties(sol2[2,0])
  72.     ball3.set_data(sol3[0:2,0])
  73.     ball3.set_3d_properties(sol3[2,0])
  74.     ball4.set_data(sol4[0:2,0])
  75.     ball4.set_3d_properties(sol4[2,0])
  76.     line1.set_data(sol1[0:2,0])
  77.     line1.set_3d_properties(sol1[2,0])
  78.     line2.set_data(sol2[0:2,0])
  79.     line2.set_3d_properties(sol2[2,0])
  80.     line3.set_data(sol3[0:2,0])
  81.     line3.set_3d_properties(sol3[2,0])
  82.     line4.set_data(sol4[0:2,0])
  83.     line4.set_3d_properties(sol4[2,0])
  84.    
  85.     return ball1,ball2,ball3,ball4,line1,line2,line3,line4
  86.  
  87. def animate(i):
  88.     ball1.set_data(sol1[0:2,i])
  89.     ball1.set_3d_properties(sol1[2,i])
  90.     ball2.set_data(sol2[0:2,i])
  91.     ball2.set_3d_properties(sol2[2,i])
  92.     ball3.set_data(sol3[0:2,i])
  93.     ball3.set_3d_properties(sol3[2,i])
  94.     ball4.set_data(sol4[0:2,i])
  95.     ball4.set_3d_properties(sol4[2,i])
  96.     ball1.set_data(sol1[0:2,i])
  97.     line1.set_3d_properties(sol1[2,:i])
  98.     line2.set_data(sol2[0:2,:i])
  99.     line2.set_3d_properties(sol2[2,:i])
  100.     line3.set_data(sol3[0:2,:i])
  101.     line3.set_3d_properties(sol3[2,:i])
  102.     line4.set_data(sol4[0:2,:i])
  103.     line4.set_3d_properties(sol4[2,:i])
  104.    
  105.     return ball1,ball2,ball3,ball4,line1,line2,line3,line4
  106.  
  107. line_ani = animation.FuncAnimation(lorenzpic,animate,np.arange(1,t.size),init_func=init,interval=20,repeat=False,blit=False)
  108. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement