Example #1
0
        public static MethodRK GetMethod_Rule38_P4S4(VectorFunk f)
        {
            var C = new double[4];

            C[0] = 0;
            C[1] = 1.0 / 3.0;
            C[2] = 2.0 / 3.0;
            C[3] = 1;
            var B = new double[4];

            B[0] = 1.0 / 8.0;
            B[1] = 3.0 / 8.0;
            B[2] = 3.0 / 8.0;
            B[3] = 1.0 / 8.0;
            var A = new double[4][];

            A[0] = new double[4] {
                0, 0, 0, 0
            };
            A[1] = new double[4] {
                1.0 / 3.0, 0, 0, 0
            };
            A[2] = new double[4] {
                -1.0 / 3.0, 1, 0, 0
            };
            A[3] = new double[4] {
                1, -1, 1, 0
            };

            return(CreateMethod(C, B, A, f));
        }
Example #2
0
        public static MethodRK GetMethod_Sympson_P3S3(VectorFunk f)
        {
            var C = new double[3];

            C[0] = 0;
            C[1] = 1.0 / 2.0;
            C[2] = 1;
            var B = new double[3];

            B[0] = 1.0 / 6.0;
            B[1] = 4.0 / 6.0;
            B[2] = 1.0 / 6.0;
            var A = new double[3][];

            A[0] = new double[3] {
                0, 0, 0
            };
            A[1] = new double[3] {
                1.0 / 2.0, 0, 0
            };
            A[2] = new double[3] {
                -1, 2, 0
            };

            return(CreateMethod(C, B, A, f));
        }
Example #3
0
        public static MethodRK CreateMethod(double[] C, double[] B, double[][] A, VectorFunk f)
        {
            if (C.Length != B.Length || B.Length != A.Length)
            {
                throw new Exception("Not correct size!");
            }
            int s = B.Length;             //шаги

            return((double X0, double[] Y0, double h) =>
            {
                var beginVector = new Vector(Y0);

                var K = new Vector[s];
                K[0] = new Vector(f(X0, Y0));

                var result = B[0] * new Vector(K[0].data);
                for (int i = 1; i < s; i++)
                {
                    var tempX = X0 + C[i] * h;
                    var tempY = new Vector(beginVector.data);
                    for (int j = 0; j < i; j++)
                    {
                        tempY += h * A[i][j] * K[j];
                    }
                    K[i] = new Vector(f(tempX, tempY.data));

                    result += B[i] * K[i];
                }
                return result;
            });
        }
Example #4
0
        public static MethodRK GetMethod_Hoyna_P3S3(VectorFunk f)
        {
            var C = new double[3];

            C[0] = 0;
            C[1] = 1.0 / 3.0;
            C[2] = 2.0 / 3.0;
            var B = new double[3];

            B[0] = 1.0 / 4.0;
            B[1] = 0;
            B[2] = 1.0 / 4.0;
            var A = new double[3][];

            A[0] = new double[3] {
                0, 0, 0
            };
            A[1] = new double[3] {
                1.0 / 3.0, 0, 0
            };
            A[2] = new double[3] {
                0, 2.0 / 3.0, 0
            };

            return(CreateMethod(C, B, A, f));
        }
Example #5
0
        public static MethodRK GetMethod_Trapeze_P2S2(VectorFunk f)
        {
            var C = new double[2];

            C[0] = 0;
            C[1] = 1;
            var B = new double[2];

            B[0] = 0.5;
            B[1] = 0.5;
            var A = new double[2][];

            A[0] = new double[2] {
                0, 0
            };
            A[1] = new double[2] {
                1, 0
            };

            return(CreateMethod(C, B, A, f));
        }
Example #6
0
        public static MethodRK CreateMethod_byC2_P2S2(double c2, VectorFunk f)
        {
            var C = new double[2];

            C[0] = 0;
            C[1] = c2;
            var B = new double[2];

            B[1] = 1.0 / (2 * c2);
            B[0] = 1 - B[1];
            var A = new double[2][];

            A[0] = new double[2] {
                0, 0
            };
            A[1] = new double[2] {
                c2, 0
            };

            return(CreateMethod(C, B, A, f));
        }
Example #7
0
        static void Main(string[] args)
        {
            #region Условия
            double c2 = 0.05;

            double x0 = 0;
            double xN = 5;

            double A = 3;
            double B = 3;
            double C = -3;

            var Y0 = new double[4] {
                1, 1, A, 1
            };

            VectorFunk f = (double X, double[] Y) =>
            {
                var resultY = new double[Y.Length];
                resultY[0] = 2 * X * Math.Pow(Math.Abs(Y[1]), 1.0 / B) * Math.Sign(Y[1]) * Y[3];
                resultY[1] = 2 * B * X * Math.Exp((B / C) * (Y[2] - A)) * Y[3];
                resultY[2] = 2 * C * X * Y[3];
                resultY[3] = -2 * X * Math.Log(Math.Abs(Y[0]));

                return(resultY);
            };

            var realFuncs = new ScalarFunk1[4];
            realFuncs[0] = (double X) => Math.Exp(Math.Sin(X * X));
            realFuncs[1] = (double X) => Math.Exp(B * Math.Sin(X * X));
            realFuncs[2] = (double X) => C *Math.Sin(X *X) + A;

            realFuncs[3] = (double X) => Math.Cos(X * X);
            #endregion
            VectorFunk1 valueInRealFunc = (double X) => new double[] { realFuncs[0](X), realFuncs[1](X), realFuncs[2](X), realFuncs[3](X) };

            //Создание методов
            int p_C2           = 2;
            int p_MP           = 2;
            var methodC2_rk    = OduCalculation.CreateMethod_byC2_P2S2(c2, f);
            var middlePoint_rk = OduCalculation.GetMethod_MiddlePoint_P2S2(f);


            #region Запуск на константном шаге
            int    k_const = 9;
            double h_const = 1 / Math.Pow(2, k_const);
            var    result_methodC2_ConstH = OduCalculation.Result_ConstH(x0, xN, Y0, methodC2_rk, h_const);
            var    result_methodMP_ConstH = OduCalculation.Result_ConstH(x0, xN, Y0, middlePoint_rk, h_const);

            //ExcelTool.GetInstance().Export_points_4Func("MethodC2_ConstH", result_methodC2_ConstH, valueInRealFunc);
            //ExcelTool.GetInstance().Export_points_4Func("MethodMP_ConstH", result_methodMP_ConstH, valueInRealFunc);
            #endregion

            #region Принт точек
            //for (int i = 0; i < result_methodC2.Length; i++)
            //{
            //	string tempPoint = $"({x0 + i * h}".Replace(',', '.') + ", " + $"{result_methodC2[i].data[0]})".Replace(',', '.');
            //	Console.Write($"{tempPoint}, ");
            //}
            #endregion

            #region Все итерации
            //Console.Write("\nx: ");
            //for (int i = 0; i < 4; i++)
            //{
            //	Console.Write($" y{i}");
            //}
            //Console.WriteLine("\nResult methodC2:");
            //for (int i = 0; i < result_methodC2.Length; i++)
            //{
            //	Console.Write($"\n{i} ({x0 + i * h}): ");
            //	result_methodC2[i].Show();
            //}
            #endregion


            #region Сравнение с реальным значением
            Console.WriteLine("\nDifferent methodC2 with real in last point:");
            (new Vector(valueInRealFunc(result_methodC2_ConstH.Last().FirstElement)) - result_methodC2_ConstH.Last().SecondElement).Show();
            Console.WriteLine("\nDifferent methodMP with real in last point:");
            (new Vector(valueInRealFunc(result_methodMP_ConstH.Last().FirstElement)) - result_methodMP_ConstH.Last().SecondElement).Show();
            #endregion

            #region Для графика зависимости нормы от длины шага
            int k_end       = 7;
            var Rs_methodC2 = OduCalculation.GetRs(x0, xN, Y0, methodC2_rk, k_end, p_C2);
            Console.WriteLine("\nRs methodC2: ");
            Rs_methodC2.Show1();
            var Rs_MiddlePoint = OduCalculation.GetRs(x0, xN, Y0, middlePoint_rk, k_end, p_MP);
            Console.WriteLine("Rs middlePoint: ");
            Rs_MiddlePoint.Show1();
            #endregion

            #region Нахождение h_opt по Рунге
            var tol      = 10e-5;
            var h_opt_c2 = OduCalculation.H_Opt(x0, xN, Y0, methodC2_rk, p_C2, tol);
            Console.Write($"\nh_opt_c2: {h_opt_c2}");
            var h_opt_mp = OduCalculation.H_Opt(x0, xN, Y0, middlePoint_rk, p_MP, tol);
            Console.Write($"\nh_opt_mp: {h_opt_mp}");

            var result_methodC2_check = OduCalculation.Result_ConstH(x0, xN, Y0, methodC2_rk, h_opt_c2);
            var R_methodC2_check      = (new Vector(valueInRealFunc(result_methodC2_check.Last().FirstElement)) - result_methodC2_check.Last().SecondElement).Norm();
            Console.WriteLine($"\nCheck R methodC2 with h_opt: {R_methodC2_check}");

            var result_methodMP_check = OduCalculation.Result_ConstH(x0, xN, Y0, methodC2_rk, h_opt_c2);
            var R_methodMP_check      = (new Vector(valueInRealFunc(result_methodMP_check.Last().FirstElement)) - result_methodMP_check.Last().SecondElement).Norm();
            Console.WriteLine($"Check R methodMP with h_opt: {R_methodMP_check}");

            var realRsOnX_methodC2_h_opt = OduCalculation.RealRsOnX(result_methodC2_check, valueInRealFunc);
            var realRsOnX_methodMP_h_opt = OduCalculation.RealRsOnX(result_methodMP_check, valueInRealFunc);

            //ExcelTool.GetInstance().Export_RealRsOnX("RsOnX_methodC2_hOpt", realRsOnX_methodC2_h_opt);
            //ExcelTool.GetInstance().Export_RealRsOnX("RsOnX_methodMP_hOpt", realRsOnX_methodMP_h_opt);
            #endregion

            #region Запуск на вариативном шаге
            var h_begin = 1 / Math.Pow(2, 6);
            //Выбор начального шага
            var result0 = new Vector(f(x0, Y0));
            var delta   = Math.Pow(1 / Math.Max(Math.Abs(x0), Math.Abs(xN)), p_C2 + 1) + Math.Pow(result0.Norm(), p_C2 + 1);
            //h_begin = Math.Pow(1e-5 / delta, 1.0 / (p_C2 + 1));

            var accH_methodC2        = new List <Pair <double, Pair <double, List <double> > > >();
            var result_methodC2_varH = OduCalculation.Result_VariableH(x0, xN, Y0, methodC2_rk, p_C2, h_begin, ref accH_methodC2);
            Console.WriteLine("\nDifferent methodC2 with real in last point:");
            (new Vector(valueInRealFunc(result_methodC2_varH.Last().FirstElement)) - result_methodC2_varH.Last().SecondElement).Show();

            var accH_methodMP        = new List <Pair <double, Pair <double, List <double> > > >();
            var result_methodMP_varH = OduCalculation.Result_VariableH(x0, xN, Y0, middlePoint_rk, p_C2, h_begin, ref accH_methodMP);
            Console.WriteLine("\nDifferent methodMP with real in last point:");
            (new Vector(valueInRealFunc(result_methodMP_varH.Last().FirstElement)) - result_methodMP_varH.Last().SecondElement).Show();

            //ExcelTool.GetInstance().Export_InfoHs("MethodC2_InfoHs", accH_methodC2);
            //ExcelTool.GetInstance().Export_InfoHs("MethodMP_InfoHs", accH_methodMP);

            //ExcelTool.GetInstance().Export_points_4Func("MethodC2_VarH", result_methodC2_varH, valueInRealFunc);
            //ExcelTool.GetInstance().Export_points_4Func("MethodMP_VarH", result_methodMP_varH, valueInRealFunc);

            var realRsOnX_methodC2_h_var = OduCalculation.RealRsOnX(result_methodC2_varH, valueInRealFunc);
            var realRsOnX_methodMP_h_var = OduCalculation.RealRsOnX(result_methodMP_varH, valueInRealFunc);

            //ExcelTool.GetInstance().Export_RealRsOnX("RsOnX_methodC2_hVar", realRsOnX_methodC2_h_var);
            //ExcelTool.GetInstance().Export_RealRsOnX("RsOnX_methodMP_hVar", realRsOnX_methodMP_h_var);

            var countCallF_onRtol_methodC2 = OduCalculation.CountCallF_onRtol(x0, xN, Y0, methodC2_rk, p_C2, 2);
            var countCallF_onRtol_methodMP = OduCalculation.CountCallF_onRtol(x0, xN, Y0, middlePoint_rk, p_C2, 2);

            //ExcelTool.GetInstance().Export_CountCallF("CountCallF_methodC2", countCallF_onRtol_methodC2);
            //ExcelTool.GetInstance().Export_CountCallF("CountCallF_methodMP", countCallF_onRtol_methodMP);
            #endregion
        }