public static double Integrate(Func <double, double> func, double a, double b, double alpha, double beta, int n) { var Mu = Mathematics.GetMuArray(n, a, b, alpha, beta); double[] X = new double[n]; double h = (b - a) / (n - 1); for (int i = 0; i < n; i++) { X[i] = a + h * i; } double[,] matrix = new double[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { matrix[i, j] = Math.Pow(X[j], i); } } var A = Mathematics.GausMethod(matrix, Mu); double integralSum = 0; for (int i = 0; i < n; i++) { integralSum += func(X[i]) * A[i]; } return(integralSum); }
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); }
public static double Integrate(Func <double, double> func, double a, double b, double alpha, double beta, int n) { var Mu = Mathematics.GetMuArray(2 * n, a, b, alpha, beta); var SLAE = new double[n, n]; var bSLAE = new double[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { SLAE[i, j] = Mu[j + i]; } bSLAE[i] = -Mu[n + i]; } var acoefs = Mathematics.GausMethod(SLAE, bSLAE); var polcoefs = new double[acoefs.Length + 1]; for (int i = 0; i < polcoefs.Length - 1; i++) { polcoefs[i] = acoefs[i]; } polcoefs[polcoefs.Length - 1] = 1; var nodes = GetSquarePolynomialRoots(polcoefs); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { SLAE[i, j] = Math.Pow(nodes[j], i); } bSLAE[i] = Mu[i]; } var intcoefs = Mathematics.GausMethod(SLAE, bSLAE); double result = 0; for (int i = 0; i < n; i++) { result += intcoefs[i] * func(nodes[i]); } return(result); }