Example #1
0
    def test_solve_exercise_2_3(self):
        def f(x):
            return x * math.tan(x) - math.log(x)

        # check that s.split fails when the separator is not a string
        with self.assertRaises(BisectError):
            solve(f, interval=(1.0, 4.0), places=2, logger=test_logger)
Example #2
0
    def test_solve_exercise_2_2(self):
        def f(x):
            return x * math.cos(x) - math.log(x)

        predicted_effort = bisect.predicted_effort(f,
                                                   interval=(1.0, 1.6),
                                                   places=2,
                                                   logger=test_logger)
        ans = bisect.solve(f,
                           interval=(1.0, 1.6),
                           places=2,
                           logger=test_logger)

        self.assertAlmostEqual(ans.solution, 1.35, 2)
        self.assertEqual(predicted_effort, ans.effort)
Example #3
0
    def test_solve_exercise_2_1(self):
        def f(x):
            return x * math.exp(-x) - 0.25

        predicted_effort = bisect.predicted_effort(f,
                                                   interval=(0.3, 0.4),
                                                   places=3,
                                                   logger=test_logger)
        ans = bisect.solve(f,
                           interval=(0.3, 0.4),
                           places=3,
                           logger=test_logger)

        self.assertAlmostEqual(ans.solution, 0.357, 3)
        self.assertEqual(predicted_effort, ans.effort)
Example #4
0
    def test_solve_example_2_1(self):
        def f(x):
            return x**3 - 0.75 * x**2 - 4.5 * x + 4.75

        predicted_effort = bisect.predicted_effort(f,
                                                   interval=(1.5, 2.0),
                                                   places=3,
                                                   logger=test_logger)
        self.assertEqual(predicted_effort, 12)
        ans = bisect.solve(f,
                           interval=(1.5, 2.0),
                           places=3,
                           logger=test_logger)

        self.assertAlmostEqual(ans.solution, 1.779, 3)
        self.assertEqual(predicted_effort, ans.effort)
Example #5
0
            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),
                   places=1,
                   logger=logger)
print("Root found with bisect = ", ans.solution)
"""
n.b. Each N-R iteration is regarded as 2 function evaluations, as f(x) and f'(x) are considered separately
(presumably this means that the method comes up with the derivative f'(x), but unclear how this is done programatically)
"""