public void NoRoot()
        {
            Func <double, double> f1  = x => x * x + 4;
            Func <double, double> df1 = x => 2 * x;

            Assert.That(() => NewtonRaphson.FindRoot(f1, df1, -5, 5, 1e-14, 50), Throws.TypeOf <NonConvergenceException>());
        }
Beispiel #2
0
    private static bool Solve(float x1, float y1, float x2, float y2, float L, out double a, out double v, out double b)
    {
        a = 0;
        v = 0;
        b = 0;
        double d = x2 - x1;
        double h = y2 - y1;

        NewtonRaphson.Function f = delegate(double a0)
        {
            return(2.0 * a0 * Math.Sinh(d / (2.0 * a0)) - Math.Sqrt((L * L) - (h * h)));
        };
        NewtonRaphson.Function df = delegate(double a0)
        {
            return(2.0 * Math.Sinh(d / (2.0 * a0)) - d * Math.Cosh(d / (2.0 * a0)) / a0);
        };
        double newA = NewtonRaphson.FindRoot(0.01 * d, 0.00001, 100, f, df);

        if (double.IsNaN(newA) || double.IsInfinity(newA))
        {
            return(false);
        }
        else
        {
            a = newA;
            double k = 0.5 * (a * Math.Log((L + h) / (L - h)) - d);
            v = k - x1;
            b = y1 - a * Math.Cosh(k / a);
            return(true);
        }
    }
        public void LocalMinima()
        {
            Func <double, double> f1  = x => x * x * x - 2 * x + 2;
            Func <double, double> df1 = x => 3 * x * x - 2;

            Assert.AreEqual(0, f1(NewtonRaphson.FindRoot(f1, df1, -5, 6, 1e-14)));
            //Assert.AreEqual(0, f1(NewtonRaphson.FindRoot(f1, df1, 1, -2, 4, 1e-14, 100)));
        }
        public void Cubic()
        {
            // with complex roots (looking for the real root only): 3x^3 + 4x^2 + 5x + 6, derivative 9x^2 + 8x + 5
            Func <double, double> f1  = x => Polynomial.Evaluate(x, 6, 5, 4, 3);
            Func <double, double> df1 = x => Polynomial.Evaluate(x, 5, 8, 9);

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

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

            Assert.AreEqual(-6.1466562197069, NewtonRaphson.FindRoot(f2, df2, -8, -5, 1e-10), 1e-6);
            Assert.AreEqual(0.12124737195841, NewtonRaphson.FindRoot(f2, df2, -1, 1, 1e-10), 1e-6);
            Assert.AreEqual(4.0254088477485, NewtonRaphson.FindRoot(f2, df2, 3, 5, 1e-10), 1e-6);
        }
        public void MultipleRoots()
        {
            // Roots at -2, 2
            Func <double, double> f1  = x => x * x - 4;
            Func <double, double> df1 = x => 2 * x;

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

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

            Assert.AreEqual(0, f2(NewtonRaphson.FindRoot(f2, df2, -5, 6, 1e-14)));
            Assert.AreEqual(3, NewtonRaphson.FindRoot(f2, df2, -5, 3.5, 1e-14));
            Assert.AreEqual(4, NewtonRaphson.FindRoot(f2, df2, 3.2, 5, 1e-14));
            Assert.AreEqual(3, NewtonRaphson.FindRoot(f2, df2, 2.1, 3.9, 0.001, 50), 0.001);
            Assert.AreEqual(3, NewtonRaphson.FindRoot(f2, df2, 2.1, 3.4, 0.001, 50), 0.001);
        }