Advertisement
Guest User

pytongiinterpolacje

a guest
Jan 23rd, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. import math
  2.  
  3.  
  4. def f(x):
  5.     return x + (1 / x**2)
  6.  
  7.  
  8. def interp_parabolic(_a, _b, eps):
  9.     a = _a
  10.     b = _b
  11.     c = a + ((b - a) * 0.5)
  12.     d_top = 0.0
  13.     d_bottom = 0.0
  14.     d = 0.0
  15.     l = b - a
  16.  
  17.     while True:
  18.         if l < eps:
  19.             print('a:', a)
  20.             print('b:', b)
  21.             print('c:', c)
  22.             print('d:', d)
  23.             return True
  24.         else:
  25.             f_c = f(c)
  26.             f_b = f(b)
  27.             f_a = f(a)
  28.  
  29.             a_2 = a**2
  30.             b_2 = b**2
  31.             c_2 = c**2
  32.  
  33.             d_top = (f_a * (c_2 - b_2)) + \
  34.                 (f_c * (b_2 - a_2)) + (f_b * (a_2 - c_2))
  35.             d_bottom = (f_a * (c - b)) + (f_c * (b - a)) + (f_b * (a - c))
  36.             d = 0.5 * (d_top / d_bottom)
  37.  
  38.             f_d = f(d)
  39.  
  40.             if d < c:
  41.  
  42.                 if f_d < f_c:
  43.                     b = c
  44.                     c = d
  45.                 else:
  46.                     a = d
  47.  
  48.             else:
  49.                 if f_d < f_c:
  50.                     a = c
  51.                     c = d
  52.                 else:
  53.                     b = d
  54.  
  55.         l = b - a
  56.  
  57.  
  58. interp_parabolic(1.0, 2.0, 0.0001)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement