Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.05 KB | None | 0 0
  1. import math
  2.  
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. import scipy.integrate as integrate
  6.  
  7. global l, r, x, y
  8.  
  9.  
  10. def f(x, y):
  11.     return x * y * beta / (x + y)
  12.  
  13.  
  14. def RoundAndZeros(X, Y):
  15.     X = round(X, rou)
  16.     Y = round(Y, rou)
  17.     X = 0 if X < 0 else X
  18.     Y = 0 if Y < 0 else Y
  19.     return X, Y
  20.  
  21.  
  22. def system1(cond1, itter):
  23.     X, Y = cond1[0], cond1[1]
  24.     X, Y = RoundAndZeros(X, Y)
  25.     dXdT = round(-f(X, Y), rou)
  26.     dYdT = round(f(X, Y), rou) - gamma * Y
  27.     return [dXdT, dYdT]
  28.  
  29.  
  30. def system2(cond1, itter):
  31.     X, Y = cond1[0], cond1[1]
  32.     X, Y = RoundAndZeros(X, Y)
  33.     dXdT = round(-f(X, Y), rou) - u if X > 0 else round(-f(X, Y), rou)
  34.     dYdT = round(f(X, Y), rou) - gamma * Y
  35.     return [dXdT, dYdT]
  36.  
  37.  
  38. # вычисление значения y
  39. def f_fun(l, r, e):
  40.     global x, y
  41.     X = np.linspace(l, r, n)  # for forward integration
  42.     cond1 = [x, y]
  43.     if e == 1:
  44.         sol1 = integrate.odeint(system1, cond1, X)
  45.         Psi, Psi1 = sol1[:, 0], sol1[:, 1]
  46.         x, y = Psi[n - 1], Psi1[n - 1]
  47.         x, y = RoundAndZeros(x, y)
  48.         return Psi, Psi1
  49.     if e == 2:
  50.         sol2 = integrate.odeint(system2, cond1, X)
  51.         Fi, Fi1 = sol2[:, 0], sol2[:, 1]
  52.         return Fi, Fi1
  53.  
  54.  
  55. def J(l, a, r):
  56.     global x, y
  57.     # решение задачи коши вперед
  58.     x1, y1 = f_fun(l, a, 1) if l > a else 0, 0
  59.     x2, y2 = f_fun(a, r, 2)
  60.     x, y = x0, y0
  61.     leftIntegral = integrate.quad(integrateLeft, l, a, args=y1[n - 1]) if l > a else [0]
  62.     rightIntegral = integrate.quad(integrateRight, a, r, args=y2[n - 1])
  63.     return leftIntegral[0] + rightIntegral[0]
  64.  
  65.  
  66. def integrateLeft(t, y1):
  67.     return q * y1
  68.  
  69.  
  70. def integrateRight(t, y2):
  71.     return q * y2 + c * u
  72.  
  73.  
  74. def goldenSectionSearch(eps):
  75.     global l, r
  76.     l1, r1 = l, r
  77.     phi = (1 + math.sqrt(5)) / 2
  78.     resphi = 2 - phi
  79.     a1 = l + resphi * (r - l)
  80.     a2 = r - resphi * (r - l)
  81.     f1 = J(l, a1, r)
  82.     f2 = J(l, a2, r)
  83.     while abs(f1 - f2) > eps:
  84.         if f1 < f2:
  85.             r = a2
  86.             a2 = a1
  87.             f2 = f1
  88.             a1 = l + resphi * (r - l)
  89.             f1 = J(l, a1, r)
  90.         else:
  91.             l = a1
  92.             a1 = a2
  93.             f1 = f2
  94.             a2 = r - resphi * (r - l)
  95.             f2 = J(l, a2, r)
  96.     return (a1 + a2) / 2
  97.  
  98.  
  99. # Левый конец
  100. l = 0
  101. # Правый конец
  102. r = 60
  103. t = r
  104. eps = 0.01
  105. n = t + 1  # odd integer number
  106. x0 = 10000
  107. y0 = 4000
  108. x, y = x0, y0
  109. u = 350
  110. c = 1
  111. q = 1
  112. beta = 0.2
  113. gamma = 0.1
  114. X = np.linspace(0, n - 1, n)
  115.  
  116. # округление чисел после запятой
  117. rou = 6
  118.  
  119. zolotoe = goldenSectionSearch(eps)
  120. print("Дата рекомендованной вакцинации", zolotoe)
  121. print("Затраты на вакцинацию при стоимости вакцины 20 по 35 привитых", J(0, zolotoe, t))
  122.  
  123. x1_1, y1_1 = f_fun(0, t, 1)
  124. # plt.plot(X[0:n - 2], x1_1[0:n - 2], color="red", zorder=3)
  125. # plt.plot(X[0:n - 2], y1_1[0:n - 2], color="green", zorder=3)
  126.  
  127. x, y = x0, y0
  128. x1_2, y1_2 = f_fun(0, t, 2)
  129. # plt.plot(X[0:n - 2], x1_2[0:n - 2], zorder=2)
  130. # plt.plot(X[0:n - 2], y1_2[0:n - 2], color="purple", zorder=2)
  131.  
  132. n1 = n
  133. # zolotoe = 61
  134. zolotoe = round(zolotoe)
  135. n = int(zolotoe)
  136.  
  137. # y2_1 = f_fun(0, zolotoe, True)
  138. n1 = t
  139. Jgraf = np.zeros(n1)
  140. nj = 0
  141. while nj < n1:
  142.     Jgraf[nj] = J(0, nj, t)
  143.     nj = nj + 1
  144.  
  145. plt.plot(X[0:n1 - 2], Jgraf[0:n1 - 2], color="gold", zorder=4)
  146. #
  147. # y2_1 = np.zeros(n)
  148. #
  149. # i = 0
  150. # while i < n:
  151. #     y2_1[i] = y1_1[i]
  152. #     i = i + 1
  153. #
  154. # if i != 0:
  155. #     x, y = x1_1[i - 1], y1_1[i - 1]
  156. #
  157. # n = n1 - n
  158. # x2_2, y2_2 = f_fun(zolotoe, t, 2)
  159. #
  160. # n = n1
  161. # y211 = np.zeros(n)
  162. #
  163. # i = 0
  164. # while i < int(zolotoe) - 1:
  165. #     y211[i] = y2_1[i]
  166. #     i = i + 1
  167. #
  168. # j = 0
  169. # while j < (n - zolotoe) - 1:
  170. #     y211[i] = y2_2[j]
  171. #     j = j + 1
  172. #     i = i + 1
  173. #
  174. # ii = 0
  175. # while ii < n:
  176. #     if (y211[ii] <= 0):
  177. #         y211[ii] == None
  178. #     ii = ii + 1
  179. #
  180. # yy = [*y211]
  181. # plt.plot(X[0:n - 2], yy[0:n - 2], color="green", zorder=1)
  182. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement