コード例 #1
0
        public void Cubic()
        {
            // with complex roots (looking for the real root only): 3x^3 + 4x^2 + 5x + 6
            Func <double, double> f1 = x => Polynomial.Evaluate(x, 6, 5, 4, 3);

            Assert.AreEqual(-1.265328088928, Secant.FindRoot(f1, -2, -1, -10, 10, 1e-10), 1e-6);
            Assert.AreEqual(-1.265328088928, Secant.FindRoot(f1, -5, 6, -10, 10, 1e-10), 1e-6);

            // real roots only: 2x^3 + 4x^2 - 50x + 6
            Func <double, double> f2 = x => Polynomial.Evaluate(x, 6, -50, 4, 2);

            Assert.AreEqual(-6.1466562197069, Secant.FindRoot(f2, -8, -5, -10, 10, 1e-10), 1e-6);
            Assert.AreEqual(0.12124737195841, Secant.FindRoot(f2, -1, 1, -10, 10, 1e-10), 1e-6);
            Assert.AreEqual(4.0254088477485, Secant.FindRoot(f2, 3, 5, 0, 10, 1e-10), 1e-6);
        }
コード例 #2
0
        public void MultipleRoots()
        {
            // Roots at -2, 2
            Func <double, double> f1 = x => x * x - 4;

            Assert.AreEqual(0, f1(Secant.FindRoot(f1, 0, 6, -10, 10, 1e-14)));
            Assert.AreEqual(-2, Secant.FindRoot(f1, -3, 0, -10, 10, 1e-14));
            Assert.AreEqual(2, Secant.FindRoot(f1, 1, 4, -10, 10, 1e-14));
            Assert.AreEqual(0, f1(Secant.FindRoot(x => f1(x), -6, 0, -10, 10, 1e-14)));
            Assert.AreEqual(-2, Secant.FindRoot(x => f1(x), -5, -1, -10, 10, 1e-14));
            Assert.AreEqual(2, Secant.FindRoot(x => f1(x), 1, 4, -10, 10, 1e-14));

            // Roots at 3, 4
            Func <double, double> f2 = x => (x - 3) * (x - 4);

            Assert.AreEqual(0, f2(Secant.FindRoot(f2, 3.5, 6, 0, 10, 1e-14)));
            Assert.AreEqual(3, Secant.FindRoot(f2, -5, 3.5, -10, 10, 1e-14));
            Assert.AreEqual(4, Secant.FindRoot(f2, 3.2, 4.2, 0, 10, 1e-14));
            Assert.AreEqual(3, Secant.FindRoot(f2, 2.1, 3.1, 0, 10, 0.001, 50), 0.001);
            Assert.AreEqual(3, Secant.FindRoot(f2, 2.1, 3.4, 0, 10, 0.001, 50), 0.001);
        }
コード例 #3
0
        public void NoRoot()
        {
            Func <double, double> f1 = x => x * x + 4;

            Assert.That(() => Secant.FindRoot(f1, -5, 5, -10, 10, 1e-14, 50), Throws.TypeOf <NonConvergenceException>());
        }
コード例 #4
0
        public void LocalMinima()
        {
            Func <double, double> f1 = x => x * x * x - 2 * x + 2;

            Assert.AreEqual(0, f1(Secant.FindRoot(f1, -5, 6, -10, 10, 1e-14)));
        }