示例#1
0
        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);
        }
示例#2
0
        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);
        }