mirosh111000

НПтаМ_ПР№2_Мірошниченко

Mar 16th, 2026
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.58 KB | None | 0 0
  1.  
  2. import math
  3. import os
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. from scipy.integrate import solve_ivp
  7.  
  8. def system(t, y, s, tau):
  9.     eta, S = y
  10.     return [-eta * (1 - s * S), (1 - S * (1 + eta**2)) / tau]
  11.  
  12. def lambda_D(s, tau):
  13.     a = (s - 1) - 1 / tau
  14.     disc = 1 + 4 * (1 / tau) * (s - 1) / (a * a)
  15.     r = math.sqrt(disc)
  16.     return 0.5 * a * (1 + r), 0.5 * a * (1 - r)
  17.  
  18. def lambda_O(s, tau):
  19.     disc = 1 - 8 * tau * (s - 1) / (s * s)
  20.     pref = -s / (2 * tau)
  21.     if disc >= 0:
  22.         r = math.sqrt(disc)
  23.         return pref * (1 + r), pref * (1 - r)
  24.     r = math.sqrt(-disc)
  25.     return complex(pref, -pref * r), complex(pref, pref * r)
  26.  
  27. def plot_phase_portraits(s, taus, ordered, out_path):
  28.     fig, axes = plt.subplots(1, 3, figsize=(14, 4.5), constrained_layout=True)
  29.     labels = ["а", "б", "в"]
  30.  
  31.     for ax, tau, lab in zip(axes, taus, labels):
  32.         if ordered:
  33.             Smin, Smax = 0.0, 1.5
  34.             etamin, etamax = -1.3, 1.3
  35.         else:
  36.             Smin, Smax = 0.0, 2.0
  37.             etamin, etamax = -1.1, 1.1
  38.  
  39.         Sg = np.linspace(Smin, Smax, 120)
  40.         Eg = np.linspace(etamin, etamax, 120)
  41.         X, Y = np.meshgrid(Sg, Eg)
  42.  
  43.         U = (1 - X * (1 + Y**2)) / tau
  44.         V = -Y * (1 - s * X)
  45.         speed = np.sqrt(U * U + V * V)
  46.  
  47.         ax.streamplot(
  48.             Sg, Eg, U / (speed + 1e-9), V / (speed + 1e-9),
  49.             density=1.0, linewidth=0.75, arrowsize=0.7, color="0.25"
  50.         )
  51.  
  52.         eta_nc = np.linspace(etamin, etamax, 400)
  53.         ax.plot(1 / (1 + eta_nc**2), eta_nc, "--", lw=1, color="0.5")
  54.         ax.axhline(0, ls=":", lw=1, color="0.5")
  55.         ax.axvline(1 / s, ls=":", lw=1, color="0.7")
  56.  
  57.         if ordered:
  58.             eta0 = math.sqrt(s - 1)
  59.             inits = [
  60.                 (1.1, 0.1), (-1.1, 0.1), (1.1, 1.4), (-1.1, 1.4),
  61.                 (0.0, 1.4), (0.4, 0.5), (-0.4, 0.5),
  62.                 (0.9, 1.0), (-0.9, 1.0),
  63.             ]
  64.         else:
  65.             inits = [
  66.                 (1.0, 0.1), (-1.0, 0.1), (1.0, 1.9), (-1.0, 1.9),
  67.                 (0.0, 1.8), (0.6, 0.5), (-0.6, 0.5),
  68.                 (0.6, 1.2), (-0.6, 1.2),
  69.             ]
  70.  
  71.         t_end = 25 if tau == 0.01 else (40 if tau == 1 else 500)
  72.         max_step = 0.05 if tau == 0.01 else (0.1 if tau == 1 else 1.0)
  73.  
  74.         for e0, s0 in inits:
  75.             sol = solve_ivp(
  76.                 system, [0, t_end], [e0, s0],
  77.                 args=(s, tau), method="Radau",
  78.                 max_step=max_step, rtol=1e-5, atol=1e-8
  79.             )
  80.             ax.plot(sol.y[1], sol.y[0], color="k", lw=0.8)
  81.  
  82.         ax.plot([1], [0], "ko", ms=3)
  83.         ax.text(1.02, -0.08, "D", fontsize=10)
  84.  
  85.         if ordered:
  86.             ax.plot([1 / s, 1 / s], [eta0, -eta0], "ko", ms=3)
  87.             ax.text(1 / s + 0.03, eta0 + 0.03, "O+", fontsize=10)
  88.             ax.text(1 / s + 0.03, -eta0 - 0.12, "O-", fontsize=10)
  89.  
  90.         ax.set_title(f"{lab}) τ = {tau:g}")
  91.         ax.set_xlim(Smin, Smax)
  92.         ax.set_ylim(etamin, etamax)
  93.         ax.set_xlabel("S")
  94.         ax.set_ylabel("η")
  95.  
  96.     fig.savefig(out_path, dpi=220, bbox_inches="tight")
  97.     plt.close(fig)
  98.  
  99. if __name__ == "__main__":
  100.     os.makedirs("phase_output", exist_ok=True)
  101.     taus = [0.01, 1, 100]
  102.     plot_phase_portraits(0.7, taus, ordered=False, out_path="phase_output/part1.png")
  103.     plot_phase_portraits(1.5, taus, ordered=True, out_path="phase_output/part2.png")
  104.     print("λ_D for s = 0.7:")
  105.     for tau in taus:
  106.         print(tau, lambda_D(0.7, tau))
  107.     print("λ_O for s = 1.5:")
  108.     for tau in taus:
  109.         print(tau, lambda_O(1.5, tau))
  110.  
Advertisement
Add Comment
Please, Sign In to add comment