public static double GetOptStepRichardson(Func <Func <double, double>, double, double, double, double, int, int, double> method, Func <double, double> func, double a, double b, double alpha, double beta, int n, int r, int ada, double epsilon) { double startStep = (b - a); double[,] A = new double[r + 1, r + 1]; double[] B = new double[r + 1]; double[] X = new double[r + 1]; do { for (int i = 0; i < A.GetLength(0); i++) { double step = startStep / Math.Pow(2, i); for (int j = 0; j < A.GetLength(0); j++) { if (j == 0) { A[i, j] = -1; } else { A[i, j] = Math.Pow(step, ada + j - 1); } } B[i] = -method(func, a, b, alpha, beta, n, Mathematics.GetGoodNum(a, b, step)); } X = Mathematics.GausMethod(A, B); startStep /= 2; } while (Math.Abs(X[0] + B[r]) > epsilon); return(startStep / Math.Pow(2, r - 1)); }
static void Main(string[] args) { Stopwatch SW = new Stopwatch(); double accRunge; double richOptStep; int segmNum = 100; double integralSum; int n; double a = 1.1d; double b = 2.5d; double alpha = 0.4d; double beta = 0; SW.Start(); SW.Stop(); //SW.Restart(); //n = 2; //integralSum = GaussMethod.CompoundIntegrate(Function, a, b, alpha, beta, n, segmNum); //SW.Stop(); //output("CQF (Gauss)", integralSum, n, SW.ElapsedTicks); //richOptStep = Mathematics.GetOptStepRichardson(GaussMethod.CompoundIntegrate, Function, a, b, alpha, beta, n, 2, n - 1, 1e-6); //Console.WriteLine("Optimal step (Richardson): " + richOptStep); //Console.WriteLine(" => Optimal n: " + Mathematics.GetGoodNum(a, b, richOptStep)); SW.Restart(); n = 3; integralSum = NewtonCotesMethod.CompoundIntegrate(Function, a, b, alpha, beta, n, segmNum); SW.Stop(); output("CQF (Newton-Cotes)", integralSum, n, SW.ElapsedTicks); accRunge = Mathematics.AccuracyRunge(integralSum, NewtonCotesMethod.CompoundIntegrate(Function, a, b, alpha, beta, n, segmNum * 2), n - 1); var optStep = Mathematics.GetOptStep(a, b, segmNum, accRunge, n - 1, 1e-6); Console.WriteLine("Optimal step (Runge): " + optStep); Console.WriteLine(" => Optimal n: " + Mathematics.GetGoodNum(a, b, optStep)); richOptStep = Mathematics.GetOptStepRichardson(NewtonCotesMethod.CompoundIntegrate, Function, a, b, alpha, beta, n, 2, n - 1, 1e-6); Console.WriteLine("Optimal step (Richardson): " + richOptStep); Console.WriteLine(" => Optimal n: " + Mathematics.GetGoodNum(a, b, richOptStep)); Console.WriteLine("Error (Runge): " + accRunge); }