-
Notifications
You must be signed in to change notification settings - Fork 0
/
newton.py
27 lines (23 loc) · 850 Bytes
/
newton.py
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
from equations import *
def newton_method(f, f_prima, x, nmax, epsilon, delta):
fx = f(x)
print(f"x = {x}, f(x) = {fx}")
for n in range(1, nmax + 1):
fp = f_prima(x)
if abs(fp) < delta:
print("Small derivative")
return None
d = fx / fp
x = x - d
fx = f(x)
print(f"n = {n}, x = {x}, f(x) = {fx}")
if abs(d) < epsilon:
print(f"Convergence achieved after {n} iterations")
return None
'''
Newton's method theorem
If f is a function with a continuous second derivative and f'(x) != 0, then, for any initial
guess x_0 close enough to the root, the sequence {x_n} generated by Newton's method will
converge to the root quadratically, i.e., the error at each step is approximately the square
of the error at the previous step.
'''