-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbisection.py
47 lines (39 loc) · 1.06 KB
/
bisection.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
39
40
41
42
43
44
45
46
47
import numpy as np
def bi_section(f, a, b):
'''
This is a function that calculates a root for given function "f"
in the interval [a, b]
inputs:
f : the function for which we calculate the root
a : the beginning of the interval
b : the end of interval
'''
# the main while loop to check
# if we are close enough to the desired tolerance
while abs(b-a) > 1e-8:
# we have chosen the tolerance to be 1e-8 but we can consider
# to give it as a parameter to the function
c = (a+b)/2 # derive the mid point
if f(a)*f(c) < 0:
b = c
elif f(b)*f(c) < 0:
a = c
# catch the cases where the end of intervals or mid point
# the exact root
elif f(a) == 0:
return a
elif f(b) == 0:
return b
elif f(c) == 0:
return c
return (a+b)/2
def f(x):
'''
the function for which we are calculating the root
'''
y = np.cos(x)
return y
a = 0
b = np.pi
print(np.pi/2)
print(bi_section(f, a, b))