Advertisement
awsmpshk

Polyline_method

Dec 11th, 2021
850
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def polyline_method(rng, eps):
  2.     l = lipshic_const(rng, eps)
  3.  
  4.     left = rng[0]
  5.     right = rng[1]
  6.  
  7.     x = (1 / (2 * l)) * (f(left) - f(right) + l * (left + right))
  8.     y = (1 / 2) * (f(left) + f(right) + l * (left - right))
  9.  
  10.     first_delta = (1 / (2 * l)) * (f(x) - y)
  11.     second_delta = 2 * l * first_delta
  12.  
  13.     while second_delta > eps:
  14.         first_x = x - first_delta
  15.         second_x = x + first_delta
  16.  
  17.         if f(first_x) < f(second_x):
  18.             x = first_x
  19.         else:
  20.             x = second_x
  21.  
  22.         f_val = 0.5 * (f(x) + y)
  23.         first_delta = 1 / (2 * l) * (f(x) - y)
  24.         second_delta = 2 * l * first_delta
  25.         y = f_val
  26.    
  27.     return (x, f(x))
  28.  
  29. print(polyline_method([-3, 3], 0.001))
Advertisement
RAW Paste Data Copied
Advertisement