Advertisement
Dari_

Untitled

Dec 8th, 2019
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. from math import *
  2.  
  3. def f(x, y):
  4.     return (x - x * x) * y
  5.  
  6. def t1(x, y):
  7.     return 3 - y - x
  8.  
  9. def t2(x, y):
  10.     return y - y * x
  11.  
  12. def t3(x, y):
  13.     return (y - y * y) * x
  14.  
  15. def f1(x, u, v):
  16.     return cos(u + 1.1 * v) + 2.1
  17.  
  18. def f2(x, u, v):
  19.     return 1.1 / (x + 2.1 * u * u) + x + 1
  20.  
  21. def rut2(x0, y_past, h, n, f):
  22.     print("ROOT2")
  23.     for i in range(n):
  24.         x_past = x0 + i * h
  25.         x_now = x0 + (i + 1) * h
  26.         f_past = f(x_past, y_past)
  27.         y_now = y_past + (f_past + f(x_now, y_past + f_past * h)) * h / 2
  28.         print(x_now, y_now)
  29.         y_past = y_now
  30.  
  31.  
  32. def rut2_sys(x0, u_past, v_past, h, n):
  33.     print("ROOT2_SYS")
  34.     for i in range(n):
  35.         x_past = x0 + i * h
  36.         x_now = x0 + (i + 1) * h
  37.         fu = u_past + f1(x_past, u_past, v_past) * h
  38.         fv = v_past + f2(x_past, u_past, v_past) * h
  39.         u_now = u_past + (f1(x_past, u_past, v_past) + f1(x_now, fu, fv)) * h / 2
  40.         v_now = v_past + (f2(x_past, u_past, v_past) + f2(x_now, fu, fv)) * h / 2
  41.         print(x_now, u_now, v_now)
  42.         u_past = u_now
  43.         v_past = v_now
  44.  
  45. def rut4(x0, y_past, h, n, f):
  46.     print("ROOT4")
  47.     for i in range(n):
  48.         x_past = x0 + i * h
  49.         x_now = x0 + (i + 1) * h
  50.         k1 = h * f(x_past, y_past)
  51.         k2 = h * f(x_past + h / 2, y_past + k1 / 2)
  52.         k3 = h * f(x_past + h / 2, y_past + k2 / 2)
  53.         k4 = h * f(x_past + h, y_past + k3)
  54.         y_now = y_past + (k1 + 2 * k2 + 2 * k3 + k4) / 6
  55.         print(x_now, y_now)
  56.         y_past = y_now
  57.  
  58.  
  59. def rut4_sys(x0, u_past, v_past, h, n):
  60.     print("ROOT4_SYS")
  61.     for i in range(n):
  62.         x_past = x0 + i * h
  63.         x_now = x0 + (i + 1) * h
  64.         k1 = h * f1(x_past, u_past, v_past)
  65.         l1 = h * f2(x_past, u_past, v_past)
  66.         k2 = h * f1(x_past + h / 2, u_past + k1 / 2, v_past + l1 / 2)
  67.         l2 = h * f2(x_past + h / 2, u_past + k1 / 2, v_past + l1 / 2)
  68.         k3 = h * f1(x_past + h / 2, u_past + k2 / 2, v_past + l2 / 2)
  69.         l3 = h * f2(x_past + h / 2, u_past + k2 / 2, v_past + l2 / 2)
  70.         k4 = h * f1(x_past + h, u_past + k3, v_past + l3)
  71.         l4 = h * f2(x_past + h, u_past + k3, v_past + l3)
  72.         u_now = u_past + (k1 + 2 * k2 + 2 * k3 + k4) / 6
  73.         v_now = v_past + (l1 + 2 * l2 + 2 * l3 + l4) / 6
  74.         print(x_now, u_now, v_now)
  75.         u_past = u_now
  76.         v_past = v_now
  77.  
  78.  
  79. n = int(input())
  80. a, b = map(int, input().split())
  81. el = input()
  82. if (el == 'eq'):      #-eq
  83.     num = input()
  84.     x0 = 0
  85.     y0 = 1
  86.     h = (b - a) / n
  87.  
  88.     if (num == '2'):
  89.         rut2(x0, y0, h, n, f)
  90.  
  91.     if (num == '4'):
  92.         rut4(x0, y0, h, n, f)
  93.  
  94. if (el == 'sys'):      #-sys
  95.     num = input()
  96.     x0 = 0
  97.     y10 = 1
  98.     y20 = 1
  99.     h = (b - a) / n
  100.  
  101.     if (num == '4'):
  102.         rut4_sys(x0, y10, y20, h, n)
  103.     if (num == '2'):
  104.         rut2_sys(x0, y10, y20, h, n)
  105.  
  106.  
  107. if (el == 'test'):     #-test
  108.     h = (b - a) / n
  109.     rut2(0, 0, h, n, t1)
  110.     print("\n")
  111.     rut4(0, 0, h, n, t1)
  112.     print("\n")
  113.     rut2(0, 5, h, n, t2)
  114.     print("\n")
  115.     rut4(0, 5, h, n, t2)
  116.     print("\n")
  117.     rut2(0, 3, h, n, t3)
  118.     print("\n")
  119.     rut4(0, 3, h, n, t3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement