def test_solve_ex_2_10(self): # Attempt to solve sqrt(2) def g(x): return x ** 2 + x - 2 # cannot be used for solving sqrt(2) as it does not converge s = iterate.solve(g, estimate=1.0, iterations=5, logger=logger) self.assertNotAlmostEqual(s, 1.414, 3)
def test_solve_example_2_2(self): def g(x): return 0.25 * math.exp(x) solution = iterate.solve(g, estimate=0.35, iterations=4, logger=test_logger) self.assertAlmostEqual(solution, 0.357, 3)
def test_solve_ex_2_11(self): """ Two rearrangements of x^3 + 4x^2 - 10 = 0 """ def f_a(x): return x - x ** 3 - 4 * x ** 2 + 10 def f_b(x): inner = 10 / x - 4 * x logger.info("Performing sqrt({})".format(inner)) return math.sqrt(inner) logger.info('-' * 40) # f_a(x) cannot be used to solve x^3 + 4x^2 - 10 = 0 as it diverges and oscillates. iterate.solve(f_a, estimate=1.5, iterations=5, logger=logger) logger.info('-' * 40) with self.assertRaises(ValueError): # f_b(x) cannot be used to solve x^3 + 4x^2 - 10 = 0 as the 3rd iteration attempts to root a -ve number. iterate.solve(f_b, estimate=1.5, iterations=5, logger=logger) logger.info('-' * 40)
def test_solve_root_2(self): # From section 1.1 def g(x): return (x + 2 / x) / 2 # TODO: could parameterise to accept a stopping criterion (the examples don't use fixed # iterations) solution = iterate.solve(g, estimate=1.0, iterations=6, logger=test_logger) self.assertAlmostEqual(solution, 1.414, 3)
def solve(f, *, estimate, iterations=5, logger=None, counter=0): def df(argx): # Use sympy to do the differentiation x = sym.symbols('x') return sym.diff(f(x), x).evalf(subs={x: argx}) def g(x): return x - f(x) / df(x) return iterate.solve(g, estimate=estimate, iterations=iterations, logger=logger, counter=counter)
def test_solve_ex_2_9_ii(self): def f_ii(x): return math.sqrt(5 * x - 2) logger.info("-" * 10, "ii with x0 = 0.5", "-" * 20) # Might be converging to root ~ 4.56 iterate.solve(f_ii, estimate=0.5, iterations=5, logger=logger) logger.info("-" * 10, "ii with x0 = 4.5", "-" * 20) # Appears to be slowly converging to root ~ 4.56 iterate.solve(f_ii, estimate=4.5, iterations=5, logger=logger) logger.info("-" * 10, "ii with x0 = 10", "-" * 20) # Appears to be converging towards root ~ 4.56 iterate.solve(f_ii, estimate=10, iterations=5, logger=logger) logger.info("-" * 40)
def test_solve_ex_2_9_i(self): def f_i(x): return 0.2 * x ** 2 + 0.4 logger.info("-" * 10, "i with x0 = 0.5", "-" * 20) # Seems to be converging s = iterate.solve(f_i, estimate=0.5, iterations=5, logger=logger) self.assertAlmostEqual(s, 0.438, 3) logger.info("-" * 10, "i with x0 = 4.5", "-" * 20) # Unclear if converging s = iterate.solve(f_i, estimate=4.5, iterations=5, logger=logger) self.assertAlmostEqual(s, 3.493, 3) logger.info("-" * 10, "i with x0 = 10", "-" * 20) # Diverges iterate.solve(f_i, estimate=10, iterations=5, logger=logger) logger.info("-" * 10, "i with x0 = 4.5", "-" * 20) # Seems to be slowly converging to 0.438 iterate.solve(f_i, estimate=4.5, iterations=15, logger=logger)
""" b) ii) f = x^3 - 5x + 4 f' = 3x^2 - 5 """ def f(x): return x**3 - 5 * x + 4 def df(x): return 3 * x**2 - 5 return x - f(x) / df(x) return g, h iterate.solve(f, estimate=1.0, iterations=5, logger=logger) print('-' * 40) iterate.solve(g, estimate=0.35, iterations=5, logger=logger) print('-' * 40) iterate.solve(h, estimate=1.5, iterations=5, logger=logger) print('-' * 40) print("Exercise 2.14") funcs = make_functions() iterate.solve(funcs[0], estimate=1.3, iterations=5, logger=logger) print('-' * 40) iterate.solve(funcs[1], estimate=1.3, iterations=5, logger=logger) # Ex 2.14 c) ans = bisect.solve(lambda x: x**3 - 5 * x + 4, interval=(1.1, 1.9),