示例#1
0
            public float GetParameter(ICurve <float, float> curve, float interpolatedPoint, int maxNumberOfIterations)
            {
                _curve = curve;
                _rootFinder.MaxNumberOfIterations = maxNumberOfIterations;
                float parameter = _rootFinder.FindRoot(0, 1, interpolatedPoint);

                _curve = null;
                return(parameter);
            }
示例#2
0
            public float GetParameter(ICurve <float, Vector2F> curve, float length, int maxNumberOfIterations, float tolerance)
            {
                _curve = curve;
                _maxNumberOfIterations            = maxNumberOfIterations;
                _rootFinder.MaxNumberOfIterations = maxNumberOfIterations;
                _tolerance           = tolerance;
                _rootFinder.EpsilonY = tolerance;
                float parameter = _rootFinder.FindRoot(0, 1, length);

                _curve = null;
                return(parameter);
            }
        // This method shows how to use root finding.
        // A method y = Foo(x) is given and we want to find x such that Foo(x) = 7.
        private void FindRoot()
        {
            var debugRenderer = GraphicsScreen.DebugRenderer2D;

            debugRenderer.DrawText("----- RootFinding Example:");

            // Create a new instance of a root finder class.
            // The Newton-Raphson root finding method uses the function and the first-order
            // derivative of the function where we want to find solutions. If the derivate
            // is not known, other root finding classes can be used, like the BisectionMethod
            // or RegulaFalsiMethod classes.
            var rootFinder = new ImprovedNewtonRaphsonMethodF(Foo, FooDerived);

            // Several x could result in Foo(x) = 7. So we need to define an x interval
            // that contains the desired x solution.
            // We decide to look for a solution near x = 0 and let the rootFinder approximate
            // an x interval that contains the result.
            float x0 = 0;
            float x1 = 0;

            // Find an interval [x0, x1] that contains an x such that Foo(x) = 7.
            rootFinder.ExpandBracket(ref x0, ref x1, 7);

            // Now we use the root finder to find an x within [x0, x1] such that Foo(x) = 7.
            float x = rootFinder.FindRoot(x0, x1, 7);

            // Lets check the result.
            float y = Foo(x);

            // Important note: We use Numeric.AreEqual() to safely compare floating-point numbers.
            if (Numeric.AreEqual(y, 7))
            {
                debugRenderer.DrawText("Solution is correct.\n");
            }
            else
            {
                debugRenderer.DrawText("Solution is wrong.\n");
            }
        }
        public void FindRootTest1()
        {
            Func <float, float> polynomial        = x => (x - 1) * (x - 10) * (x - 18);
            Func <float, float> computeDerivative = x => 3 * x * x - 58 * x + 208;

            ImprovedNewtonRaphsonMethodF rootFinder = new ImprovedNewtonRaphsonMethodF(polynomial, computeDerivative);

            rootFinder.EpsilonX = Numeric.EpsilonF / 100;

            float xRoot = rootFinder.FindRoot(0, 2);

            Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
            Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

            xRoot = rootFinder.FindRoot(4, 10);
            Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
            Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

            xRoot = rootFinder.FindRoot(10, 4);
            Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
            Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

            xRoot = rootFinder.FindRoot(10, 12);
            Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
            Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

            xRoot = rootFinder.FindRoot(2, 0);
            Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
            Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

            xRoot = rootFinder.FindRoot(0, 3);
            Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
            Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

            xRoot = rootFinder.FindRoot(3, 0);
            Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
            Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

            xRoot = rootFinder.FindRoot(-6, 2);
            Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
            Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

            xRoot = rootFinder.FindRoot(-1, 1);
            Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
            Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

            xRoot = rootFinder.FindRoot(-1, 1);
            Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
            Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

            xRoot = rootFinder.FindRoot(0.9f, 9.9f);
            Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
            Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

            xRoot = rootFinder.FindRoot(2, 3);
            Assert.IsTrue(float.IsNaN(xRoot));
            Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

            xRoot = rootFinder.FindRoot(3, 2);
            Assert.IsTrue(float.IsNaN(xRoot));
            Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

            rootFinder.MaxNumberOfIterations = 1;
            xRoot = rootFinder.FindRoot(0, 1000);
            Assert.IsTrue(float.IsNaN(xRoot));
            Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);
        }
        public void FindRootTest1()
        {
            Func<float, float> polynomial = x => (x - 1)*(x - 10)*(x - 18);
              Func<float, float> computeDerivative = x => 3 * x * x - 58 * x + 208;

              ImprovedNewtonRaphsonMethodF rootFinder = new ImprovedNewtonRaphsonMethodF(polynomial, computeDerivative);

              rootFinder.EpsilonX = Numeric.EpsilonF / 100;

              float xRoot = rootFinder.FindRoot(0, 2);
              Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
              Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

              xRoot = rootFinder.FindRoot(4, 10);
              Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
              Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

              xRoot = rootFinder.FindRoot(10, 4);
              Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
              Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

              xRoot = rootFinder.FindRoot(10, 12);
              Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
              Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

              xRoot = rootFinder.FindRoot(2, 0);
              Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
              Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

              xRoot = rootFinder.FindRoot(0, 3);
              Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
              Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

              xRoot = rootFinder.FindRoot(3, 0);
              Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
              Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

              xRoot = rootFinder.FindRoot(-6, 2);
              Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
              Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

              xRoot = rootFinder.FindRoot(-1, 1);
              Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
              Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

              xRoot = rootFinder.FindRoot(-1, 1);
              Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
              Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

              xRoot = rootFinder.FindRoot(0.9f, 9.9f);
              Assert.IsTrue(Numeric.AreEqual(0, polynomial(xRoot)));
              Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

              xRoot = rootFinder.FindRoot(2, 3);
              Assert.IsTrue(float.IsNaN(xRoot));
              Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

              xRoot = rootFinder.FindRoot(3, 2);
              Assert.IsTrue(float.IsNaN(xRoot));
              Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);

              rootFinder.MaxNumberOfIterations = 1;
              xRoot = rootFinder.FindRoot(0, 1000);
              Assert.IsTrue(float.IsNaN(xRoot));
              Console.WriteLine("NumberOfIterations: {0}", rootFinder.NumberOfIterations);
        }