public static double MathApprox_4_Compute_2(double arg, double error) { int L = 1; int M = 1; //finding the appropriate L and M for the approximation to meet the adjusted error for (;;) { if (error > CMathApprox_4.MathApprox_4_2_CheckError(L, M)) { break; } M++; L++; } double[] C = new double[L + M + 1]; int p = 0; C[0] = Math.PI / 2; for (int i = 1; i < L + M + 1; i++) { if ((i % 2) == 0) { C[i] = 0; } else { C[i] = 1.0 / i * Math.Pow(-1, p + 1); p++; } } double[] Numer = new double[L + 1]; double[] Denom = new double[M + 1]; CFind_Pade.Find_Denominator(Denom, C, L, M); CFind_Pade.Find_Numerator(Numer, Denom, C, L, M); double P0 = 0; double P1 = 0; for (int j = 0; j < M + 1; j++) { double Tmp = Math.Pow(arg, j); if (j <= L) { P0 += Numer[j] * Tmp; } P1 += Denom[j] * Tmp; } double Res = P0 / P1; return(Res); }
public static double MathApprox_4_2_CheckError(int L, int M) { double[] C = new double[L + M + 1]; int p = 0; C[0] = Math.PI / 2; for (int i = 1; i < L + M + 1; i++) { if ((i % 2) == 0) { C[i] = 0; } else { C[i] = 1.0 / i * Math.Pow(-1, p + 1); p++; } } double[] Numer = new double[L + 1]; double[] Denom = new double[M + 1]; CFind_Pade.Find_Denominator(Denom, C, L, M); CFind_Pade.Find_Numerator(Numer, Denom, C, L, M); double error = 0; Random rnd = new Random(); for (int i = 0; i < 10000; i++) { double arg = 0.001 * rnd.Next(500); double P0 = 0; double P1 = 0; for (int j = 0; j < M + 1; j++) { double Tmp = Math.Pow(arg, j); if (j <= L) { P0 += Numer[j] * Tmp; } P1 += Denom[j] * Tmp; } double Res = P0 / P1; double Temp = Math.Abs((Math.PI / 2 - Math.Atan(arg)) - Res); if (error < Temp) { error = Temp; } } return(error); }
public static double MathApprox_1_Compute_2(double arg, double error) { int L = 0; int M = 1; //finding the appropriate L and M for the approximation to meet the adjusted error for (;;) { if (error > CMathApprox_1.MathApprox_1_2_CheckError(L, M)) { break; } if (L == M) { M++; } else { L++; } } //creating suitable approximation; //Ln_Numer is the numerator, Ln_Denom is the denominator, C is the Taylor approximation double[] C = new double[L + M + 1]; C[0] = 0; for (int i = 1; i < L + M + 1; i++) { C[i] = 1.0 / i * Math.Pow(-1, i + 1); } double[] Numer = new double[L + 1]; double[] Denom = new double[M + 1]; CFind_Pade.Find_Denominator(Denom, C, L, M); CFind_Pade.Find_Numerator(Numer, Denom, C, L, M); double P0 = 0; double P1 = 1; for (int j = 1; j < M + 1; j++) { double Tmp = Math.Pow(arg, j); if (j <= L) { P0 += Numer[j] * Tmp; } P1 += Denom[j] * Tmp; } double Res = P0 / P1; return(Res); }
public static double MathApprox_3_2_CheckError(int L, int M) { double[] C = new double[L + M + 1]; C[0] = 0; C[1] = 1; for (int i = 2; i < L + M + 1; i++) { if ((i % 2) == 0) { C[i] = 0; } else { C[i] = C[i - 2] * (i - 2) * (i - 2) / i / (i - 1); } } double[] Numer = new double[L + 1]; double[] Denom = new double[M + 1]; CFind_Pade.Find_Denominator(Denom, C, L, M); CFind_Pade.Find_Numerator(Numer, Denom, C, L, M); double error = 0; Random rnd = new Random(); for (int i = 0; i < 10000; i++) { double arg = 0.001 * rnd.Next(500); double P0 = 0; double P1 = 1; for (int j = 1; j < M + 1; j++) { double Tmp = Math.Pow(arg, j); if (j <= L) { P0 += Numer[j] * Tmp; } P1 += Denom[j] * Tmp; } double Res = P0 / P1; double Temp = Math.Abs(Math.Asin(arg) - Res); if (error < Temp) { error = Temp; } } return(error); }
public static double MathApprox_1_2_CheckError(int L, int M) { if ((L == 1) && (M == 2)) { return(0.005); } double[] C = new double[L + M + 1]; C[0] = 0; for (int i = 1; i < L + M + 1; i++) { C[i] = 1.0 / i * Math.Pow(-1, i + 1); } double[] Numer = new double[L + 1]; double[] Denom = new double[M + 1]; CFind_Pade.Find_Denominator(Denom, C, L, M); CFind_Pade.Find_Numerator(Numer, Denom, C, L, M); double error = 0; Random rnd = new Random(); for (int i = 0; i < 10000; i++) { double arg = 0.001 * rnd.Next(1000); double P0 = 0; double P1 = 1; for (int j = 1; j < M + 1; j++) { double Tmp = Math.Pow(arg, j); if (j <= L) { P0 += Numer[j] * Tmp; } P1 += Denom[j] * Tmp; } double Res = P0 / P1; double Temp = Math.Abs(Math.Log(arg + 1) - Res); if (error < Temp) { error = Temp; } } return(error); }
public static string MathApprox_1_2(string PathName, double error) { int L = 0; int M = 1; //finding the appropriate L and M for the approximation to meet the adjusted error for (;;) { if (error > MathApprox_1_2_CheckError(L, M)) { break; } if (L == M) { M++; } else { L++; } } //creating suitable approximation; //Ln_Numer is the numerator, Ln_Denom is the denominator, C is the Taylor approximation double[] C = new double[L + M + 1]; C[0] = 0; for (int i = 1; i < L + M + 1; i++) { C[i] = 1.0 / i * Math.Pow(-1, i + 1); } double[] Numer = new double[L + 1]; double[] Denom = new double[M + 1]; CFind_Pade.Find_Denominator(Denom, C, L, M); CFind_Pade.Find_Numerator(Numer, Denom, C, L, M); //creating program with found approximation string FileName = PathName + "MathApprox_1_2" + "_" + error.ToString() + ".cs"; System.IO.StreamWriter FileWriter = CMakeProgram.MakeBeginning(PathName, "MathApprox_1_2", error); //!!! //Необходимо заменить Ln_Numer и Ln_Denom на реально полученные коэффициенты !!! FileWriter.WriteLine(" double P0 = 0;"); FileWriter.WriteLine(" double P1 = 1;"); FileWriter.WriteLine(" double[] Denom = new double[" + (M + 1).ToString() + "];"); FileWriter.WriteLine(" double[] Numer = new double[" + (L + 1).ToString() + "];"); for (int i = 0; i < M + 1; i++) { FileWriter.WriteLine(" Denom[" + i.ToString() + "] = " + Denom[i].ToString().Replace(",", ".") + ";"); } for (int i = 0; i < L + 1; i++) { FileWriter.WriteLine(" Numer[" + i.ToString() + "] = " + Numer[i].ToString().Replace(",", ".") + ";"); } FileWriter.WriteLine(" Console.Write(\"Input argument for ln(1 + x): \");"); FileWriter.WriteLine(" double arg = double.Parse(Console.ReadLine());"); FileWriter.WriteLine(" for (int j = 1; j < " + (M + 1).ToString() + "; j++)"); FileWriter.WriteLine(" {"); FileWriter.WriteLine(" double Tmp = Math.Pow(arg, j);"); FileWriter.WriteLine(" P0 += Numer[j] * Tmp;"); FileWriter.WriteLine(" P1 += Denom[j] * Tmp;"); FileWriter.WriteLine(" }"); FileWriter.WriteLine(" double Res = P0 / P1;"); FileWriter.WriteLine(" "); FileWriter.WriteLine(" Console.Write(\"Ln(1 + \" + arg.ToString() + \") = \");"); FileWriter.WriteLine(" Console.WriteLine(Res);"); FileWriter.WriteLine(" Console.Write(\"Target value: \" + Math.Log(arg + 1).ToString());"); FileWriter.WriteLine(" Console.WriteLine();"); FileWriter.WriteLine(" }"); FileWriter.WriteLine(" }"); FileWriter.WriteLine("}"); FileWriter.Close(); return(FileName); }