Advertisement
HellFinger

Untitled

Dec 12th, 2021
1,121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. from math import tan, fabs, cos, sin
  2.  
  3.  
  4. def first(x):
  5.     # return 3 * x1**2 - x1 + x2**2 - 1
  6.     # return 2*x1**2 - x1*x2 - 5*x1 + 1
  7.     return cos(x) + 2
  8.  
  9.  
  10. def second(x):
  11.     # return x2 - tan(x1)
  12.     # return x1 + 3*log10(x1) - x2**2
  13.     return sin(x) + 2
  14.  
  15.  
  16. def is_continue(x1, old_x1, x2, old_x2, eps):
  17.     return fabs(x1 - old_x1) > eps or (fabs(x2 - old_x2)) > eps
  18.  
  19.  
  20. def simple(system, eps=0.01):
  21.     new_system = [lambda x1, x2: (1 - x2**2) / (3*x1 - 1), lambda x: tan(x)]
  22.     x1 = -0.5
  23.     old_x1 = 0
  24.     x2 = -0.5
  25.     old_x2 = 0
  26.     pribl = [x1, x2]
  27.  
  28.     iters = 0
  29.     while is_continue(x1, old_x1, x2, old_x2, eps):
  30.         old_x1 = x1
  31.         old_x2 = x2
  32.         x1 = new_system[0](old_x2)
  33.         x2 = new_system[1](old_x1)
  34.         # print(x1, x2)
  35.         iters += 1
  36.     print("Начальные приближения: ", pribl)
  37.     print("\nИтераций: ", iters)
  38.     return [x1, x2]
  39.  
  40.  
  41. def first_center(y2, y0, h):
  42.     return (y2 - y0) / (2 * h)
  43.  
  44. def newton(system, eps=0.01):
  45.     x1 = 1.5
  46.     old_x1 = 0
  47.     x2 = 2.5
  48.     old_x2 = 0
  49.     pribl = [x1, x2]
  50.  
  51.     iters = 0
  52.     t = 0
  53.     while is_continue(x1, old_x1, x2, old_x2, eps) and t != 2:
  54.         t += 1
  55.         old_x1 = x1
  56.         old_x2 = x2
  57.         # delta_x1 = (-system[1](old_x1) + system[0](old_x2)) / ((-1/(cos(old_x1)**2)) - 6*old_x2 - 1)
  58.         # delta_x2 = -system[0](old_x1, old_x2) - (6*old_x2 - 1) * delta_x1
  59.         delta_x1 = (-system[1](old_x1) + system[0](old_x2))
  60.         delta_x2 =
  61.         x1 = old_x1 + delta_x1
  62.         x2 = old_x2 + delta_x2
  63.         # print(x1, x2)
  64.         iters += 1
  65.     print("Начальные приближения: ", pribl)
  66.     print("\nИтераций: ", iters)
  67.     return [x1, x2]
  68.  
  69. system = [first, second]
  70. eps = 0.0001
  71. # print("Вариант - 4\n")
  72. # print("Точность: ", eps)
  73.  
  74. # print("\nМетод простой итерации: ")
  75. # ans = simple(system, eps)
  76. # print("Ответ: ", ans)
  77. # print("Проверка:")
  78. # print("3 * x1**2 - x1 + x2**2 - 1 = {}\nx2 - tg(x1) = {}".format(system[0](ans[0], ans[1]), system[1](ans[0], ans[1])))
  79.  
  80. print("\nМетод Ньютона: ")
  81. ans = newton(system, eps)
  82. print("Ответ: ", ans)
  83. print("Проверка:")
  84. print("3 * x1**2 - x1 + x2**2 - 1 = {}\nx2 - tg(x1) = {}".format(system[0](ans[0], ans[1]), system[1](ans[0], ans[1])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement