• API
• FAQ
• Tools
• Archive
SHARE
TWEET

Untitled

a guest Nov 19th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. class FunctionResult:
2.     def __init__(self, y, x):
3.         self.x = x
4.         self.y = y
5.
6.
8.     def __init__(self, func, x_1, delta, eps, eps_1):
9.         self.func = func
10.         self.x_1 = x_1
11.         self.delta = delta
12.         self.eps = eps
13.         self.eps_1 = eps_1
14.
15.     def compute(self):
16.         self.x_2 = self.x_1 + self.delta
17.         self.left_border = FunctionResult(self.func(self.x_1), self.x_1)
18.         self.right_border = FunctionResult(self.func(self.x_2), self.x_2)
19.         self.x_3 = self.x_1 + 2 * self.delta if self.right_border.y < self.left_border.y else self.x_1 - self.delta
20.         self.center = FunctionResult(self.func(self.x_3), self.x_3)
21.         self.compute_2()
22.
23.     def compute_2(self):
24.         temp = sorted((self.left_border, self.right_border, self.center), key=lambda v: v.y)[0]
25.         F_MIN = getattr(temp, 'y')
26.         x_min = temp.x
27.         x_hat = self.interpolation_polynomial()
28.         if x_hat:
29.             print(abs((F_MIN - self.func(x_hat)) / self.func(x_hat)) < self.eps and abs(
30.                 (x_min - x_hat) / x_hat) < self.eps_1)
31.             if abs((F_MIN - self.func(x_hat)) / self.func(x_hat)) < self.eps and abs(
32.                     (x_min - x_hat) / x_hat) < self.eps_1:
33.                 self.res = x_hat
34.                 return
35.             else:
36.                 if self.x_1 <= x_hat <= self.x_3:
37.                     self.x_1, self.x_2, self.x_3 = sorted([self.x_1, self.x_2, self.x_3, x_hat, x_min])[:3]
38.                     self.left_border = FunctionResult(self.func(self.x_1), self.x_1)
39.                     self.right_border = FunctionResult(self.func(self.x_2), self.x_2)
40.                     self.center = FunctionResult(self.func(self.x_3), self.x_3)
41.                     self.compute_2()
42.                 else:
43.                     self.x_1 = x_hat
44.                     self.compute()
45.         else:
46.             return {
47.
48.             }
49.
50.     def square_diff(self, x, y):
51.         return x ** 2 - y ** 2
52.
53.     def interpolation_polynomial(self):
54.         try:
55.             return 0.5 * (
56.                     (
57.                             self.square_diff(self.x_2, self.x_3)
58.                             * self.left_border.y + self.square_diff(self.x_3, self.x_1)
59.                             * self.right_border.y + self.square_diff(self.x_1, self.x_2)
60.                             * self.center.y
61.                     )
62.                     /
63.                     (
64.                             (self.x_2 - self.x_3) * self.left_border.y
65.                             + (self.x_3 - self.x_1) * self.right_border.y
66.                             + (self.x_1 - self.x_2) * self.center.y
67.                     )
68.             )
69.         except ZeroDivisionError:
70.             return 0
71.
72.
73. def func(x):
74.     2 * x ** 2 + 16 / x
75.
76.
77. a = QuadroInterpolation(func, 1, 1, 0.003, 0.03)
78. a.compute()
79.
80. print(a.res)
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.

Top