mirosh111000

НПтаМ_Ат_Мірошниченко

Apr 10th, 2026
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. from __future__ import annotations
  2.  
  3. import math
  4. from pathlib import Path
  5.  
  6. import matplotlib.pyplot as plt
  7. import numpy as np
  8. from scipy.integrate import solve_ivp
  9.  
  10. S_VALUE = 0.7
  11. CASES = [
  12.     ('a', 0.01, r'$\tau=0.01$'),
  13.     ('b', 1.0,  r'$\tau=1$'),
  14.     ('c', 100.0, r'$\tau=100$'),
  15. ]
  16.  
  17.  
  18. def rhs(_, z, s, tau):
  19.     eta, h = z
  20.     return np.array([
  21.         -eta + h,
  22.         (s * eta - h * (1.0 + eta**2)) / tau,
  23.     ])
  24.  
  25.  
  26. def lyapunov_roots(s, tau):
  27.     disc = (tau + 1.0) ** 2 - 4.0 * tau * (1.0 - s)
  28.     root = math.sqrt(disc)
  29.     lam1 = (-(tau + 1.0) + root) / (2.0 * tau)
  30.     lam2 = (-(tau + 1.0) - root) / (2.0 * tau)
  31.     return lam1, lam2
  32.  
  33.  
  34. def build_initial_conditions():
  35.     initials = []
  36.     for eta0 in np.linspace(-1.2, 1.2, 7):
  37.         for h0 in np.linspace(-1.0, 1.0, 6):
  38.             if abs(eta0) < 0.08 and abs(h0) < 0.08:
  39.                 continue
  40.             initials.append((eta0, h0))
  41.     return initials
  42.  
  43.  
  44. def make_plot(output, s=S_VALUE):
  45.     etas = np.linspace(-1.4, 1.4, 250)
  46.     hs = np.linspace(-1.15, 1.15, 250)
  47.     E, H = np.meshgrid(etas, hs)
  48.     initials = build_initial_conditions()
  49.  
  50.     fig, axes = plt.subplots(1, 3, figsize=(13.5, 4.3), constrained_layout=True)
  51.  
  52.     for ax, (_, tau, title) in zip(axes, CASES):
  53.         U = -E + H
  54.         V = (s * E - H * (1.0 + E**2)) / tau
  55.         ax.streamplot(etas, hs, U, V, density=1.1, color='0.75', linewidth=0.75, arrowsize=0.75)
  56.  
  57.         t_max = 80 if tau < 10 else 250
  58.         t_eval = np.linspace(0, t_max, 2200)
  59.         for idx, z0 in enumerate(initials):
  60.             sol = solve_ivp(rhs, [0, t_max], z0, t_eval=t_eval, args=(s, tau), rtol=1e-7, atol=1e-9)
  61.             lw = 1.0 if idx % 5 else 1.2
  62.             ax.plot(sol.y[0], sol.y[1], color='black', linewidth=lw, alpha=0.85)
  63.  
  64.         ax.plot(etas, etas, linestyle='--', linewidth=1.4, color='black', label=r'$\dot{\eta}=0$')
  65.         ax.plot(etas, s * etas / (1 + etas**2), linestyle=':', linewidth=1.8, color='black', label=r'$\dot{h}=0$')
  66.         ax.scatter([0], [0], s=28, color='black', zorder=5)
  67.         ax.text(0.05, 0.05, 'D', fontsize=11, transform=ax.transAxes)
  68.         ax.set_title(title, fontsize=13, pad=8)
  69.         ax.set_xlabel(r'$\eta$', fontsize=12)
  70.         if ax is axes[0]:
  71.             ax.set_ylabel(r'$h$', fontsize=12)
  72.         ax.set_xlim(-1.35, 1.35)
  73.         ax.set_ylim(-1.05, 1.05)
  74.         ax.grid(alpha=0.18)
  75.  
  76.     handles = [
  77.         plt.Line2D([0], [0], linestyle='--', linewidth=1.4, color='black', label=r'$\dot{\eta}=0$'),
  78.         plt.Line2D([0], [0], linestyle=':', linewidth=1.8, color='black', label=r'$\dot{h}=0$'),
  79.         plt.Line2D([0], [0], color='black', linewidth=1.1, label='траєкторії'),
  80.     ]
  81.     fig.legend(handles=handles, loc='lower center', ncol=3, frameon=False, bbox_to_anchor=(0.5, -0.03), fontsize=11)
  82.     fig.savefig(output, dpi=220, bbox_inches='tight')
  83.     plt.close(fig)
  84.  
  85.  
  86. def main():
  87.     output = Path('phase_portraits_task2.png')
  88.     make_plot(output)
  89.  
  90.     print('Завдання №2: неупорядкована фаза для випадку τ_s << τ_η, τ_h; s =', S_VALUE)
  91.     for label, tau, _ in CASES:
  92.         lam1, lam2 = lyapunov_roots(S_VALUE, tau)
  93.         print(f'{label}: tau={tau:>6g}; lambda1={lam1: .6f}; lambda2={lam2: .6f}; тип точки D -> стійкий вузол')
  94.     print(f'Зображення збережено у файл: {output.resolve()}')
  95.  
  96.  
  97. if __name__ == '__main__':
  98.     main()
  99.  
Advertisement
Add Comment
Please, Sign In to add comment