def testTrig(self): f = lambda x: N.sin(x) g = lambda x: N.cos(x) solver = newton.Newton(f, tol=1.e-15, maxiter=10) x = solver.solve(1.0) solver = newton.Newton(g, tol=1.e-15, maxiter=10) y = solver.solve(1.0) self.assertAlmostEqual(x, 0.0) self.assertAlmostEqual(y, N.pi / 2)
def testNotActualRoot1(self): # no roots at all f = lambda x: np.exp(x) solver = newton.Newton(f, tol=1.e-15, maxiter=10, max_radius=10) self.assertRaises(Exception, solver, 1.0) # bad x0, small maxiter f = F.Polynomial([-15, 23, -9, 1]) solver = newton.Newton(f, tol=1.e-15, maxiter=10, max_radius=10) self.assertRaises(Exception, solver, 100)
def testMaxRadius(self): s=F.Sinusoid(1., 3., N.pi/2.) #cosine, roots at +-pi/2 r=N.pi/8. guess=N.pi/4. solver = newton.Newton(s, tol=1.e-15, maxiter=50, Df=s.Jacobian) x=solver.solve(guess) self.assertAlmostEqual(x, N.pi/2) #check that it reaches within maxiter if no radius imposed solver = newton.Newton(s, tol=1.e-15, maxiter=50, Df=s.Jacobian, maxrad=r) #included max radius self.assertRaises(RuntimeError, solver.solve, guess) #should not reach solution within maxrad
def testQuadraticAnalyticJac(self): p=F.Polynomial([1,-1,-6]) #x^2-x+6 has roots 3,-2 solver = newton.Newton(p, tol=1.e-15, maxiter=20, Df=p.Jacobian) x1=solver.solve(3.5) x2=solver.solve(-1.5) self.assertEqual(x1, 3.0) self.assertEqual(x2, -2.0)
def test_approximate_2d(self): f = lambda x: np.matrix([[2 * x[0, 0] - 2 * x[1, 0] - 2], [x[0, 0] + 2 * x[1, 0] - 7]]) solver = newton.Newton(f, tol=1.e-10) x0 = np.matrix([[1.], [1.]]) x = solver.solve(x0) np.testing.assert_array_almost_equal(x, np.matrix([[3], [2]]))
def testMaxiterTooSmall(self): # tests whether Newton handles unacceptable maxiter: # I updated Maxiter such that if maxiter<1 # maxiter is set to 2 f = lambda x: newton.F.MyGaussian(x) + 10 # is always >1 solver = newton.Newton(f, tol=1.e-15, maxiter=-1, r_conv=1) self.assertEqual(solver._maxiter, 1)
def testTwoParamsApprox(self): f = lambda x: N.matrix([[2 * x[0, 0] - 6 * x[1, 0]], [x[0, 0] + x[1, 0] - 8]]) solver = newton.Newton(f, tol=1.e-6, maxiter=100) x0 = N.matrix([[7.], [0.]]) x = solver.solve(x0) N.testing.assert_array_almost_equal(x, N.matrix([[6.], [2.]]))
def testQuad2(self): #P=x**2+2*x+6 f = F.Polynomial([1, 2, 6]) _Df = F.Polynomial([0, 2, 2]) solver = newton.Newton(f, tol=1.e-15, maxiter=100, Df=_Df) x = solver.solve(0) self.assertAlmostEqual(x, -1)
def testQuad(self): #P=(x-2)**2=x*2-4x+4 f = F.Polynomial([1, -4, 4]) _Df = F.Polynomial([0, 2, -4]) solver = newton.Newton(f, tol=1.e-15, maxiter=100, Df=_Df) x = solver.solve(10) self.assertAlmostEqual(x, 2)
def testStep(self): f = lambda x: np.cos(x) solver = newton.Newton(f, tol=1.e-15, maxiter=1) x0 = 3 x1 = solver.step(x0) x2 = solver.step(x0, f(x0)) self.assertEqual(x1, x2)
def test2D(self): f = lambda x: np.matrix([[x[0, 0] - x[1, 0] + 7], [x[0, 0] + x[1, 0] - 15]]) solver = newton.Newton(f, tol=1.e-6, maxiter=30) x0 = np.matrix([[3], [2]]) x = solver.solve(x0) np.testing.assert_array_almost_equal(x, np.matrix([[4.], [11.]]))
def testQuadratic2(self): # y : x*x + 6*x + 9 f = F.Polynomial([9, 6, 1]) _Df = F.Polynomial([6, 2]) solver = newton.Newton(f, tol=1.e-15, maxiter=200, Df=_Df) x = solver.solve(-2.0) self.assertAlmostEqual(x, -3.0)
def testPolyLogAnalVsNum(self): # function (x-2)(x-4)log^2(x) has zeros at x=1,2,4 x0 = 1.7 p = F.PolyLog([1, -6, 8], 2) dp = p.Df solverAnal = newton.Newton(p, tol=1.e-15, Df=dp) x1Anal = solverAnal.step(x0) xSolAnal = solverAnal.solve(x0) solverNum = newton.Newton(p, tol=1.e-15, dx=1.e-8) x1Num = solverNum.step(x0) xSolNum = solverNum.solve(x0) self.assertNotEqual(x1Anal, x1Num) self.assertEqual(xSolAnal, xSolNum)
def testQuadratic2DAnalyticJac(self): q=F.BivariateQuadratic2D([2,0,0,0,0,0],[0,2,0,0,0,0]) #x^2+y^2 guess=N.matrix("0.1;-0.1") solver = newton.Newton(q, tol=1.e-13, maxiter=50, Df=q.Jacobian) x=solver.solve(guess) self.assertEqual(x.shape, (2,1)) N.testing.assert_array_almost_equal(x, N.matrix("0;0"))
def testCubicAnalyticJac(self): p = F.Polynomial([1,0,1,0]) #root is x=0 solver = newton.Newton(p, tol=1.e-15, maxiter=50, Df=p.Jacobian) x1=solver.solve(0.1) x2=solver.solve(-0.1) self.assertAlmostEqual(x1, 0.) self.assertAlmostEqual(x2, 0.)
def testPolynomial(self): # Tests third degree polynomial: y = x(x+10)(x-10) # for which the expected roots are x = -10, 0, 10 # starting from within one of three intervals. Each # interval corresponds to initial x values that lie # closest to one of the roots. f = lambda x: x * (x + 10) * (x - 10) solver = newton.Newton(f, tol=1.e-8, maxiter=100) # Checks initial conditions close to root at x = -10 root1_init_cond = np.linspace(-11.0, -9.0, 10) for init in root1_init_cond: x1 = solver.solve(init) self.assertAlmostEqual(x1, -10.0) # Checks initial conditions close to root at x = 0 root2_init_cond = np.linspace(-1.0, 1.0, 10) for init in root2_init_cond: x2 = solver.solve(init) self.assertAlmostEqual(x2, 0.0) # Checks initial conditions close to root at x = 10 root3_init_cond = np.linspace(9.0, 11.0, 10) for init in root3_init_cond: x3 = solver.solve(init) self.assertAlmostEqual(x3, 10.0)
def testPoly(self): f = F.Polynomial([1,3,2]) solver = newton.Newton(f, tol=1.e-15, maxiter=20) x = solver.solve(-3.0) self.assertAlmostEqual(x,-2.0) x = solver.solve(0.0) self.assertAlmostEqual(x,-1.0)
def test2dimAnalyt(self): f = lambda x: N.matrix([[5*x[0,0]-2*x[1,0]-13],[2*x[0,0]+x[1,0]-7]]) _Df = lambda x: N.matrix([[5,-2],[2, 1]]) solver = newton.Newton(f, tol=1.e-8, maxiter=20, Df=_Df) x0 = N.matrix([[4],[2]]) x = solver.solve(x0) N.testing.assert_array_almost_equal(x, N.matrix([[3.],[1.]]))
def test_poor_initial_guess(self): import math f = lambda x : x*math.exp(x) solver = newton.Newton(f, tol=1.e-15, maxiter=3000) x = solver.solve(-50) self.assertAlmostEqual(x, 0) return
def test_guessIsSolution(self): #if the intial guess, x0, is the root, don't continue #probably unecessary but doesn't hurt f = lambda x: 3.0 * x + 6.0 solver = newton.Newton(f, tol=1.e-15, maxiter=1) x = solver.solve(-2.0) #-2.0 is the root self.assertEqual(x, -2.0)
def test_2D_analytical_jacobian(self): #lambda setup does not allow for this type of manipulation, so using the #old school way of setting up the function like demonstrated above def k(x) : k = np.matrix("1.0 -4.0; 1.0 1.0") k[0,0] *= x[0]*x[0] k[0,1] *= x[1] k[1,0] *= x[0]*x[0] k[1,1] *= x[1]*x[1] k1 = np.sum(k[0,:]) #need to sum the rows to get vector valued func. k2 = np.sum(k[1,:]) k = np.matrix([[k1],[k2]]) return k #this sets up the derivative analytically instead of using the numerical #solver def Dk(x): Dk = np.matrix("2.0 -4.0; 2.0 2.0") Dk[0,0] *= x[0] Dk[1,0] *= x[0] Dk[1,1] *= x[1] return Dk solver = newton.Newton(k, tol=1.e-15, maxiter=1000, Df=Dk) x = solver.solve(np.matrix("1.0; 4.0")) np.testing.assert_almost_equal(x, np.matrix("0.0; 0.0")) return
def test_analytical_jacobian(self): f = lambda x : (x-5)**2 Df = lambda x : 2*(x-5) solver = newton.Newton(f, tol=1.e-15, maxiter=30, Df=Df) x = solver.solve(0) self.assertAlmostEqual(x,5.0) return
def testSinusoidalAnalyticJac(self): s=F.Sinusoid(2., 5., 0.) solver = newton.Newton(s, tol=1.e-15, maxiter=50, Df=s.Jacobian) x1=solver.solve(0.1) x2=solver.solve(-0.1) self.assertAlmostEqual(x1, 0.) self.assertAlmostEqual(x2, 0.)
def testRootlessFunction(self): # Tests the function y = x^2 + 1, which has no real roots. # Should raise exception when roots aren't located within # desired threshold. f = lambda x: x**2 + 1.0 solver = newton.Newton(f, tol=1.e-8, maxiter=10, max_radius=1000.0) x0 = 10.0 self.assertRaises(RuntimeError, solver.solve, x0)
def testAnalyticJacobiPoly(self): f = F.Polynomial([1,3,2]) _Df = lambda x: 2*x+3 solver = newton.Newton(f, tol=1.e-15, maxiter=25, Df=_Df) x = solver.solve(-0.5) self.assertAlmostEqual(x,-1.0) x = solver.solve(-3.0) self.assertAlmostEqual(x,-2.0)
def test_there_are_no_roots(self): f = lambda x : x**2 + 13 #f = lambda x : x*x + 1.0 solver = newton.Newton(f, tol=1.e-15, maxiter=10) #it should be exactly equal if the root is provided #i = np.sqrt(-1) self.assertRaises(Exception,solver.solve, -1) return
def test_zero_deriv(self): import math f = lambda x : -x**2 + 3 Df = lambda x : -2*x solver = newton.Newton(f, tol=1.e-15, maxiter = 30, Df=Df) x = solver.solve(0) self.assertAlmostEqual(x, -math.sqrt(3)) return
def testGeneralFunctionMultiD_2(self): # test multidimensional array with another non-linear function # I use my custom designed function MyNonLinear2D in functions.py solver = newton.Newton(newton.F.MyNonLinear2D_1, tol=1.e-15, maxiter=20) x = solver.solve(N.matrix(".1; 1.2")) N.testing.assert_array_almost_equal(x, N.matrix("0; 1"))
def test_NewtonStep(self): f = F.Polynomial([9, 6, 1]) solver = newton.Newton(f, tol=1.e-10, maxiter=1) x0 = 0 x = solver.solve(x0) self.assertAlmostEqual(x, -1.5, places=6) trueroot = -3 self.assertTrue(abs(trueroot - x) < abs(trueroot - x0))
def test2dimApprox(self): # A = N.matrix("1. 2.; 3. 4.") # def f(x): # return A * x f = lambda x: N.matrix([[5*x[0,0]-2*x[1,0]-13],[2*x[0,0]+x[1,0]-7]]) solver = newton.Newton(f, tol=1.e-8, maxiter=20) x0 = N.matrix([[4],[2]]) x = solver.solve(x0) N.testing.assert_array_almost_equal(x, N.matrix([[3.],[1.]]))