# lab2(CM) ver 1.0

Nov 16th, 2019
107
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 - 5.5 + 4.2
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.     return (a[0][0]*a[1][1]*a[2][2] + a[2][0]*a[0][1]*a[1][2] +
58.             a[1][0]*a[2][1]*a[0][2] - a[2][0]*a[1][1]*a[0][2] -
59.             a[0][0]*a[2][1]*a[1][2] - a[1][0]*a[0][1]*a[2][2])
60.
61.
62. def t_matrix(a=[]):
63.     a1 = a[0][0]
64.     a2 = a[1][0]
65.     a3 = a[2][0]
66.     b1 = a[0][1]
67.     b2 = a[1][1]
68.     b3 = a[2][1]
69.     c1 = a[0][2]
70.     c2 = a[1][2]
71.     c3 = a[2][2]
72.     B = [[a1*b2-a2*b1, -a2*c3+a3*c2, a2*b3-a3*b2],
73.          [-b1*c3+b3*c1, a1*c3-a3*c1, -a1*b3+a3*b1],
74.          [b1*c2-b2*c1, -a1*c2+a2*c1, a1*b2-a2*b1]]
75.     return B
76.
77.
78. def inverse_matrix(a=[]):
79.     T = t_matrix(a)
80.     q = def_matrix(a)
81.     if def_matrix(a) == 0:
82.         print("def_matrix = 0")
83.         return a
84.     for i in range(3):
85.         for k in range(3):
86.             a[i][k] = T[i][k]/q
87.     return a
88.
89.
90. def output_matrix(a=[]):
91.     print(a[0][0], ' ', a[0][1], ' ', a[0][2])
92.     print(a[1][0], ' ', a[1][1], ' ', a[1][2])
93.     print(a[2][0], ' ', a[2][1], ' ', a[2][2])
94.     print('')
95.
96.
97. x = -2.8 #начальное решение
98. y = -0.2
99. z = 2.6
100. print(f1(x, y, z))
101. print(f2(x, y, z))
102. print(f3(x, y, z))
103. print('')
104.  #[номер строки][номер столбца]
105. #W = [[2,1,1],[1,2,1],[1,1,2]]
106.
107. for i in range(10):
108.     W = w_matrix(x, y, z)
109.     I = inverse_matrix(W)
110.     x2 = x - (I[0][0]*f1(x,y,z) + I[0][1]*f2(x,y,z) + I[0][2]*f3(x,y,z))
111.     y2 = y - (I[1][0]*f1(x,y,z) + I[1][1]*f2(x,y,z) + I[1][2]*f3(x,y,z))
112.     z2 = z - (I[2][0]*f1(x,y,z) + I[2][1]*f2(x,y,z) + I[2][2]*f3(x,y,z))
113.
114.     x=x2
115.     y=y2
116.     z=z2
117.     print(f1(x, y, z))
118.     print(f2(x, y, z))
119.     print(f3(x, y, z))
120.     print('ответ: ', x, y, z)
121.     print('')
RAW Paste Data