-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbisection.py
More file actions
36 lines (28 loc) · 787 Bytes
/
bisection.py
File metadata and controls
36 lines (28 loc) · 787 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
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
from math import exp, sqrt
def f(x):
return exp(x - sqrt(x)) - x
def bisection(f,x0,x1,kmax=200,tol=1.e-8):
f0 = f(x0)
for k in range(1,kmax):
x2 = (x0+x1)/2
f2 = f(x2)
if f0*f2 < 0:
x1 = x2
else:
x0, f0 = x2, f2
x2new = (x0+x1)/2
xdiff = abs(x2new-x2)
rowf = "{0:2d} {1:1.16f} {2:1.16f} {3:1.16f}"
print(rowf.format(k,x2new,xdiff,abs(f(x2new))))
if abs(xdiff/x2new) < tol:
break
else:
x2new = None
return x2new
if __name__ == '__main__':
root = bisection(f,0.,1.5)
print(root); print("")
root = bisection(f,1.5,3.)
print(root)