Advertisement
vatman

Untitled

Sep 28th, 2023
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.36 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, 'b-')
  58.  
  59. plt.scatter(x1, y1, color='yellow', marker='.')
  60.  
  61. sol2 = solve_ivp(rhs, [0.0, 100.0], (2.0, 0.5), method='RK45', rtol=1e-12)
  62. x2, y2 = sol2.y
  63. plt.scatter(x2, y2, color='red', marker='.')
  64.  
  65. sol3 = solve_ivp(rhs, [0.0, 100.0], (-0.01, -0.02), method='RK45', rtol=1e-12)
  66. x3, y3 = sol3.y
  67. plt.scatter(x3, y3, color='yellow', marker='.')
  68.  
  69. sol4 = solve_ivp(rhs, [0.0, -100.0], (2.0, 0.5), method='RK45', rtol=1e-12)
  70. x4, y4 = sol4.y
  71. plt.scatter(x4, y4, color='red', marker='.')
  72.  
  73. sol5 = solve_ivp(rhs, [0.0, 100.0], (2.0, -0.5), method='RK45', rtol=1e-12)
  74. x5, y5 = sol5.y
  75. plt.scatter(x5, y5, color='red', marker='.')
  76.  
  77. sol6 = solve_ivp(rhs, [0.0, -100.0], (2.0, -0.5), method='RK45', rtol=1e-12)
  78. x6, y6 = sol6.y
  79. plt.scatter(x6, y6, color='red', marker='.')
  80.  
  81. sol7 = solve_ivp(rhs, [0.0, 100.0], (2.2, 0.0), method='RK45', rtol=1e-12)
  82. x7, y7 = sol7.y
  83. plt.scatter(x7, y7, color='red', marker='.')
  84.  
  85. sol8 = solve_ivp(rhs, [0.0, -100.0], (-2.2, 0.0), method='RK45', rtol=1e-12)
  86. x8, y8 = sol8.y
  87. plt.scatter(x8, y8, color='red', marker='.')
  88.  
  89. sol9 = solve_ivp(rhs, [0.0, -100.0], (2.2, 0.0), method='RK45', rtol=1e-12)
  90. x9, y9 = sol9.y
  91. plt.scatter(x9, y9, color='red', marker='.')
  92.  
  93. sol10 = solve_ivp(rhs, [0.0, 100.0], (-2.2, 0.0), method='RK45', rtol=1e-12)
  94. x10, y10 = sol10.y
  95. plt.scatter(x10, y10, color='red', marker='.')
  96.  
  97. sol11 = solve_ivp(rhs, [0.0, 100.0], (0.0, 1.0), method='RK45', rtol=1e-12)
  98. x11, y11 = sol11.y
  99. plt.scatter(x11, y11, color='green', marker='.')
  100.  
  101. sol12 = solve_ivp(rhs, [0.0, 100.0], (1.0, 0.0), method='RK45', rtol=1e-12)
  102. x12, y12 = sol12.y
  103. plt.scatter(x12, y12, color='blue', marker='0')
  104.  
  105. sol13 = solve_ivp(rhs, [0.0, 100.0], (2.01, np.sqrt(24)*0.01), method='RK45', rtol=1e-12)
  106. x13, y13 = sol13.y
  107. plt.scatter(x13, y13, color='yellow', marker='.')
  108.  
  109. sol13 = solve_ivp(rhs, [0.0, -100.0], (2.01, np.sqrt(24)*0.01), method='RK45', rtol=1e-12)
  110. x13, y13 = sol13.y
  111. plt.scatter(x13, y13, color='yellow', marker='.')
  112.  
  113. sol14 = solve_ivp(rhs, [0.0, -100.0], (1.99, np.sqrt(24)*0.01), method='RK45', rtol=1e-12)
  114. x14,y14 = sol14.y
  115. plt.scatter(x14, y14, color='yellow', marker='.')
  116.  
  117. sol15 = solve_ivp(rhs, [0.0, 100.0], (1.99, -np.sqrt(24)*0.01), method='RK45', rtol=1e-12)
  118. x15,y15 = sol15.y
  119. plt.scatter(x15, y15, color='yellow', marker='.')
  120.  
  121. sol16 = solve_ivp(rhs, [0.0, 100.0], (-1.0, 0.0), method='RK45', rtol=1e-12)
  122. x16, y16 = sol16.y
  123. plt.scatter(x16, y16, color='blue', marker='+')
  124.  
  125. sol17 = solve_ivp(rhs, [0.0, 100.0], (0.0, 0.0), method='RK45', rtol=1e-12)
  126. x17, y17 = sol17.y
  127. plt.scatter(x17, y17, color='red', marker='+')
  128.  
  129. sol17 = solve_ivp(rhs, [0.0, 100.0], (-2.0, 0.0), method='RK45', rtol=1e-12)
  130. x17, y17 = sol17.y
  131. plt.scatter(x17, y17, color='red', marker='+')
  132.  
  133. sol17 = solve_ivp(rhs, [0.0, 100.0], (2.0, 0.0), method='RK45', rtol=1e-12)
  134. x17, y17 = sol17.y
  135. plt.scatter(x17, y17, color='red', marker='+')
  136.  
  137. sol18 = solve_ivp(rhs, [0.0, 100.0], (0.5, 0.0), method='RK45', rtol=1e-12)
  138. x18, y18 = sol18.y
  139. plt.scatter(x18, y18, color='green', marker='.')
  140.  
  141. sol19 = solve_ivp(rhs, [0.0, 100.0], (-0.5, 0.0), method='RK45', rtol=1e-12)
  142. x19, y19 = sol19.y
  143. plt.scatter(x19, y19, color='green', marker='.')
  144.  
  145. # Построение графика траектории
  146. # x1, y1 = sol1.y
  147. # plt.plot(x1, y1, 'k-')
  148.  
  149. # Отображение результатов
  150. plt.show()
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement