Skip to content

Commit e19e0d2

Browse files
committed
Newton's Method
1 parent 3af2e92 commit e19e0d2

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

jmath/approximation/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33
44
Tools for approximating mathematical equations.
55
6-
Optional Packages
6+
Default Packages
77
-----------------
88
99
jmath.approximation.euler_method
10-
The euler method for approximating the integrals of differential equations.
10+
The Euler Method for approximating the integrals of differential equations.
11+
jmath.approximation.newton_method
12+
The Newton Method for approximating the roots of an equation.
1113
'''
1214

1315
# - Namespace
1416

1517
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
1618

17-
# No Defaults
19+
# - Defaults
20+
21+
from .euler_method import euler_step, euler_step_interval, iterate_euler_step
22+
from .newton_method import newton_method

jmath/approximation/newton_method.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Newton's method for approximating the roots of equations.
3+
"""
4+
5+
# - Imports
6+
7+
from ..uncertainties import Uncertainty
8+
from typing import Callable
9+
from warnings import warn
10+
11+
# - Functions
12+
13+
def newton_method(f: Callable[[float], float], f_prime: Callable[[float], float], x_0: float, threshold: float = 1e-6, max_iter: int = 100) -> float:
14+
"""
15+
Newton's Method for approximation of the root of an equation.
16+
17+
Parameters
18+
----------
19+
20+
f
21+
The function to determine the root of.
22+
f_prime
23+
The derivative of the function to determine the root of.
24+
x_0
25+
The starting point for determining the root from.
26+
threshold (optional)
27+
The threshold error to approximate the root to.
28+
max_iter (optional)
29+
The maximum iterations to apply.
30+
"""
31+
32+
# Instantiate x_n from x_0
33+
x_n = x_0
34+
# Create a placeholder new x such that the threshold is not triggered
35+
x_new = x_n + 2*threshold
36+
# Iterator counter
37+
i = 0
38+
39+
# While the distance between points is greater than the threshold
40+
while abs(f(x_new) - f(x_n)) >= threshold:
41+
42+
x_new = x_n
43+
# Compute new x_n
44+
x_n = x_n - f(x_n)/f_prime(x_n)
45+
46+
# If iterator is too high
47+
if i >= max_iter:
48+
# Warn user
49+
warn(f"Newton Method reached max iterations of {max_iter}.")
50+
# Return value of x_n
51+
return(Uncertainty(x_n, threshold))
52+
53+
# Increase iterator
54+
i += 1
55+
56+
# Error under threshold
57+
# Return value
58+
return(Uncertainty(x_n, threshold))

tests/test_newton_method.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Tests that Newton's Method behaves as expected.
3+
"""
4+
5+
# - Imports
6+
7+
from ..jmath.approximation.newton_method import *
8+
9+
# - Tests
10+
11+
def test_root_2():
12+
"""Tests that root 2 comes from the Newton Method as expected."""
13+
f = lambda x : x**2 - 2
14+
f_prime = lambda x : 2*x
15+
assert round(newton_method(f, f_prime, 1).value, 6) == 1.414214

0 commit comments

Comments
 (0)