SHARE
TWEET

Fibonacci method

a guest Oct 18th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def fibonacci_numbers(n):
  2.     return 1 / (5 ** 0.5) * \
  3.            (((1 + 5 ** 0.5) / 2) ** n
  4.             - ((1 - 5 ** 0.5) / 2) ** n)
  5.  
  6.  
  7. def steps(eps, a, b, x, y, n, k):
  8.     if func(x) > func(y):
  9.         a_k1 = x
  10.         b_k1 = b
  11.         x_k1 = y
  12.         y_k1 = a_k1 + fibonacci_numbers(n - k - 1) * (b_k1 - a_k1) / fibonacci_numbers(n - k)
  13.         if k != n - 2:
  14.             k += 1
  15.             return steps(eps, a_k1, b_k1, x_k1, y_k1, n, k + 1)
  16.         else:
  17.             fifth_step = "Не знаю, как сделать 5 шаг"
  18.             return fifth_step
  19.     else:
  20.         a_k1 = a
  21.         b_k1 = y
  22.         y_k1 = x
  23.         x_k1 = a_k1 + fibonacci_numbers(n - k - 2) * (b_k1 - a_k1) / fibonacci_numbers(n - k)
  24.         if k != n - 2:
  25.             return steps(eps, a_k1, b_k1, x_k1, y_k1, n, k + 1)
  26.         else:
  27.             fifth_step = "Не знаю, как сделать 5 шаг"
  28.             return fifth_step
  29.  
  30. def fibonacci(eps, a0, b0):
  31.     e = (b0 - a0) / eps
  32.     n = 1
  33.     while fibonacci_numbers(n) <= e:
  34.         n += 1
  35.     fn = fibonacci_numbers(n)
  36.     x = a0 + fibonacci_numbers(n - 2) / fn * (b0 - a0)
  37.     y = a0 + fibonacci_numbers(n - 1) / fn * (b0 - a0)
  38.     print(steps(eps, a0, b0, x, y, n, 1))
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