-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
41 lines (28 loc) · 835 Bytes
/
Copy pathmain.py
File metadata and controls
41 lines (28 loc) · 835 Bytes
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
# User defined function
# Assumptions of the function:
# - Domain: ℝ
def func(x: float) -> float:
return x**2 - 1.7**2
# The main root finding function
def find_roots():
# Start by two points (assume atm that f(a)*f(b) < 0 )
# assume guess_1 < guess_2
guess_1 = 0.0
guess_2 = 3.0
print('negative' if func(guess_1)*func(guess_2) < 0 else 'non-negative')
tolerance = 0.0001
diff = guess_2 - guess_1
while diff > tolerance:
diff = guess_2 - guess_1
# Converge towards the root
mid_point = (guess_1 + guess_2 ) / 2
if (func(guess_1)*func(mid_point) < 0):
guess_2 = mid_point
else:
guess_1 = mid_point
return mid_point
def main():
print(find_roots())
if __name__ == '__main__':
print("Running main")
main()