MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / bisection

Function bisection

arithmetic_analysis/bisection.py:4–26  ·  view source on GitHub ↗
(function, a, b)

Source from the content-addressed store, hash-verified

2
3
4def bisection(function, a, b): # finds where the function becomes 0 in [a,b] using bolzano
5
6 start = a
7 end = b
8 if function(a) == 0: # one of the a or b is a root for the function
9 return a
10 elif function(b) == 0:
11 return b
12 elif function(a) * function(b) > 0: # if none of these are root and they are both positive or negative,
13 # then his algorithm can't find the root
14 print("couldn't find root in [a,b]")
15 return
16 else:
17 mid = (start + end) / 2
18 while abs(start - mid) > 10**-7: # until we achieve precise equals to 10^-7
19 if function(mid) == 0:
20 return mid
21 elif function(mid) * function(start) < 0:
22 end = mid
23 else:
24 start = mid
25 mid = (start + end) / 2
26 return mid
27
28
29def f(x):

Callers 1

bisection.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected