Exemplo n.º 1
0
        public static double[] Solve(double c, double d)
        {
            if (d < 0)
            {
                return(Solve(c, -d).Reverse().Select(x => - x).ToArray());
            }

            // 最小の実数解
            var x1 = d == 0 && c >= 0 ? 0 : SolveNegative();

            // f(x) = (x - x_1) (x^2 + x_1 x + x_1^2 + c)
            var det = -3 * x1 * x1 - 4 * c;

            if (det < 0)
            {
                return new[] { x1 }
            }
            ;
            return(new[] { x1, (-x1 - Sqrt(det)) / 2, (-x1 + Sqrt(det)) / 2 });

            double SolveNegative()
            {
                var f = CubicEquation1.CreateFunction(c, d);

                var f1 = CubicEquation1.CreateDerivative(c);
                var x0 = -1D;

                while (f(x0) > 0)
                {
                    x0 *= 2;
                }
                return(NewtonMethod.Solve(f, f1, x0));
            }
        }
    }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();

            NewtonMethod equation = new NewtonMethod();
            // Func<double,double> f = x => Math.Pow(x,3)-x-0.231;
            // //derivative
            // Func<double,double> g = x => 3*Math.Pow(x,2)-1;

            // Func<double,double> f = x => x-Math.Sin(x) - 0.25;
            // Func<double, double> g = x=> 1-Math.Cos(x);

            // Func<double,double> f = x => Math.Pow(Math.E,-x)-5*x-3;
            // Func<double,double> g = x => Math.Pow(Math.E,-x)-5;

            Func <double, double> f = x => Math.Pow(x, 2) - 612;
            Func <double, double> g = x => 2 * x;

            //double? root = equation.ExecuteNewtonRaphson(f,g,3,0.00001);
            double?root = equation.ExecuteNewtonRaphson(f, g, 20, 0.05);

            watch.Stop();
            Console.WriteLine($"Root is {root}");
            System.Console.WriteLine("\nTime to run: " + watch.Elapsed.TotalMilliseconds + " milisec");
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var lb = 0.01f;
            var ub = 0.0001f;

            Console.WriteLine("Bisection methods:");
            Console.WriteLine("#5");
            Console.WriteLine(BisectionMethod.Evaluate(Function_5, 2, 3, lb));
            Console.WriteLine(BisectionMethod.Evaluate(Function_5, 2, 3, ub));
            Console.WriteLine("#9");
            Console.WriteLine(BisectionMethod.Evaluate(Function_9, 0, (float)Math.PI * (3f / 2f), lb));
            Console.WriteLine(BisectionMethod.Evaluate(Function_9, 0, (float)Math.PI * (3f / 2f), ub));
            Console.WriteLine("#11");
            Console.WriteLine(BisectionMethod.Evaluate(Function_11, 0.000000000001f, 2, lb));
            Console.WriteLine(BisectionMethod.Evaluate(Function_11, 0.000000000001f, 2, ub));
            Console.WriteLine("#15");
            Console.WriteLine(BisectionMethod.Evaluate(Function_15, 0.2f, 2f, lb));
            Console.WriteLine(BisectionMethod.Evaluate(Function_15, 0.2f, 2f, ub));
            Console.WriteLine("Newton method");
            Console.WriteLine("#5");
            Console.WriteLine(NewtonMethod.Evaluate(Function_5, 2, 3, lb));
            Console.WriteLine(NewtonMethod.Evaluate(Function_5, 2, 3, ub));
            Console.WriteLine("#9");
            Console.WriteLine(NewtonMethod.Evaluate(Function_9, 0, (float)Math.PI * (3f / 2f), lb));
            Console.WriteLine(NewtonMethod.Evaluate(Function_9, 0, (float)Math.PI * (3f / 2f), ub));
            Console.WriteLine("#11");
            Console.WriteLine(NewtonMethod.Evaluate(Function_11, 0.000000000001f, 2, lb));
            Console.WriteLine(NewtonMethod.Evaluate(Function_11, 0.000000000001f, 2, ub));
            Console.WriteLine("#15");
            Console.WriteLine(NewtonMethod.Evaluate(Function_15, 0.2f, 2f, lb));
            Console.WriteLine(NewtonMethod.Evaluate(Function_15, 0.2f, 2f, ub));
        }
Exemplo n.º 4
0
    // f(x) = ax^2 + bx + c = 0
    public static double[] Solve(double a, double b, double c)
    {
        if (a == 0)
        {
            throw new ArgumentException("The value must not be 0.", nameof(a));
        }
        if (a < 0)
        {
            return(Solve(-a, -b, -c));
        }

        var det = b * b - 4 * a * c;

        if (det.EqualsNearly(0))
        {
            return new[] { -b / (2 * a) }
        }
        ;
        if (det < 0)
        {
            return(new double[0]);
        }

        var f   = CreateFunction(a, b, c);
        var f1  = CreateDerivative(a, b);
        var x01 = (-b - Math.Max(det, 1)) / (2 * a);
        var x02 = (-b + Math.Max(det, 1)) / (2 * a);

        return(new[] { NewtonMethod.Solve(f, f1, x01), NewtonMethod.Solve(f, f1, x02) });
    }
}
Exemplo n.º 5
0
        public void quadric()
        {
            double func(Vector x) => x[0] * x[0];

            NewtonMethod sut = new NewtonMethod()
            {
                Function       = func,
                MaxError       = 1e-3,
                MinPointChange = 1e-3,
                Direction      = 0,
            };

            var result = sut.FindMinimum(makePoint(0.0));

            Assert.AreEqual(0.0, result.Value, 1e-3);
            Assert.AreEqual(0.0, result.Point[0], 1e-3);

            result = sut.FindMinimum(makePoint(2.0));

            Assert.AreEqual(0.0, result.Value, 1e-3);
            Assert.AreEqual(0.0, result.Point[0], 1e-3);

            result = sut.FindMinimum(makePoint(200.0));

            Assert.AreEqual(0.0, result.Value, 1e-3);
            Assert.AreEqual(0.0, result.Point[0], 1e-3);

            result = sut.FindMinimum(makePoint(-2000.0));

            Assert.AreEqual(0.0, result.Value, 1e-3);
            Assert.AreEqual(0.0, result.Point[0], 1e-3);
        }
Exemplo n.º 6
0
    public static void Main()
    {
        NewtonMethod nm = new NewtonMethod();
        // 解を計算(初期値, 収束条件)
        double ah = nm.calc(1.0, 0.0001);
        // 結果表示
        System.Console.WriteLine(ah); // 解:2
}
Exemplo n.º 7
0
 static void Main(string[] args)
 {
     NewtonMethod a = new NewtonMethod();
     double x;
     x = a.MethodN(0.01,64, 2);
     Console.Write("x=" + x);
     Console.ReadKey();
 }
Exemplo n.º 8
0
        public void NthRoot_3rdrootminus3_exceptionReturned()
        {
            var    actual   = NewtonMethod.NthRoot(-125, 3, 6);
            double expected = -5;

            Debug.WriteLine(expected - actual);
            Assert.IsTrue(condition: Math.Abs(expected - actual) < Math.Pow(10, -6));
        }
Exemplo n.º 9
0
 public void IsNewtonMethodCorrect()
 {
     Assert.That(NewtonMethod.Evaluate(new[] { 0d, 2, 3 }, new[] { 1d, 2, 1 }),
                 Is.EqualTo(new Polynomial(0, 1.5, -0.5)));
     Assert.That(NewtonMethod.Evaluate(new [] { -1, 0, 0.5, 1 }, new [] { 0, 2, 9d / 8, 0 }),
                 Is.EqualTo(new Polynomial(2, -1, -2, 1)));
     Assert.That(NewtonMethod.Evaluate(new [] { -2d, 0, 1 }, new [] { -4d, -1, -3 }),
                 Is.EqualTo(new Polynomial(11, -5d / 6, -7d / 6)));
 }
Exemplo n.º 10
0
 public void TestMethodRightNumZero()
 {
     int deg = 3, a = 0;
     double e = 0.001;
     NewtonMethod c = new NewtonMethod();
     double expected = 0.00;
     double actual = Math.Round(c.MethodN(e, a, deg), 2);
     Assert.AreEqual(expected, actual);
 }
Exemplo n.º 11
0
        public void TestMethodDegZero()
        {
            int deg = 0, a = 125;
            double e = 0.001;

            NewtonMethod c = new NewtonMethod();
            Boolean actual = double.IsNaN(c.MethodN(e,a,deg));

            Assert.IsTrue(actual);
        }
Exemplo n.º 12
0
    // f(x) = ax^3 + bx^2 + cx + d = 0
    public static double[] Solve(double a, double b, double c, double d)
    {
        if (a == 0)
        {
            throw new ArgumentException("The value must not be 0.", nameof(a));
        }
        if (a < 0)
        {
            return(Solve(-a, -b, -c, -d));
        }

        var f  = CreateFunction(a, b, c, d);
        var xc = -b / (3 * a);
        var yc = f(xc);

        if (yc < 0)
        {
            return(Solve(a, -b, c, -d).Reverse().Select(x => - x).ToArray());
        }

        var f1 = CreateDerivative(a, b, c);

        // 微修正
        // 自明解
        if (yc == 0 && f1(xc) > 0)
        {
            return new[] { xc }
        }
        ;
        if (yc == 0 && f1(xc) == 0)
        {
            return new[] { xc, xc, xc }
        }
        ;

        // xc より小さい実数解
        var x1 = SolveNegative();
        var p  = a * x1 + b;
        var q  = p * x1 + c;

        return(QuadraticEquation0.Solve(a, p, q).Prepend(x1).ToArray());

        double SolveNegative()
        {
            var x0 = -1D;

            while (f(xc + x0) > 0)
            {
                x0 *= 2;
            }
            return(NewtonMethod.Solve(f, f1, xc + x0));
        }
    }
}
Exemplo n.º 13
0
        public void NthRoot_sqrt2_Eps6Returned()
        {
            //A
            var expected = 1.4142;
            //A
            var actual = NewtonMethod.NthRoot(2, 2, 4);

            //A
            Debug.WriteLine(expected - actual);
            Assert.IsTrue(condition: Math.Abs(expected - actual) < Math.Pow(10, -4));
        }
        public void NewtonMethod_ValidData_ValidResult_Test()
        {
            double number   = double.Parse(TestContext.DataRow["Number"].ToString());
            double degrre   = double.Parse(TestContext.DataRow["Degree"].ToString());
            double accuracy = double.Parse(TestContext.DataRow["Accuracy"].ToString());
            double expected = double.Parse(TestContext.DataRow["Expected"].ToString());

            double result = NewtonMethod.FindNthRoot(number, degrre, accuracy);

            Assert.AreEqual(expected, result, accuracy);
        }
Exemplo n.º 15
0
        public void NthRoot_3thminus1000_Eps15Returned()
        {
            //A
            double expected = -10;
            //A
            var actual = NewtonMethod.NthRoot(-1000, 3, 15);

            //A

            Debug.WriteLine(expected - actual);
            Assert.IsTrue(condition: Math.Abs(expected - actual) < Math.Pow(10, -15));
        }
Exemplo n.º 16
0
        public void NthRoot_maxvalue_Eps15Returned()
        {
            //A
            var expected = Math.Pow(double.MaxValue, 0.01);
            //A
            var actual = NewtonMethod.NthRoot(double.MaxValue, 100, 15);

            //A

            Debug.WriteLine(expected - actual);
            Assert.IsTrue(condition: Math.Abs(expected - actual) < Math.Pow(10, -15));
        }
Exemplo n.º 17
0
        public void Function_11_Test()
        {
            var a = 0.000000000001f;
            var b = 2f;
            var expectedResult = 0f;
            var result1        = NewtonMethod.Evaluate(Function_11, a, b, accuracyLowerBound);
            var result2        = NewtonMethod.Evaluate(Function_11, a, b, accuracyUpperBound);

            Assert.LessOrEqual(result2.Value, expectedResult);

            Console.WriteLine(result1);
            Console.WriteLine(result2);
        }
Exemplo n.º 18
0
        public void Pow_5And_NegativeNumber5_ShouldThrowExeption()
        {
            //Arrange
            var power   = 5;
            var epsilon = 0.0000001;
            var number  = 0;

            //Act
            void GetException() => NewtonMethod.Pow(power, number, epsilon);

            //Assert
            Assert.Throws <ArgumentException>(GetException);
        }
Exemplo n.º 19
0
        public void Function_15_Test()
        {
            var a = 0.2f;
            var b = 2f;
            var expectedResult = 2f;
            var result1        = NewtonMethod.Evaluate(Function_15, a, b, accuracyLowerBound);
            var result2        = NewtonMethod.Evaluate(Function_15, a, b, accuracyUpperBound);

            Assert.GreaterOrEqual(result2.Iterations, expectedResult);

            Console.WriteLine(result1);
            Console.WriteLine(result2);
        }
Exemplo n.º 20
0
        public void DrawInterpolation(LambdaFunc func, float step)
        {
            DrawFunction((x) => (float)(50 * func(x / 100) + 100),
                         Color.Black);

            double[] pointsX = new double[] { 0, Math.PI / 2, Math.PI, Math.PI * 3 / 2, Math.PI * 2 };
            double[] pointsY = new double[] { func(pointsX[0]), func(pointsX[1]), func(pointsX[2]), func(pointsX[3]), func(pointsX[4]) };


            DrawFunction((x) => (float)(50 * NewtonMethod.Newton(x / 100, pointsX, pointsY, (float)step) + 100), Color.Red);

            DrawFunction((x) => (float)(50 * LagrangeMethod.Langrange(x / 100, pointsX, pointsY, (float)step - 0.6f) + 100), Color.Green);
        }
Exemplo n.º 21
0
        public void Function_9_Test()
        {
            var   a       = 0f;
            float b       = (float)Math.PI * (3f / 2f);
            var   result1 = NewtonMethod.Evaluate(Function_9, a, b, accuracyLowerBound);
            var   result2 = NewtonMethod.Evaluate(Function_9, a, b, accuracyUpperBound);

            Assert.LessOrEqual(result2.Value, result1.Value);
            Assert.GreaterOrEqual(result2.Iterations, result1.Iterations);

            Console.WriteLine(result1);
            Console.WriteLine(result2);
        }
Exemplo n.º 22
0
        public void Test_Function()
        {
            var a       = 1.6f;
            var b       = 6f;
            var result1 = NewtonMethod.Evaluate(TestFunction, a, b, accuracyLowerBound);
            var result2 = NewtonMethod.Evaluate(TestFunction, a, b, accuracyUpperBound);

            Assert.LessOrEqual(result2.Value, result1.Value);
            Assert.GreaterOrEqual(result2.Iterations, result1.Iterations);

            Console.WriteLine(result1);
            Console.WriteLine(result2);
        }
        public static void Update(DiffirentialEquationInput input)
        {
            _lagrange = new LagrangeMethod();
            _newton   = new NewtonMethod();
            _repo     = new FunctionRepository();
            FuncInit();

            X0       = input.X0;
            XN       = input.XN;
            Y0       = input.Y0;
            Accuracy = input.Accuracy;
            FuncType = input.FuncType;
        }
Exemplo n.º 24
0
        private static void Lab1()
        {
            var newton = new NewtonMethod(F, Df, Constants.A, Constants.B);

            Console.WriteLine(newton.GetAnswer());

            var simple = new SimpleIterations(F, Phi, Dphi, Constants.A, Constants.B);

            Console.WriteLine(simple.GetAnswer());

            //var simpleV2 = new SimpleIterationsV2(F, Phi, Dphi, Constants.Av2, Constants.Bv2);
            //Console.WriteLine(simpleV2.GetAnswer());
        }
Exemplo n.º 25
0
        public void Pow_5And_Number10_ShouldReturnCorrectValue()
        {
            //Arrange
            var power    = 5;
            var epsilon  = 0.0000001;
            var number   = 10;
            var expected = Math.Pow(10, (double)1 / 5);

            //Act
            var actual = NewtonMethod.Pow(power, number, epsilon);

            //Assert
            Assert.AreEqual(expected, actual, epsilon, "Pow 5 and number 10 test is successful");
        }
Exemplo n.º 26
0
        public void multimodal()
        {
            // x ^ 4 - 0.5 * x ^ 3 - 2 * x ^ 2;
            // max12 -> 0.0, 0.0
            // min1 -> (-0.83 ~0.01, -0.617 ~0.002)
            // min2 -> ( 1.20 ~0.01, -1.670 ~0.002)

            double func(Vector x) => x[0] * x[0] * (x[0] * x[0] - 0.5 * x[0] - 2.0);

            NewtonMethod sut = new NewtonMethod()
            {
                Function       = func,
                MaxError       = 1e-3,
                MinPointChange = 1e-3,
                Direction      = 0,
            };

            var result = sut.FindMinimum(makePoint(-100.0));

            Assert.AreEqual(-0.617, result.Value, 2e-3);
            Assert.AreEqual(-0.83, result.Point[0], 1e-2);

            result = sut.FindMinimum(makePoint(-0.01));

            Assert.AreEqual(-0.617, result.Value, 2e-3);
            Assert.AreEqual(-0.83, result.Point[0], 1e-2);

            result = sut.FindMinimum(makePoint(0.01));

            Assert.AreEqual(-1.670, result.Value, 2e-3);
            Assert.AreEqual(1.2, result.Point[0], 1e-2);

            result = sut.FindMinimum(makePoint(1.201));

            Assert.AreEqual(-1.670, result.Value, 2e-3);
            Assert.AreEqual(1.2, result.Point[0], 1e-2);

            result = sut.FindMinimum(makePoint(1000));

            Assert.AreEqual(-1.670, result.Value, 2e-3);
            Assert.AreEqual(1.2, result.Point[0], 1e-2);

            result = sut.FindMinimum(makePoint(0.0));

            Assert.AreEqual(-0.617, result.Value, 2e-3);
            Assert.AreEqual(-0.83, result.Point[0], 1e-2);
        }
Exemplo n.º 27
0
        public ScalarValue Function(FunctionValue f, ScalarValue x)
        {
            Func <double, double> lambda = t =>
            {
                var sv = f.Perform(Context, new ScalarValue(t));

                if (!(sv is ScalarValue))
                {
                    throw new YAMPArgumentInvalidException(Name, sv.Header, 1);
                }

                return(((ScalarValue)sv).Re);
            };

            var newton = new NewtonMethod(lambda, x.Re, 0.00001);

            return(new ScalarValue(newton.Result[0, 0]));
        }
Exemplo n.º 28
0
        float CalcAttenuateRate()
        {
            float n = maxSENum;

            return(NewtonMethod.run(
                       delegate(float p)
            {
                return (1.0f - Mathf.Pow(p, n)) / (1.0f - p) - 1.0f / initVolume;
            },
                       delegate(float p)
            {
                float ip = 1.0f - p;
                float t0 = -n * Mathf.Pow(p, n - 1.0f) / ip;
                float t1 = (1.0f - Mathf.Pow(p, n)) / ip / ip;
                return t0 + t1;
            },
                       0.9f, 100
                       ));
        }
Exemplo n.º 29
0
    private void drawPlot(Func <double, double> func, double x0, double h, int count)
    {
        var polynomial = NewtonMethod.Interpolate(func, x0, h, count);

        //Отступ, чтобы отрезок интерполяции занимал 75% графика (отступы - 12.5% каждый)
        double offsetNodes = count * 0.125;
        double left        = x0 - offsetNodes * h;
        double right       = x0 + (offsetNodes + count - 1) * h;

        if (left > right)
        {
            double temp = left;
            left  = right;
            right = temp;
        }

        var myModel = new PlotModel {
            Title = "График"
        };

        myModel.Series.Add(new FunctionSeries(func, left, right, 0.1, "f(x)"));
        myModel.Series.Add(new FunctionSeries(polynomial.ValueAt, left, right, 0.1, "P(x)"));

        var scatterSeries = new ScatterSeries {
            MarkerType = MarkerType.Circle
        };

        for (int i = 0; i < count; i++)
        {
            double xn = x0 + i * h;
            scatterSeries.Points.Add(new ScatterPoint(xn, func(xn), 5, 255));
        }
        myModel.Series.Add(scatterSeries);

        plotView.Model = myModel;
    }
Exemplo n.º 30
0
 public void Sqrt_ThrowsArgumentOutOfRangeException(double number, int n, double precision)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => NewtonMethod.Sqrt(number, n, precision));
 }
Exemplo n.º 31
0
 public void NthRoot_sqrt2_sqrt2AccuracyEps16Returned()
 {
     var actual = NewtonMethod.NthRoot(2, 2, 16);
 }
Exemplo n.º 32
0
        public void ExecuteMethod(
            string nameMethod,
            double param1,
            double param2,
            double param3,
            double param4,
            string testFunction,
            int rangeArray,
            double[] LinSysMasA,
            double[,] LinSysMatrixB,
            double[] massX,
            double[] massF,
            double[] massW,
            double pointInterpolation,
            double[,]  MatrixAlgebraA,
            double pointPercentile,
            double pointGenerator)
        {
            TestFunction = testFunction;
            switch (nameMethod)
            {
            //*** Approximate decision of equalization f(x)=0 ***
            case "Bisection Method":
            {
                Bisection bisect = new Bisection(new FunctionOne(TestFunBisection), param1, param2, param3);
                result = "\n Result of the program: \n" +
                         "    x= " + string.Format("{0:f" + precision + "}", bisect.Result);
            }
            break;

            case "Chord Method":
            {
                Сhord chord = new Сhord(new FunctionOne(TestFunNewton), new FunctionOne(TestFunNewton2), param1, param2, param3, param4);
                result = "\n Result of the program: \n" +
                         "   x= " + string.Format("{0:f" + precision + "}", chord.GetSolution());
            }
            break;

            case "Iteration Method":
            {
                IterationMethod itermet = new IterationMethod(new FunctionOne(TestFunIteration), param1, param2, param3, param4);
                result = "\n Result of the program: \n" +
                         "   x= " + string.Format("{0:f" + precision + "}", itermet.GetSolution());
            }
            break;

            case "Newton Method":
            {
                Newton newton = new Newton(new FunctionOne(TestFunNewton), new FunctionOne(TestFunNewton2), param1, param2, param3, param4);
                result = "\n Result of the program: \n" +
                         "   x= " + string.Format("{0:f" + precision + "}", newton.GetSolution());
            }
            break;

            //*** Differential Equations ***
            case "Euler Simple":
            {
                EulerSimple eulersimpl        = new EulerSimple(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var         result_eulersimpl = eulersimpl.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_eulersimpl[0, j])
                             + " : "
                             + string.Format("{0:f" + precision + "}", result_eulersimpl[1, j]) + "\n";
                }
            }
            break;

            case "Euler Modified":
            {
                EulerModified eulerModif        = new EulerModified(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var           result_eulerModif = eulerModif.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_eulerModif[0, j])
                             + " : " + string.Format("{0:f" + precision + "}", result_eulerModif[1, j]) + "\n";
                    // result = result + "fun=\n";
                    // result = result +Convert.ToString( TestFunDifferEquations);
                }
            }
            break;

            case "Euler Corrected":
            {
                EulerCorrected eulerCorrect        = new EulerCorrected(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var            result_eulerCorrect = eulerCorrect.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_eulerCorrect[0, j])
                             + " : " + string.Format("{0:f" + precision + "}", result_eulerCorrect[1, j]) + "\n";
                }
            }
            break;

            case "Runge-Kutta4":
            {
                RungeKutta4 rungeKutta4        = new RungeKutta4(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var         result_rungeKutta4 = rungeKutta4.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_rungeKutta4[0, j])
                             + " : " + string.Format("{0:f" + precision + "}", result_rungeKutta4[1, j]) + "\n";
                }
            }
            break;

            //*** Integration ***
            case "Chebishev":
                Chebishev chebish        = new Chebishev(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var       result_chebish = chebish.GetSolution();

                for (int j = 0; j <= Convert.ToInt32(param3); j++)
                {
                    result = result + "\n   h = " + string.Format("{0:f" + precision + "}", result_chebish[1, j]) + "   \t   integral = "
                             + string.Format("{0:f" + precision + "}", result_chebish[0, j]);
                }
                break;

            case "Simpson":
                Simpson simps        = new Simpson(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var     result_simps = simps.GetSolution();
                for (int j = 0; j <= Convert.ToInt32(param3); j++)
                {
                    result = result + "\n   h = " + string.Format("{0:f" + precision + "}", result_simps[1, j]) +
                             " \t   integral = " + string.Format("{0:f" + precision + "}", result_simps[0, j]);
                }
                break;

            case "Simpson2":
                Simpson2 simps2        = new Simpson2(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var      result_simps2 = simps2.GetSolution();
                result = "\n\n  integral = " + string.Format("{0:f" + precision + "}", result_simps2);
                break;

            case "Trapezium":
                Trapezium trapez        = new Trapezium(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var       result_trapez = trapez.GetSolution();
                for (int j = 0; j <= Convert.ToInt32(param3); j++)
                {
                    result = result + "\n   h = " + string.Format("{0:f" + precision + "}", result_trapez[1, j])
                             + " \t   integral = " + string.Format("{0:f" + precision + "}", result_trapez[0, j]);
                }
                break;

            //*** Non Linear equalization ***
            case "Half Division":
                HalfDivision halfdiv = new HalfDivision(new FunctionOne(TestFunNonLinearEquations), param1, param2, param3);
                result = "\n    X = " + string.Format("{0:f" + precision + "}", halfdiv.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", halfdiv.GetSolution()[1, 0]);
                break;

            case "Hord Metod":
                HordMetod hormet = new HordMetod(new FunctionOne(TestFunNonLinearEquations), param1, param2, Convert.ToInt32(param3));
                result = "\n    X = " + string.Format("{0:f" + precision + "}", hormet.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", hormet.GetSolution()[1, 0]);
                break;

            case "Newton Metod":
                NewtonMethod newt = new NewtonMethod(new FunctionOne(TestFunNonLinearEquations), param1, param2);
                result = "\n     X = " + string.Format("{0:f" + precision + "}", newt.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", newt.GetSolution()[1, 0]);
                break;

            case "Secant Metod":
                Secant sec = new Secant(new FunctionOne(TestFunNonLinearEquations), param1, param2);
                result = "\n     X = " + string.Format("{0:f" + precision + "}", sec.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", sec.GetSolution()[1, 0]);
                break;

            default:
                result = "";
                break;

            //*** Linear Systems ***
            case "Gaus":
                double[,] LinSysMatrix;
                LinSysMatrix = new double[100, 100];
                for (int l = 0; l < rangeArray; l++)
                {
                    for (int j = 0; j < rangeArray; j++)
                    {
                        LinSysMatrix[l, j] = LinSysMatrixB[l, j];
                    }
                    LinSysMatrix[l, rangeArray] = LinSysMasA[l];
                }
                Gaus gaus        = new Gaus(4, LinSysMatrix);
                var  result_gaus = gaus.GetSolution();
                result = "";
                for (int j = 0; j < result_gaus.Length; j++)
                {
                    result = result + "\n         X" + j + "  =  "
                             + string.Format("{0:f" + precision + "}", result_gaus[j]) + ";";
                }
                break;

            case "Zeidel":
                Zeidel zeidel        = new Zeidel(4, LinSysMatrixB, LinSysMasA);
                var    result_zeidel = zeidel.GetSolution();
                result = "";
                for (int j = 0; j < result_zeidel.Length; j++)
                {
                    result = result + "\n         X" + j + "  =  "
                             + string.Format("{0:f" + precision + "}", result_zeidel[j]) + ";";
                }
                break;

            //*** Interpolation ***
            case "Lagrange Interpolator":

                LagrangeInterpolator lagran = new LagrangeInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", lagran.GetSolution());
                break;

            case "Newton Interpolator":

                NewtonInterpolator newinterpol = new NewtonInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", newinterpol.GetSolution());
                break;

            case "Neville Interpolator":

                NevilleInterpolator newill = new NevilleInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", newill.GetSolution());
                break;

            case "Spline Interpolator":
                SplineInterpolator spline = new SplineInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", spline.GetSolution());
                break;

            case "Barycentric Interpolator":

                BarycentricInterpolation barycen = new BarycentricInterpolation(massX, massF, massW, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", barycen.GetSolution());
                break;

            //*** Matrix Algebra ***
            case "Matrix Determinant":
                MatrixDeterminant matrdet = new MatrixDeterminant();
                result = " \n\n\n  Determinant =  " + string.Format("{0:f" + precision + "}", matrdet.MatrixDet(MatrixAlgebraA, rangeArray)) + ";\n";
                break;

            case "RMatrix LU":

                MatrixLU matrlu         = new MatrixLU(MatrixAlgebraA, rangeArray, rangeArray);
                var      result_matrlu  = matrlu.GetSolution();
                var      result_matrlu2 = matrlu.GetSolution2();
                result = "A:\n";
                for (int ii = 0; ii < rangeArray; ii++)
                {
                    for (int j = 0; j < rangeArray; j++)
                    {
                        result = result + "  \t " + string.Format("{0:f" + precision + "}", result_matrlu[ii, j]);
                    }
                    result = result + " \n";
                }
                result = result + "\nL:\n";
                for (int ii = 0; ii < rangeArray; ii++)
                {
                    result = result + "      " + string.Format("{0:f" + precision + "}", result_matrlu2[ii]);
                }
                result = result + " \n ";
                break;

            case "Matrix Inverse LU":
                RMatrixLuInverse matrluinv = new RMatrixLuInverse();

                MatrixLU matrlu2 = new MatrixLU(MatrixAlgebraA, rangeArray, rangeArray);
                if (matrluinv.rmatrixluinverse(MatrixAlgebraA, rangeArray, matrlu2.GetSolution2()) == true)
                {
                    result = "\n             An inverse matrix exists \n\n ";
                    var result_matrluinv = matrluinv.GetSolution();
                    for (int ii = 0; ii < rangeArray; ii++)
                    {
                        for (int j = 0; j < rangeArray; j++)
                        {
                            result = result + "    \t" + string.Format("{0:f" + precision + "}", result_matrluinv[ii, j]);
                        }
                        result = result + "\n\n";
                    }
                }
                else
                {
                    result = "\n             An inverse matrix does not exist";
                }

                break;

            //*** Optimizing***
            case "Brentopt":
                Brentopt brent = new Brentopt(new FunctionOne(TestFunOptimizing), param1, param2, param3);
                result = "\n    Point of the found minimum :";
                result = result + "\n\n    XMin = " + string.Format("{0:f" + precision + "}", brent.GetSolution());
                result = result + "\n\n     A value of function is in the found minimum :";
                result = result + "\n\n    F(XMin) = " + string.Format("{0:f" + precision + "}", brent.GetSolutionFunction());
                break;

            case "Golden Section":
                GoldenSection godsection = new GoldenSection(new FunctionOne(TestFunOptimizing), param1, param2, Convert.ToInt32(param3));
                result = "\n    Scopes   of segment  which a decision of task is on .";
                result = result + "\n\n    a = " + string.Format("{0:f" + precision + "}", godsection.GetSolutionA());
                result = result + "\n\n    b = " + string.Format("{0:f" + precision + "}", godsection.GetSolutionB());
                break;

            case "Pijavsky":
                Pijavsky pijavsky = new Pijavsky(new FunctionOne(TestFunOptimizing), param1, param2, param3, Convert.ToInt32(param4));
                result = "\n    Abscissa of the best point from found..";
                result = result + "\n\n    F = " + string.Format("{0:f" + precision + "}", pijavsky.GetSolution());
                break;

            //*** Statistics ***
            case "Correlation Pearson":


                CorrelationPearson corelperson = new CorrelationPearson(massX, massF, 6);
                result = "\n    Pearson product-moment correlation coefficient.";
                result = result + "\n\n    K = " + string.Format("{0:f" + precision + "}", corelperson.GetSolution());
                break;

            case "Correlation Spearmans Rank":
                CorrelationSpearmansRank corelspear = new CorrelationSpearmansRank(massX, massF, 6);
                result = "\n    Pearson product-moment correlation coefficient.";
                result = result + "\n\n    K = " + string.Format("{0:f" + precision + "}", corelspear.GetSolution());
                break;

            case "Descriptive Statistics Median":
                DescriptiveStatisticsADevMedian desceripM = new DescriptiveStatisticsADevMedian(massX, 6);
                result = "\n    Output parameters:";
                result = result + "\n\n    M = " + string.Format("{0:f" + precision + "}", desceripM.GetSolution());
                break;

            case "Descriptive Statistics Moments":
                DescriptiveStatisticsMoments desceripMo = new DescriptiveStatisticsMoments(massX, 6);
                result = "\n    Output parameters:";
                result = result + "\n\n    M = " + string.Format("{0:f" + precision + "}", desceripMo.GetSolution());
                result = result + "\n\n    Variance = " + string.Format("{0:f" + precision + "}", desceripMo.variance);
                result = result + "\n\n    Skewness = " + string.Format("{0:f" + precision + "}", desceripMo.skewness) + " (if variance<>0; zero otherwise)";
                result = result + "\n\n    Kurtosis = " + string.Format("{0:f" + precision + "}", desceripMo.kurtosis) + " (if variance<>0; zero otherwise)";
                break;

            case "Descriptive Statistics Percentile":
                DescriptiveStatisticsPercentile desceripP = new DescriptiveStatisticsPercentile(massX, 6, pointPercentile);
                result = "\n    Output parameters:";
                result = result + "\n\n    V = " + string.Format("{0:f" + precision + "}", desceripP.GetSolution());
                break;

            case "Generator 1":
                RandomGeneratorsMethod1 random1 = new RandomGeneratorsMethod1();
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random1.GetSolution());
                break;

            case "Generator 2":
                RandomGeneratorsMethod2 random2 = new RandomGeneratorsMethod2(Convert.ToInt32(pointGenerator));
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random2.GetSolution());
                break;

            case "Generator 3":
                RandomGeneratorsMethod3 random3 = new RandomGeneratorsMethod3();
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random3.GetSolution());
                break;

            case "Generator 4":
                RandomGeneratorsMethod4 random4 = new RandomGeneratorsMethod4();
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random4.GetSolution());
                break;

            case "Generator 5":
                RandomGeneratorsMethod5 random5 = new RandomGeneratorsMethod5(pointGenerator);
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random5.GetSolution());
                break;
            }
        }
Exemplo n.º 33
0
 public double Sqrt_PositiveTest(double number, int n, double precision = 0.000001)
 {
     return(NewtonMethod.Sqrt(number, n, precision));
 }