SHARE
TWEET

Untitled

a guest Nov 19th, 2019 85 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.  
  7. class QuadroInterpolation:
  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.             self.res = self.func(x_hat)
  47.  
  48.     def square_diff(self, x, y):
  49.         return x ** 2 - y ** 2
  50.  
  51.     def interpolation_polynomial(self):
  52.         try:
  53.             return 0.5 * (
  54.                     (
  55.                             self.square_diff(self.x_2, self.x_3)
  56.                             * self.left_border.y + self.square_diff(self.x_3, self.x_1)
  57.                             * self.right_border.y + self.square_diff(self.x_1, self.x_2)
  58.                             * self.center.y
  59.                     )
  60.                     /
  61.                     (
  62.                             (self.x_2 - self.x_3) * self.left_border.y
  63.                             + (self.x_3 - self.x_1) * self.right_border.y
  64.                             + (self.x_1 - self.x_2) * self.center.y
  65.                     )
  66.             )
  67.         except ZeroDivisionError:
  68.             return 0
  69.  
  70.  
  71. def func(x):
  72.     return x**2
  73.  
  74.  
  75. a = QuadroInterpolation(func, 2, 1, 0.003, 0.03)
  76. a.compute()
  77.  
  78. 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. OK, I Understand
 
Top