public void Export_points_4Func(string nameTable, Pair <double, Vector>[] points, VectorFunk1 realValues) { using (ExcelPackage excel = new ExcelPackage(excelFile)) { //Создание листа if (excel.Workbook.Worksheets[nameTable] != null) { excel.Workbook.Worksheets.Delete(nameTable); } excel.Workbook.Worksheets.Add(nameTable); var excelWorksheet = excel.Workbook.Worksheets[nameTable]; //Добавление заголовка List <string[]> headerRow = new List <string[]>(); var row = new string[9] { "X", "Y1_calc", "Y1_real", "Y2_calc", "Y2_real", "Y3_calc", "Y3_real", "Y4_calc", "Y4_real" }; headerRow.Add(row); string headerRange = "A1:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + "1"; excelWorksheet.Cells[headerRange].LoadFromArrays(headerRow); //Добавление содержимого var cellData = new List <object[]>(); for (int i = 0; i < points.Length; i++) { var realValuesVector = realValues(points[i].FirstElement); var rowValues = new object[9] { points[i].FirstElement, points[i].SecondElement.data[0], realValuesVector[0], points[i].SecondElement.data[1], realValuesVector[1], points[i].SecondElement.data[2], realValuesVector[2], points[i].SecondElement.data[3], realValuesVector[3] }; cellData.Add(rowValues); } excelWorksheet.Cells[2, 1].LoadFromArrays(cellData); //Сохранение excel.Save(); } }
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 }
public static Pair <double, double>[] RealRsOnX(Pair <double, Vector>[] points, VectorFunk1 realValues) { var result = new Pair <double, double> [points.Length]; for (int i = 0; i < points.Length; i++) { result[i] = new Pair <double, double> { FirstElement = points[i].FirstElement, SecondElement = (new Vector(realValues(points[i].FirstElement)) - points[i].SecondElement).Norm() }; } return(result); }