Advertisement
vatman

Untitled

Oct 1st, 2023
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.82 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy.integrate import solve_ivp
  4.  
  5. # Определим функцию для системы дифференциальных уравнений
  6. def system_equations(gamma):
  7.     def rhs(t, X):
  8.         x, y = X
  9.         dxdt = y
  10.         dydt = +x**5 - 5*x**3 + 4*x
  11.         return [dxdt, dydt]
  12.     return rhs
  13.  
  14. # Создадим функцию для построения векторного поля
  15. def plot_vector_field(rhs, limits, N=25, arrow_length=0.1):
  16.     xlims, ylims = limits
  17.     xs = np.linspace(xlims[0], xlims[1], N)
  18.     ys = np.linspace(ylims[0], ylims[1], N)
  19.     U = np.zeros((N, N))
  20.     V = np.zeros((N, N))
  21.     for i, y in enumerate(ys):
  22.         for j, x in enumerate(xs):
  23.             vfield = rhs(0.0, [x, y])
  24.             u, v = vfield
  25.             # Нормализуем вектор
  26.             length = np.sqrt(u**2 + v**2)
  27.             if length != 0:
  28.                 u /= length
  29.                 v /= length
  30.             # Умножаем на фиксированную длину
  31.             u *= arrow_length
  32.             v *= arrow_length
  33.             U[i][j] = u
  34.             V[i][j] = v
  35.     return xs, ys, U, V
  36.  
  37.  
  38. # Функция для построения векторного поля на плоскости
  39. def plot_on_plane(rhs, limits):
  40.     plt.close()
  41.     xlims, ylims = limits
  42.     plt.xlim(xlims[0], xlims[1])
  43.     plt.ylim(ylims[0], ylims[1])
  44.     xs, ys, U, V = plot_vector_field(rhs, limits)
  45.     plt.quiver(xs, ys, U, V, alpha=0.8)
  46.  
  47. # Определение параметра gamma и создание функции для системы с этим параметром
  48. gamma = 1.0
  49. rhs = system_equations(gamma)
  50.  
  51. # Построение векторного поля на плоскости
  52. plot_on_plane(rhs, [(-3.0, 3.0), (-3.0, 3.0)])
  53.  
  54. # Решение системы дифференциальных уравнений и построение траекторий
  55. sol1 = solve_ivp(rhs, [0.0, 100.0], (0.01, 0.02), method='RK45', rtol=1e-12)
  56. x1, y1 = sol1.y
  57. plt.plot(x1, y1, 'yellow')
  58.  
  59. sol2 = solve_ivp(rhs, [0.0, 100.0], (2.0, 0.5), method='RK45', rtol=1e-12)
  60. x2, y2 = sol2.y
  61. plt.plot(x2, y2, 'red')
  62.  
  63. sol3 = solve_ivp(rhs, [0.0, 100.0], (-0.01, -0.02), method='RK45', rtol=1e-12)
  64. x3, y3 = sol3.y
  65. plt.plot(x3, y3, 'yellow')
  66.  
  67. sol4 = solve_ivp(rhs, [0.0, -100.0], (2.0, 0.5), method='RK45', rtol=1e-12)
  68. x4, y4 = sol4.y
  69. plt.plot(x4, y4, 'red')
  70.  
  71. sol5 = solve_ivp(rhs, [0.0, 100.0], (2.0, -0.5), method='RK45', rtol=1e-12)
  72. x5, y5 = sol5.y
  73. plt.plot(x5, y5, 'red')
  74.  
  75. sol6 = solve_ivp(rhs, [0.0, -100.0], (2.0, -0.5), method='RK45', rtol=1e-12)
  76. x6, y6 = sol6.y
  77. plt.plot(x6, y6, 'red')
  78.  
  79. sol7 = solve_ivp(rhs, [0.0, 100.0], (2.2, 0.0), method='RK45', rtol=1e-12)
  80. x7, y7 = sol7.y
  81. plt.plot(x7, y8, 'red')
  82.  
  83. sol8 = solve_ivp(rhs, [0.0, -100.0], (-2.2, 0.0), method='RK45', rtol=1e-12)
  84. x8, y8 = sol8.y
  85. plt.plot(x8, y8, 'red')
  86.  
  87. sol9 = solve_ivp(rhs, [0.0, -100.0], (2.2, 0.0), method='RK45', rtol=1e-12)
  88. x9, y9 = sol9.y
  89. plt.plot(x9, y9, 'red')
  90.  
  91. sol10 = solve_ivp(rhs, [0.0, 100.0], (-2.2, 0.0), method='RK45', rtol=1e-12)
  92. x10, y10 = sol10.y
  93. plt.plot(x10, y10, 'red')
  94.  
  95. sol11 = solve_ivp(rhs, [0.0, 100.0], (0.0, 1.0), method='RK45', rtol=1e-12)
  96. x11, y11 = sol11.y
  97. plt.plot(x11, y11, 'green')
  98.  
  99. sol12 = solve_ivp(rhs, [0.0, 100.0], (1.0, 0.0), method='RK45', rtol=1e-12)
  100. x12, y12 = sol12.y
  101. plt.scatter(x12, y12, color='blue', marker='o')
  102.  
  103. sol20 = solve_ivp(rhs, [0.0, 100.0], (2.01, np.sqrt(24)*0.01), method='RK45', rtol=1e-12)
  104. x20, y20 = sol20.y
  105. plt.plot(x20, y20, 'yellow')
  106.  
  107. sol13 = solve_ivp(rhs, [0.0, -100.0], (2.01, np.sqrt(24)*0.01), method='RK45', rtol=1e-12)
  108. x13, y13 = sol13.y
  109. plt.plot(x13, y13, 'yellow')
  110.  
  111. sol14 = solve_ivp(rhs, [0.0, -100.0], (1.99, np.sqrt(24)*0.01), method='RK45', rtol=1e-12)
  112. x14,y14 = sol14.y
  113. plt.plot(x14, y14, 'yellow')
  114.  
  115. sol15 = solve_ivp(rhs, [0.0, 100.0], (1.99, -np.sqrt(24)*0.01), method='RK45', rtol=1e-12)
  116. x15,y15 = sol15.y
  117. plt.plot(x15, y15, 'yellow')
  118.  
  119. sol16 = solve_ivp(rhs, [0.0, 100.0], (-1.0, 0.0), method='RK45', rtol=1e-12)
  120. x16, y16 = sol16.y
  121. plt.scatter(x16, y16, color='blue', marker='o')
  122.  
  123. sol17 = solve_ivp(rhs, [0.0, 100.0], (0.0, 0.0), method='RK45', rtol=1e-12)
  124. x17, y17 = sol17.y
  125. plt.plot(x17, y17, 'r+')
  126.  
  127. sol17 = solve_ivp(rhs, [0.0, 100.0], (-2.0, 0.0), method='RK45', rtol=1e-12)
  128. x17, y17 = sol17.y
  129. plt.plot(x17, y17, 'r+')
  130.  
  131. sol17 = solve_ivp(rhs, [0.0, 100.0], (2.0, 0.0), method='RK45', rtol=1e-12)
  132. x17, y17 = sol17.y
  133. plt.plot(x17, y17, 'r+')
  134.  
  135. sol18 = solve_ivp(rhs, [0.0, 100.0], (0.5, 0.0), method='RK45', rtol=1e-12)
  136. x18, y18 = sol18.y
  137. plt.plot(x18, y18, 'green')
  138.  
  139. sol19 = solve_ivp(rhs, [0.0, 100.0], (-0.5, 0.0), method='RK45', rtol=1e-12)
  140. x19, y19 = sol19.y
  141. plt.plot(x19, y19, 'green')
  142.  
  143. # Отображение результатов
  144. plt.show()
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement