mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-01 00:41:09 +00:00
commit
a033150426
|
@ -9,13 +9,13 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us
|
||||||
return a
|
return a
|
||||||
elif function(b) == 0:
|
elif function(b) == 0:
|
||||||
return b
|
return b
|
||||||
elif function(a) * function(b) > 0: # if noone of these are root and they are both possitive or negative,
|
elif function(a) * function(b) > 0: # if none of these are root and they are both positive or negative,
|
||||||
# then his algorith can't find the root
|
# then his algorithm can't find the root
|
||||||
print("couldn't find root in [a,b]")
|
print("couldn't find root in [a,b]")
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
mid = (start + end) / 2
|
mid = (start + end) / 2
|
||||||
while abs(start - mid) > 0.0000001: # untill we achive percise equals to 10^-7
|
while abs(start - mid) > 0.0000001: # until we achieve precise equals to 10^-7
|
||||||
if function(mid) == 0:
|
if function(mid) == 0:
|
||||||
return mid
|
return mid
|
||||||
elif function(mid) * function(start) < 0:
|
elif function(mid) * function(start) < 0:
|
||||||
|
@ -27,7 +27,7 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us
|
||||||
|
|
||||||
|
|
||||||
def f(x):
|
def f(x):
|
||||||
return math.pow(x, 3) - 2*x -5
|
return math.pow(x, 3) - 2*x - 5
|
||||||
|
|
||||||
|
|
||||||
print(bisection(f, 1, 1000))
|
print(bisection(f, 1, 1000))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user