Advertisement
mirosh111000

lr3(1)

Oct 17th, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy.integrate import odeint
  4.  
  5. omega0 = 1.02
  6. a = 0.0001
  7. b = 0.2
  8.  
  9. def system(x, t, a, b):
  10.     dxdt = x[1]
  11.     d2xdt2 = -omega0**2 * x[0] - b * x[1] + a
  12.     return [dxdt, d2xdt2]
  13.  
  14. def initial_conditions(a, b, top=True):
  15.     x0 = [0, 0]
  16.     if top:
  17.         x0[1] = np.sqrt(abs(a)) / omega0
  18.     else:
  19.         x0[1] = -np.sqrt(abs(a)) / omega0
  20.     return x0
  21.  
  22. t = np.linspace(0, 30, 1000)
  23.  
  24. x0_positive = initial_conditions(a, b, top=True)
  25. solution_positive = odeint(system, x0_positive, t, args=(a, b))
  26.  
  27. V_x = 0.5 * omega0**2 * solution_positive[:, 0]**2 + a * solution_positive[:, 0]
  28.  
  29. plt.figure(figsize=(8, 6))
  30. plt.plot(solution_positive[:, 0], V_x, label='V(x)')
  31. plt.xlabel('x')
  32. plt.ylabel('V(x)')
  33. plt.title('Залежність потенціальної енергії V(x) від x')
  34. plt.legend()
  35. plt.grid()
  36. plt.show()
  37.  
  38. plt.figure(figsize=(8, 6))
  39. plt.plot(solution_positive[:, 0], solution_positive[:, 1], label='a > 0 (dx/dt < 0)')
  40. plt.scatter([x0_positive[0]], [x0_positive[1]], color='blue', marker='>', label='Start', s=100)
  41. plt.xlabel('x')
  42. plt.ylabel('dx/dt')
  43. plt.title('Фазовий портрет')
  44. plt.legend()
  45. plt.grid()
  46. plt.show()
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement