-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewton’s Method.py
More file actions
55 lines (43 loc) · 1.56 KB
/
Newton’s Method.py
File metadata and controls
55 lines (43 loc) · 1.56 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import math
def f(x):
return math.sin(x) - 0.5 * x
# return math.sqrt(math.cos(x)) - x
# return 3 * x / math.tan(x)
# return math.tan(x) - 2 * x
# return 5 * x**3 + 5 * x**2 + 4
# return math.exp(x) - 5.5 * x
# return math.exp(3 * x) + 5 * x - 2
# return x**3 + x + 2
# return x**2 - 7 * x + 10
def f_prime(x):
return math.cos(x) - 0.5
# return -0.5 * math.sin(x) / math.sqrt(math.cos(x)) - 1
# return (3 * math.tan(x) - 3 * x * (1 / (math.cos(x))**2)) / (math.tan(x))**2
# return 1 / math.cos(x)**2 - 2
# return 15 * x**2 + 10 * x
# return math.exp(x) - 5.5
# return 3 * math.exp(3 * x) + 5
# return 3 * x**2 + 1
# return 2 * x - 7
x0 = -1
def newtons_method(x0, tolerance=1e-6, max_iterations=100):
x_n = x0
for n in range(max_iterations):
f_xn = f(x_n)
f_prime_xn = f_prime(x_n)
if abs(f_prime_xn) < 1e-10:
print("Derivative too small. Stopping iteration.")
return None
x_n1 = x_n - f_xn / f_prime_xn
print(f"Iteration {n+1}: x = {x_n1:.6f}, f(x) = {f(x_n1):.6f}")
# Check if the result is within the tolerance level
if abs(x_n1 - x_n) < tolerance:
return x_n1
x_n = x_n1
print("no convergence upon hitting max number of iterations")
return None
approximate_root = newtons_method(x0)
if approximate_root is not None:
print(f"\nroot is approximately x = {approximate_root:.6f}")
else:
print("\nnewton's method didn't converge")