mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 09:21:13 +08:00
psf/black code formatting (#1277)
This commit is contained in:

committed by
Christian Clauss

parent
07f04a2e55
commit
9eac17a408
@ -1,7 +1,9 @@
|
||||
import math
|
||||
|
||||
|
||||
def bisection(function, a, b): # finds where the function becomes 0 in [a,b] using bolzano
|
||||
def bisection(
|
||||
function, a, b
|
||||
): # finds where the function becomes 0 in [a,b] using bolzano
|
||||
|
||||
start = a
|
||||
end = b
|
||||
@ -9,13 +11,15 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us
|
||||
return a
|
||||
elif function(b) == 0:
|
||||
return b
|
||||
elif function(a) * function(b) > 0: # if none of these are root and they are both positive or negative,
|
||||
elif (
|
||||
function(a) * function(b) > 0
|
||||
): # if none of these are root and they are both positive or negative,
|
||||
# then his algorithm can't find the root
|
||||
print("couldn't find root in [a,b]")
|
||||
return
|
||||
else:
|
||||
mid = start + (end - start) / 2.0
|
||||
while abs(start - mid) > 10**-7: # until we achieve precise equals to 10^-7
|
||||
while abs(start - mid) > 10 ** -7: # until we achieve precise equals to 10^-7
|
||||
if function(mid) == 0:
|
||||
return mid
|
||||
elif function(mid) * function(start) < 0:
|
||||
@ -27,7 +31,8 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us
|
||||
|
||||
|
||||
def f(x):
|
||||
return math.pow(x, 3) - 2*x - 5
|
||||
return math.pow(x, 3) - 2 * x - 5
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(bisection(f, 1, 1000))
|
||||
|
Reference in New Issue
Block a user