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