Skip to content

Commit f8a30b4

Browse files
dimgrichrcclauss
authored andcommitted
Addition of Secant Method (#876)
* Add files via upload * Update secant_method.py * Remove unused import * Remove unused import
1 parent 3ada8bb commit f8a30b4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

arithmetic_analysis/secant_method.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Implementing Secant method in Python
2+
# Author: dimgrichr
3+
4+
5+
from math import exp
6+
7+
8+
def f(x):
9+
"""
10+
>>> f(5)
11+
39.98652410600183
12+
"""
13+
return 8 * x - 2 * exp(-x)
14+
15+
16+
def SecantMethod(lower_bound, upper_bound, repeats):
17+
"""
18+
>>> SecantMethod(1, 3, 2)
19+
0.2139409276214589
20+
"""
21+
x0 = lower_bound
22+
x1 = upper_bound
23+
for i in range(0, repeats):
24+
x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0))
25+
return x1
26+
27+
28+
print(f"The solution is: {SecantMethod(1, 3, 2)}")

0 commit comments

Comments
 (0)