Exemple #1
0
    def test_bisection_1b(self):
        """
        Unit test for exercise 1.1b.
        """
        logging.info("\nANSWERS TO EXERCISE 1.1B")
        left = 0.5
        right = 3.1

        # The final interval should contain the desired root.
        root, (left, right) = undertest.bisection(self.func, left, right, self.maxit)
        self.assertTrue(_root_in_interval(self.desired_root, left, right))
Exemple #2
0
    def test_bisection_1de(self):
        """
        Unit test for exercise 1.1d and 1.1e.
        """
        logging.info("\nANSWERS TO EXERCISE 1.1D")
        func = lambda x: x**7 - 7 * x**6 + 21 * x**5 - 35 * x**4 + 35 * x**3 - 21 * x**2 + 7 * x - 1
        starting_left = 0.95
        starting_right = 1.01

        # Margaret chose this case as a funky example of bisection. The root should NOT be in
        # the interval for this function.
        root, (left, right) = undertest.bisection(func, starting_left, starting_right, self.maxit)
        desired_root = 1.0
        self.assertFalse(_root_in_interval(desired_root, left, right))

        # Now let's try the factored form of func. Here the interval SHOULD contain the true root.
        logging.info("\nRUNNING EXERCISE 1.1E")
        factored_func = lambda x: (x - 1)**7
        root, (left, right) = undertest.bisection(
            factored_func, starting_left, starting_right, self.maxit)
        self.assertTrue(_root_in_interval(desired_root, left, right))