-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewton_unittest.py
More file actions
33 lines (26 loc) · 1.19 KB
/
newton_unittest.py
File metadata and controls
33 lines (26 loc) · 1.19 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
"""" Unittest to validate the implementation of Newtons method in newtonraphson.py"""
import unittest
from newtonraphson import NewtonRaphson, derivative, polyval
from scipy import optimize
class TestNewton(unittest.TestCase):
def test_ex1(self):
""" compare the results of scipy vs my method. """
f_possible = [[1, 2, 3, 4], [1, 1, 1, 1], [1, 20.2, 0], [1, 1, -1.01, 2]]
for ii in range(0, len(f_possible)):
function = f_possible[ii]
x0 = 0
f = lambda x: polyval(fpoly=function, x=x)
root1 = optimize.newton(f, x0, tol=1e-05, disp=False)
root2 = NewtonRaphson(fpoly=function, a=x0)
print(abs(root1 - root2))
self.assertTrue(abs(root1 - root2) < 1e-05)
def test_ex2(self):
""" compare the results of scipy vs my method. """
f_possible = [[1, 2, 3, 4], [1, 1, 1, 1], [1, 20.2, 0], [1, 1, -1.01, 2]]
for ii in range(0, len(f_possible)):
function = f_possible[ii]
x0 = complex(1, 1)
root2 = NewtonRaphson(fpoly=function, a=x0)
self.assertTrue(abs(polyval(function, root2)) < 1e-05)
if __name__ == '__main__':
unittest.main()