Binary search root finding algorithm.
        public void ConstructorTest()
        {
            Func<int, double> function = x => x * x * x + 2 * x * x - 10 * x + 1;
            BinarySearch search = new BinarySearch(function, -4, 3);
            double root = search.FindRoot();

            Assert.AreEqual(0, root);
        }
        public void ConstructorTest4()
        {
            // (x+5)^3 + 2(x+5)^2 - 10(x+5)
            Func<int, double> function = x =>
            {
                int y = (x + 5);
                return y * y * y + 2 * y * y - 10 * y;
            };

            BinarySearch search = new BinarySearch(function, -6, -4);
            double root = search.FindRoot();
            Assert.AreEqual(-5, root);
        }
Example #3
0
        public void ConstructorTest()
        {
            // https://www.wolframalpha.com/input/?i=%28x%2B1%29+*+%28x%2B1%29+*+%28x%2B1%29+%2B+2+*+%28x%2B1%29+*+%28x%2B1%29+%3D+0%2C+x+is+integer

            Func<int, double> function = x => (x + 1) * (x + 1) * (x + 1) + 2 * (x + 1) * (x + 1);

            // Possible roots are -3 or -1

            BinarySearch search;
            search = new BinarySearch(function, -2, 3);
            double r1 = search.FindRoot();
            Assert.AreEqual(-1, r1);

            search = new BinarySearch(function, -10, -2);
            double r2 = search.FindRoot();
            Assert.AreEqual(-3, r2);
        }
Example #4
0
        public void ConstructorTest1()
        {
            Func<int, double> function = x => elements[x];
            BinarySearch search = new BinarySearch(function, 0, elements.Length);

            int a1 = search.FindRoot();
            Assert.AreEqual(1, a1);

            for (int i = 0; i < elements.Length; i++)
            {
                int a2 = search.Find(elements[i]);
                Assert.AreEqual(i, a2);
            }
        }
Example #5
0
        public void ConstructorTest2()
        {
            Func<int, double> function = x => elements[x];
            BinarySearch search = new BinarySearch(function, 0, elements.Length - 1);

            int a1 = search.Find(5);
            int a2 = search.Find(6);

            int a3 = search.Find(elements.Max() + 1);
            int a4 = search.Find(elements.Max() - 1);
            int a5 = search.Find(elements.Max());

            int a6 = search.Find(elements.Min() + 1);
            int a7 = search.Find(elements.Min() - 1);
            int a8 = search.Find(elements.Min());

            Assert.AreEqual(a1, 4);
            Assert.AreEqual(a2, 5);

            Assert.AreEqual(a3, 8);
            Assert.AreEqual(a4, 8);
            Assert.AreEqual(a5, 8);

            Assert.AreEqual(a6, 1);
            Assert.AreEqual(a7, 0);
            Assert.AreEqual(a8, 0);
        }