Exemplo n.º 1
0
        public void T02_Multidimensional()
        {
            IDifferentiableVVFunction function = new TwoDimensionalFunction();

            Test(FindRoot.GlobalNewton, function, 1, 1, 0, 4, 1e-15);
            Test(FindRoot.Broyden, function, 1, 1, 0, 4, 1e-15);

            // now try computing the Jacobian via finite differences rather than directly
            Test(FindRoot.GlobalNewton, (IVectorValuedFunction)function, 1, 1, 0, 4, 1.5e-8);
            Test(FindRoot.Broyden, (IVectorValuedFunction)function, 1, 1, 0, 4, 4e-9);

            // now try a function that has multiple roots, x^2 + y^2 = 10 and x+y = 0
            function = new MultiRootTwoDimensionalFunction();
            // the initial point has to be in a correct quadrant (x positive and y negative, or vice versa) or it will fail to find a root
            double[] point = new double[2] {
                0.1, -1
            };
            Assert.IsTrue(FindRoot.GlobalNewton(function, point));
            Assert.AreEqual(Math.Sqrt(5), point[0], 1e-15);
            Assert.AreEqual(-Math.Sqrt(5), point[1], 1e-15);

            point = new double[2] {
                0.1, -1
            };
            Assert.IsTrue(FindRoot.Broyden(function, point));
            Assert.AreEqual(Math.Sqrt(5), point[0], 1.5e-11);
            Assert.AreEqual(-Math.Sqrt(5), point[1], 1.5e-11);
        }