Advertisement
mirosh111000

Лабораторна робота 2

Feb 18th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.72 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import seaborn as sns
  5. from scipy.optimize import root, minimize
  6.  
  7.  
  8. def equation(a, x, eps):
  9.     return a * (1 - x) - x * np.exp(-eps * x)
  10.  
  11. def jacobian(a, x, eps):
  12.     df_dx = -a - np.exp(-eps * x) * (1 - eps * x)
  13.     return df_dx
  14.  
  15. def is_stable(stationary_points, stability):
  16.     stable = []
  17.     not_stable = []
  18.     for i in range(len(stationary_points)):
  19.        
  20.         if stability[i] == 'b':
  21.             stable.append(stationary_points[i])
  22.             not_stable.append(np.nan)
  23.         else:
  24.             not_stable.append(stationary_points[i])
  25.             stable.append(np.nan)
  26.     stable = np.array(stable)
  27.     not_stable = np.array(not_stable)
  28.    
  29.     return stable, not_stable
  30.    
  31.    
  32. def find_min(x_values, alpha, epsilon):
  33.     min_ = []
  34.     for i in range(1, len(x_values)-2):
  35.         d2V_dx2_prev = (V(x_values[i-1], alpha, epsilon) - 2*V(x_values[i], alpha, epsilon) + V(x_values[i+1], alpha, epsilon))/(dx*dx)
  36.         d2V_dx2_next = (V(x_values[i], alpha, epsilon) - 2*V(x_values[i+1], alpha, epsilon) + V(x_values[i+2], alpha, epsilon))/(dx*dx)
  37.         if d2V_dx2_prev * d2V_dx2_next < 0:
  38.             min_.append(x_values[i])
  39.     return min_
  40.  
  41.  
  42. def V(x, alpha, epsilon):
  43.     eq = -alpha*(x - 0.5*x**2) + (-x*epsilon*np.exp(-epsilon*x) - np.exp(-epsilon*x))/(epsilon**2)
  44.     return eq
  45.  
  46.  
  47. def alpha_st(x, epsilon):
  48.     eq = -(x*np.exp(-epsilon*x))/(-1.0+x)
  49.     return eq
  50.  
  51.  
  52. epsilons = np.array([0.01, 0.1, 1, 2, 4, 6])
  53. X = np.linspace(0, 1, 1000)
  54.  
  55.  
  56. colors = [
  57.     "#FF5733", "#33FF57", "#5733FF", "#FFFF33", "#33FFFF",
  58.     "#FF33FF", "#FF5733", "#33FF57", "#5733FF", "#FFFF33",
  59.     "#33FFFF", "#FF33FF", "#FF5733", "#33FF57", "#5733FF",
  60.     "#FFFF33", "#33FFFF", "#FF33FF", "#FF5733", "#33FF57"
  61. ]
  62.  
  63. Stability = []
  64. stationary_points = []
  65. for eps in epsilons:
  66.     stationary_points_eps = []
  67.     Stability_eps = []
  68.     for x in X:
  69.         result = root(equation, x0=[0.1], args=(x, eps))
  70.         if result.success:
  71.             stationary_point = result.x
  72.             stability = 'b' if jacobian(stationary_point, x, eps) < 0 else 'r'
  73.             Stability_eps.append(stability)
  74.             stationary_point = result.x
  75.             stationary_points_eps.append(stationary_point[0])
  76.         else:
  77.             stationary_points_eps.append(np.nan)
  78.             Stability_eps.append('white')
  79.     stationary_points.append(stationary_points_eps)
  80.     Stability.append(Stability_eps)
  81.  
  82. stationary_points = np.array(stationary_points)
  83. Stability = np.array(Stability)
  84.  
  85. plt.figure(figsize=(15, 8))
  86.  
  87. for i, eps in enumerate(epsilons):
  88.     plt.plot(stationary_points[i], X, color=colors[i], label=f'eps={np.round(eps, 2)}')
  89.    
  90.    
  91. plt.xlabel(r'$\alpha$')
  92. plt.ylabel('X')
  93. plt.title(r'$\alpha (1 - x) x \exp(- \epsilon x) = 0$', size=20)
  94. plt.legend()
  95. plt.xlim(0, 0.5)
  96. plt.grid(True)
  97. plt.show()
  98.  
  99.  
  100. plt.figure(figsize=(15, 8))
  101. plt.plot(stationary_points[3], X, color=colors[3], lw=1.75, label=f'eps={np.round(epsilons[3], 2)}')
  102.  
  103.  
  104. stable, not_stable = is_stable(stationary_points[3], Stability[3])
  105. plt.plot(stable, X, color='b', lw=1, label=f'eps={np.round(epsilons[3], 2)}, стікий стан')
  106. plt.plot(not_stable, X, color='red', lw=1, label=f'eps={np.round(epsilons[3], 2)}, не стійкий стан')
  107. plt.xlabel(r'$\alpha$')
  108. plt.ylabel('X')
  109. plt.title(r'$\alpha (1 - x) x \exp(- \epsilon x) = 0$', size=20)
  110. plt.legend()
  111. plt.xlim(0, 0.5)
  112. plt.grid(True)
  113. plt.show()
  114.  
  115.  
  116. plt.figure(figsize=(15, 8))
  117. plt.plot(stationary_points[-1], X, color=colors[5], lw=1.75, label=f'eps={np.round(epsilons[-1], 2)}')
  118. stable, not_stable = is_stable(stationary_points[-1], Stability[-1])
  119. plt.plot(stable, X, color='b', lw=1, label=f'eps={np.round(epsilons[-1], 2)}, стікий стан')
  120. plt.plot(not_stable, X, color='red', lw=1, label=f'eps={np.round(epsilons[-1], 2)}, не стійкий стан')
  121. plt.plot()
  122.  
  123. plt.xlabel(r'$\alpha$')
  124. plt.ylabel('X')
  125. plt.title(r'$\alpha (1 - x) x \exp(- \epsilon x) = 0$', size=20)
  126. plt.legend()
  127. plt.xlim(0, 0.1)
  128. plt.grid(True)
  129. plt.show()
  130.  
  131.  
  132.  
  133.  
  134.  
  135. dx = 10**-4
  136. x_values = np.arange(0, 1, dx)
  137. alphas = [0.01, 0.025, 0.04, 0.06, 0.08, 0.09]
  138.  
  139. num_plots = len(alphas)
  140. num_rows = 2
  141. num_cols = 3
  142.  
  143. fig, axes = plt.subplots(num_rows, num_cols, figsize=(12, 10))
  144.  
  145. for i, alpha in enumerate(alphas):
  146.     row = i // num_cols
  147.     col = i % num_cols
  148.     ax = axes[row, col]
  149.    
  150.     V_values = [V(x, alpha, 6) for x in x_values]
  151.     ax.plot(x_values, V_values, label=f'alpha = {alpha}')
  152.     ax.set_xlabel('$x$')
  153.     ax.set_ylabel('$V(x)$')
  154.     ax.set_title(f'$V(x): \epsilon=6$')
  155.     ax.set_xlim(0, 1)
  156.     ax.legend()
  157.     ax.grid(True)
  158.  
  159. plt.tight_layout()
  160. plt.show()
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. dx = 10**-4
  173. x_values = np.arange(0, 1, dx)
  174.  
  175. min_per_epsilon = {}
  176.  
  177.  
  178. for epsilon in np.arange(3, 6.1, 0.1):
  179.     alpha_values = []
  180.     for alpha in np.arange(0, 0.2, 0.01):
  181.         V_values = [V(x, alpha, epsilon) for x in x_values]
  182.         min_ = find_min(x_values, alpha, epsilon)
  183.         if len(min_) == 2:
  184.             V_min = [V(x, alpha, epsilon) for x in min_]
  185.             if np.abs(V_min[0] - V_min[1]) < 0.001:  
  186.                 alpha_values.append(alpha)
  187.    
  188.    
  189.     min_per_epsilon[epsilon] = alpha_values
  190.  
  191.  
  192. average_epsilon_per_alpha = {}
  193. for epsilon, alpha_values in min_per_epsilon.items():
  194.     for alpha in alpha_values:
  195.         if alpha not in average_epsilon_per_alpha:
  196.             average_epsilon_per_alpha[alpha] = []
  197.         average_epsilon_per_alpha[alpha].append(epsilon)
  198.  
  199.  
  200. for alpha, epsilons in average_epsilon_per_alpha.items():
  201.     average_epsilon = np.mean(epsilons)
  202.    
  203.  
  204.  
  205.  
  206.  
  207.  
  208. dx = 0.00001
  209. epsilon_1_list = []
  210. a2_1_list = []
  211. epsilon_2_list = []
  212. a2_2_list = []
  213.  
  214. for epsilon in np.arange(3, 6.1, 0.1):
  215.     for x in np.arange(0.00001, 0.99999, dx):
  216.         a1 = alpha_st(x - dx, epsilon)
  217.         a2 = alpha_st(x, epsilon)
  218.         a3 = alpha_st(x + dx, epsilon)
  219.         if a1 < a2 and a3 < a2:
  220.             epsilon_1_list.append(epsilon)
  221.             a2_1_list.append(a2)
  222.         if a1 > a2 and a3 > a2:
  223.             epsilon_2_list.append(epsilon)
  224.             a2_2_list.append(a2)
  225.             break
  226.  
  227.  
  228. alphas = []
  229. average_epsilons = []
  230.  
  231. for alpha, epsilons in average_epsilon_per_alpha.items():
  232.     alphas.append(alpha)
  233.     average_epsilons.append(np.mean(epsilons))
  234.  
  235. plt.figure(figsize=(15, 8))
  236. plt.plot(alphas, average_epsilons, marker='o', linestyle='-')
  237. plt.plot(a2_1_list, epsilon_1_list, 'o', linestyle='-', label='Не стійкий стан')
  238. plt.plot(a2_2_list, epsilon_2_list, 'o', linestyle='-', label='Стійкий стан')
  239. plt.legend()
  240. plt.xlabel(r'$\alpha$')
  241. plt.ylabel(r'$\epsilon$')
  242. plt.title('Фазова діаграма')
  243. plt.grid(True)
  244. plt.show()  
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement