-
Notifications
You must be signed in to change notification settings - Fork 0
/
5_darboux.py
38 lines (25 loc) · 911 Bytes
/
5_darboux.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import numpy as np
def bisection(function, a, b, precision):
if (function(a)*function(b) >= 0):
raise ValueError("Wrong values of function in extremal points.")
new_a = a
new_b = b
mid = (new_a + new_b)/2.
while(abs(function(mid)) >= precision):
mid = (new_a + new_b)/2.
midFval = function(mid)
if function(new_a)*midFval < 0:
new_b = mid
elif function(new_b)*midFval < 0:
new_a = mid
elif midFval == 0:
return mid
else :
return None
return (new_a + new_b)/2.
if __name__ == "__main__":
precision = float(input("Provide the precision: "))
function = lambda x: np.cos(pow(x, 3) - 2*x)
#function = lambda x: x**2 - x - 1
#function = lambda x: np.log1p(pow(x, 2) + 2*x)
print(bisection(function, 0, 2, precision))