forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecant_method.py
More file actions
Latest commit
29 lines (23 loc) · 577 Bytes
/
Copy pathsecant_method.py
File metadata and controls
29 lines (23 loc) · 577 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
"""
Implementing Secant method in Python
Author: dimgrichr
"""
frommathimportexp
deff(x: float) ->float:
"""
>>> f(5)
39.98652410600183
"""
return8*x-2*exp(-x)
defsecant_method(lower_bound: float, upper_bound: float, repeats: int) ->float:
"""
>>> secant_method(1, 3, 2)
0.2139409276214589
"""
x0=lower_bound
x1=upper_bound
for_inrange(0, repeats):
x0, x1=x1, x1- (f(x1) * (x1-x0)) / (f(x1) -f(x0))
returnx1
if__name__=="__main__":
print(f"Example: {secant_method(1, 3, 2)}")