Created
January 1, 2021 12:11
-
-
Save officialcjunior/6e6883b3c224ad8589261f011473ff82 to your computer and use it in GitHub Desktop.
Python script to minimize unimodal expressions using Fibonacci method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def fibu(n): | |
if n <= 1: | |
return n | |
else: | |
return(fibu(n-1) + fibu(n-2)) | |
#for i in range (10): | |
# print (i-1, ",", fibu(i)) | |
def L(k): | |
''' | |
print (7-k+1) | |
print (fibu(7 - k + 2 )) | |
print ((fibu( 7 + 2) )) | |
print (l) | |
print (n) ''' | |
res = ( fibu(n - k + 1 + 1) / fibu( n + 2) ) * l | |
return res | |
def fibmethod (f, a, b, k, count): | |
count += 1 | |
l_res = L(k) | |
k = k + 1 | |
x1 = a + l_res | |
x2 = b - l_res | |
print ("n =", count) | |
print ("x1 =", x1, "x2 =", x2, "\nL =", l_res) | |
fx1 = f(x1) | |
fx2 = f(x2) | |
print ("fx1 =", fx1, "\nfx2 = ", fx2) | |
if (count == 6): | |
return 0 | |
if (fx1 < fx2): | |
print ("new interval = [", a, ",", x2, "]\n") | |
return fibmethod (f, a, x2, k, count) | |
else: | |
print ("new interval = [", x1, ",", b, "]\n") | |
return fibmethod (f, x1, b, k, count) | |
# functino goes here | |
f = lambda x: | |
# interval | |
a = -5 | |
b = 0 | |
l = b - a | |
k = 2 | |
n = 6 | |
count=0 | |
print (fibmethod (f, a, b, k, count)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment