Exemple #1
0
        public void FindRoot_QuadraticIncorrectIntialGuess_ReturnsRootWithinTolerance()
        {
            OneDimensionalFunction f = x => x * x - 4;

            double          lowerBound = -1;
            double          upperBound = 3;
            BisectionSolver solver     = new BisectionSolver(f, lowerBound, upperBound);

            var root = solver.FindRoot();

            Assert.IsTrue(Math.Abs(f(root)) < solver.Tolerance);
        }
Exemple #2
0
        public void FindRoot_CosFunction_ReturnsRootWithinTolerance()
        {
            OneDimensionalFunction f = Math.Cos;

            double          lowerBound = -Math.PI / 2;
            double          upperBound = Math.PI;
            BisectionSolver solver     = new BisectionSolver(f, lowerBound, upperBound);

            var root = solver.FindRoot();

            Assert.IsTrue(Math.Abs(f(root)) < solver.Tolerance);
        }
Exemple #3
0
        public void FindRoot_PolynomialLowerBoundIsZero_ReturnsExactRoot()
        {
            OneDimensionalFunction f = x => x * x - 4;

            double          lowerBound = -2;
            double          upperBound = 3;
            BisectionSolver solver     = new BisectionSolver(f, lowerBound, upperBound);

            var root = solver.FindRoot();

            Assert.AreEqual(lowerBound, root);
        }