Ejemplo n.º 1
0
        public virtual float GetMinAoA(Conditions conditions, float guess = float.NaN)
        {
            BrentSearch minimizer = new BrentSearch((aoa) => GetLiftForceMagnitude(conditions, (float)aoa, 1), -60 * Mathf.Deg2Rad, -10 * Mathf.Deg2Rad, 0.0001);

            if (float.IsNaN(guess) || float.IsInfinity(guess))
            {
                minimizer.Maximize();
            }
            else
            {
                minimizer.LowerBound = guess - 2 * Mathf.Deg2Rad;
                minimizer.UpperBound = guess + 2 * Mathf.Deg2Rad;
                if (!minimizer.Maximize())
                {
                    minimizer.LowerBound = guess - 5 * Mathf.Deg2Rad;
                    minimizer.UpperBound = guess + 5 * Mathf.Deg2Rad;
                    if (!minimizer.Maximize())
                    {
                        minimizer.LowerBound = Mathf.Clamp(guess - 10 * Mathf.Deg2Rad, -90 * Mathf.Deg2Rad, -60 * Mathf.Deg2Rad);
                        minimizer.UpperBound = Mathf.Max(-10 * Mathf.Deg2Rad, guess + 10 * Mathf.Deg2Rad);
                        minimizer.Maximize();
                    }
                }
            }
            return((float)minimizer.Solution);
        }
Ejemplo n.º 2
0
        public virtual float GetMaxAoA(Conditions conditions, out float lift, float guess = float.NaN)
        {
            BrentSearch maximizer = new BrentSearch((aoa) => GetLiftForceMagnitude(conditions, (float)aoa, 1), 10 * Mathf.Deg2Rad, 60 * Mathf.Deg2Rad, 0.0001);

            if (float.IsNaN(guess) || float.IsInfinity(guess))
            {
                maximizer.Maximize();
            }
            else
            {
                maximizer.LowerBound = guess - 2 * Mathf.Deg2Rad;
                maximizer.UpperBound = guess + 2 * Mathf.Deg2Rad;
                if (!maximizer.Maximize())
                {
                    maximizer.LowerBound = guess - 5 * Mathf.Deg2Rad;
                    maximizer.UpperBound = guess + 5 * Mathf.Deg2Rad;
                    if (!maximizer.Maximize())
                    {
                        maximizer.LowerBound = Mathf.Min(10 * Mathf.Deg2Rad, guess - 10 * Mathf.Deg2Rad);
                        maximizer.UpperBound = Mathf.Clamp(guess + 10 * Mathf.Deg2Rad, 60 * Mathf.Deg2Rad, 90 * Mathf.Deg2Rad);
                        maximizer.Maximize();
                    }
                }
            }
            lift = (float)maximizer.Value;
            return((float)maximizer.Solution);
        }
Ejemplo n.º 3
0
        public void ConstructorTest()
        {
            #region doc_example
            // Suppose we were given the function x³ + 2x² - 10x and
            // we have to find its root, maximum and minimum inside
            // the interval [-4,3]. First, we express this function
            // as a lambda expression:
            Func <double, double> function = x => x * x * x + 2 * x * x - 10 * x;

            // And now we can create the search algorithm:
            BrentSearch search = new BrentSearch(function, -4, 3);

            // Finally, we can query the information we need
            bool   success1 = search.Maximize(); // should be true
            double max      = search.Solution;   // occurs at -2.61

            bool   success2 = search.Minimize(); // should be true
            double min      = search.Solution;   // occurs at  1.27

            bool   success3 = search.FindRoot(); // should be true
            double root     = search.Solution;   // occurs at  0.50
            #endregion

            Assert.IsTrue(success1);
            Assert.IsTrue(success2);
            Assert.IsTrue(success3);
            Assert.AreEqual(-2.6103173042172645, max);
            Assert.AreEqual(1.2769840667540548, min);
            Assert.AreEqual(-0.5, root);
        }
Ejemplo n.º 4
0
        public void ConstructorTest()
        {
            #region doc_example
            // Suppose we were given the function x³ + 2x² - 10x + 1 and
            // we have to find its root, maximum and minimum inside
            // the interval [-4, 2]. First, we express this function
            // as a lambda expression:
            Func <double, double> function = x => x * x * x + 2 * x * x - 10 * x + 1;

            // And now we can create the search algorithm:
            BrentSearch search = new BrentSearch(function, -4, 2);

            // Finally, we can query the information we need
            bool   success1 = search.Maximize(); // should be true
            double max      = search.Solution;   // occurs at -2.61

            bool   success2 = search.Minimize(); // should be true
            double min      = search.Solution;   // occurs at  1.28

            bool   success3 = search.FindRoot(); // should be true
            double root     = search.Solution;   // occurs at  0.10
            double value    = search.Value;      // should be zero
            #endregion

            Assert.IsTrue(success1);
            Assert.IsTrue(success2);
            Assert.IsTrue(success3);
            Assert.AreEqual(-2.6103173073566239, max);
            Assert.AreEqual(1.2769839857480398, min);
            Assert.AreEqual(0.10219566016872624, root);
            Assert.AreEqual(0, value, 1e-5);
        }
Ejemplo n.º 5
0
        public void MaximizeTest()
        {
            Func <double, double> f = x => - 2 * x * x - 3 * x + 5;

            double expected = -3 / 4.0;
            double actual   = BrentSearch.Maximize(f, -200, +200);

            Assert.AreEqual(expected, actual, 1e-10);
        }
Ejemplo n.º 6
0
        public void MaximizeWithLowMaxIterationsThrowsConvergenceException()
        {
            Func <double, double> f = x => - 2 * x * x - 3 * x + 5;
            double a = -200;
            double b = +200;

            Assert.ThrowsException <ConvergenceException>(() =>
            {
                double actual = BrentSearch.Maximize(f, a, b, maxIterations: 10);
            });
        }
Ejemplo n.º 7
0
        public void MaximizeWithLowMaxIterationsFailsToSolveButGivesLastKnownSolution()
        {
            Func <double, double> f = x => - 2 * x * x - 3 * x + 5;
            double a = -200;
            double b = +200;

            var search    = new BrentSearch(f, a, b, maxIterations: 10);
            var isSuccess = search.Maximize();

            Assert.AreEqual(false, isSuccess);
            Assert.AreEqual(BrentSearchStatus.MaxIterationsReached, search.Status);

            Assert.IsTrue(search.Solution > a && search.Solution < b);
        }
Ejemplo n.º 8
0
        public void MaximizeTest()
        {
            Func <double, double> f = x => - 2 * x * x - 3 * x + 5;

            double expected = -3 / 4d;
            double actual   = BrentSearch.Maximize(f, -200, +200);

            Assert.AreEqual(expected, actual, 1e-10);


            var  search    = new BrentSearch(f, -200, 200);
            bool isSuccess = search.Maximize();

            Assert.IsTrue(isSuccess);
            Assert.AreEqual(BrentSearchStatus.Success, search.Status);
            Assert.AreEqual(expected, search.Solution, 1e-10);
            Assert.AreEqual(f(expected), search.Value, double.Epsilon);
        }
Ejemplo n.º 9
0
 public virtual float GetMaxAoA(Conditions conditions)
 {
     return((float)BrentSearch.Maximize((aoa) => GetLiftForceMagnitude(conditions, (float)aoa, 1), 10 * Mathf.Deg2Rad, 60 * Mathf.Deg2Rad, 0.0001));
 }