SHARE
TWEET

lab2(CM) ver 2.1

allekco Nov 17th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def f1(x, y, z):
  2.     return 9*x*z**2 + y*z + 2*y**2*x**3 + 175
  3.  
  4.  
  5. def f2(x, y, z):
  6.     return 0.5*y**3*z**2 - 0.8*x**2*y - 1.3
  7.  
  8.  
  9. def f3(x, y, z):
  10.     return 0.7*z**3 - 6.3*y**3 - 12.6
  11.  
  12.  
  13. def f1_der_x(x, y, z):     #derivative - производная
  14.     return 9*z**2 + 6*y**2*x**2
  15.  
  16.  
  17. def f1_der_y(x, y, z):
  18.     return z + 4*y*x**3
  19.  
  20.  
  21. def f1_der_z(x, y, z):
  22.     return 18*x*z + y
  23.  
  24.  
  25. def f2_der_x(x, y, z):
  26.     return -1.6*x*y
  27.  
  28.  
  29. def f2_der_y(x, y, z):
  30.     return 1.5*y**2*z**2 - 0.8*x**2
  31.  
  32.  
  33. def f2_der_z(x, y, z):
  34.     return y**3*z
  35.  
  36.  
  37. def f3_der_x(x, y, z):
  38.     return 0
  39.  
  40.  
  41. def f3_der_y(x, y, z):
  42.     return -18.9*y**2
  43.  
  44.  
  45. def f3_der_z(x, y, z):
  46.     return 2.1*z**2
  47.  
  48.  
  49. def w_matrix(x, y, z):
  50.     w = [[f1_der_x(x, y, z), f1_der_y(x, y, z), f1_der_z(x, y, z)],
  51.          [f2_der_x(x, y, z), f2_der_y(x, y, z), f2_der_z(x, y, z)],
  52.          [f3_der_x(x, y, z), f3_der_y(x, y, z), f3_der_z(x, y, z)]]
  53.     return w
  54.  
  55.  
  56. def def_matrix(a=[]):
  57.     a1 = a[0][0]
  58.     a2 = a[1][0]
  59.     a3 = a[2][0]
  60.     b1 = a[0][1]
  61.     b2 = a[1][1]
  62.     b3 = a[2][1]
  63.     c1 = a[0][2]
  64.     c2 = a[1][2]
  65.     c3 = a[2][2]
  66.     return a1*(b2*c3-b3*c2) - b1*(a2*c3-a3*c2) + c1*(a2*b3-a3*b2)
  67.  
  68.  
  69. def t_matrix(a=[]):
  70.     a1 = a[0][0]
  71.     a2 = a[1][0]
  72.     a3 = a[2][0]
  73.     b1 = a[0][1]
  74.     b2 = a[1][1]
  75.     b3 = a[2][1]
  76.     c1 = a[0][2]
  77.     c2 = a[1][2]
  78.     c3 = a[2][2]
  79.  
  80.     b = [[b2*c3-b3*c2, -b1*c3+b3*c1, b1*c2-b2*c1],
  81.          [-a2*c3+a3*c2, a1*c3-a3*c1, -a1*c2+a2*c1],
  82.          [a2*b3-a3*b2, -a1*b3+a3*b1, a1*b2-a2*b1]]
  83.     return b
  84.  
  85.  
  86. def inverse_matrix(a=[]):
  87.     t = t_matrix(a)
  88.     q = def_matrix(a)
  89.     if q == 0:
  90.         print("Определитель матрицы = 0!")
  91.         return a
  92.     return([[t[0][0]/q, t[0][1]/q, t[0][2]/q],
  93.             [t[1][0]/q, t[1][1]/q, t[1][2]/q],
  94.             [t[2][0]/q, t[2][1]/q, t[2][2]/q]])
  95.  
  96.  
  97. def ans(a=[], f=[], x=[]):
  98.     a1 = a[0][0]
  99.     a2 = a[1][0]
  100.     a3 = a[2][0]
  101.     b1 = a[0][1]
  102.     b2 = a[1][1]
  103.     b3 = a[2][1]
  104.     c1 = a[0][2]
  105.     c2 = a[1][2]
  106.     c3 = a[2][2]
  107.     c = [0, 0, 0]
  108.     c[0] = x[0] - (a1*f[0] + b1*f[1] + c1*f[2])
  109.     c[1] = x[1] - (a2*f[0] + b2*f[1] + c2*f[2])
  110.     c[2] = x[2] - (a3*f[0] + b3*f[1] + c3*f[2])
  111.     return c
  112.  
  113.  
  114. def output_matrix(a=[]):
  115.     print(a[0][0], ' ', a[0][1], ' ', a[0][2])
  116.     print(a[1][0], ' ', a[1][1], ' ', a[1][2])
  117.     print(a[2][0], ' ', a[2][1], ' ', a[2][2])
  118.     print('')
  119.  
  120.  
  121. x = 1 #начальное решение
  122. y = 1
  123. z = 1
  124. print('Начальное приближение: ', x, y, z)
  125. print('Функции: ', f1(x, y, z), f2(x, y, z), f3(x, y, z))
  126. print('')
  127. S = 1 #погрешность
  128. e = 0.000001
  129. k = 0
  130. while (S > e) and (k < 100):
  131.     W = w_matrix(x, y, z)
  132.     Inv = inverse_matrix(W)
  133.     answer = ans(Inv, [f1(x,y,z), f2(x,y,z), f3(x,y,z)], [x,y,z])
  134.     S = max(x-answer[0], y-answer[1], z-answer[2])
  135.     x = answer[0]
  136.     y = answer[1]
  137.     z = answer[2]
  138.     k = k + 1
  139.     print('Итерация: ', k)
  140.     print('Ответ: ', x, y, z)
  141.     print('Функции: ', f1(x, y, z), f2(x, y, z), f3(x, y, z))
  142.     print('')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top