Esempio n. 1
0
        /// <summary>
        /// Integrates a given function within the given integral.
        /// </summary>
        /// <param name="f">The function to integrate.</param>
        /// <param name="a">The lower limit.</param>
        /// <param name="b">The higher limit.</param>
        /// <returns>
        /// The integral of <paramref name="function"/> over the interval from <paramref name="a"/> to <paramref name="b"/>
        /// </returns>
        public double Integrate(Sharp3D.Math.Core.MathFunctions.UnaryFunction <double> f, double a, double b)
        {
            if (a == b)
            {
                return(0);
            }

            if (a > b)
            {
                return(-Integrate(f, b, a));
            }

            // Start with the crudest estimate
            int    n           = 1;
            double estimate    = ((f(a) + f(b)) * (b - a)) / 2.0;
            double newEstimate = 0;

            int i = 1;

            do
            {
                switch (_method)
                {
                case Method.Default:
                    newEstimate = defaultIteration(f, a, b, estimate, n);
                    n          *= 2;
                    break;

                case Method.MidPoint:
                    newEstimate = midPointIteration(f, a, b, estimate, n);
                    n          *= 3;
                    break;
                }

                // Check accuracy
                if (System.Math.Abs(newEstimate - estimate) <= _accuracy)
                {
                    return(newEstimate);
                }

                estimate = newEstimate;
                i++;
            }while (i < _maxSteps);

            throw new MathException("Max number of iterations reached.");
        }
        /// <summary>
        /// Integrates a given function within the given integral.
        /// </summary>
        /// <param name="f">The function to integrate.</param>
        /// <param name="a">The lower limit.</param>
        /// <param name="b">The higher limit.</param>
        /// <returns>
        /// The integral of <paramref name="function"/> over the interval from <paramref name="a"/> to <paramref name="b"/>
        /// </returns>
        public double Integrate(Sharp3D.Math.Core.MathFunctions.UnaryFunction <double> f, double a, double b)
        {
            if (a > b)
            {
                return(-Integrate(f, b, a));
            }

            double sum          = 0;
            double stepSize     = (b - a) / _stepCount;
            double stepSizeDiv3 = stepSize / 3;

            for (int i = 0; i < _stepCount; i = i + 2)
            {
                sum += (f(a + i * stepSize) + 4 * f(a + (i + 1) * stepSize) + f(a + (i + 2) * stepSize)) * stepSizeDiv3;
            }

            return(sum);
        }
        /// <summary>
        /// Integrates a given function within the given integral.
        /// </summary>
        /// <param name="f">The function to integrate.</param>
        /// <param name="a">The lower limit.</param>
        /// <param name="b">The higher limit.</param>
        /// <returns>
        /// The integral of <paramref name="function"/> over the interval from <paramref name="a"/> to <paramref name="b"/>
        /// </returns>
        public float Integrate(Sharp3D.Math.Core.MathFunctions.UnaryFunction <float> f, float a, float b)
        {
            if (a > b)
            {
                return(-Integrate(f, b, a));
            }

            float sum          = 0;
            float stepSize     = (float)((b - a) / _stepCount);
            float stepSizeDiv3 = stepSize / 3.0f;

            for (int i = 0; i < _stepCount; i = i + 2)
            {
                sum += (f(a + i * stepSize) + 4.0f * f(a + (i + 1) * stepSize) + f(a + (i + 2) * stepSize)) * stepSizeDiv3;
            }

            return(sum);
        }
        /// <summary>
        /// Integrates a given function within the given integral.
        /// </summary>
        /// <param name="f">The function to integrate.</param>
        /// <param name="a">The lower limit.</param>
        /// <param name="b">The higher limit.</param>
        /// <returns>
        /// The integral of <paramref name="function"/> over the interval from <paramref name="a"/> to <paramref name="b"/>
        /// </returns>
        public double Integrate(Sharp3D.Math.Core.MathFunctions.UnaryFunction <double> f, double a, double b)
        {
            // Check the _romD field is initialized correctly.
            if ((_romD == null) || (_romD.GetLength(1) == _order))
            {
                _romD = new double[1, _order];
            }

            if (a > b)
            {
                return(Integrate(f, b, a));
            }

            double h = (b - a);

            _romD[0, 0] = 0.5 * h * (f(a) + f(b));
            for (int i = 2, ipower = 1; i <= _order; i++, ipower *= 2, h /= 2)
            {
                // Approximation using the trapezoid rule.
                double sum = 0;
                for (int j = 1; j <= ipower; j++)
                {
                    sum += f(a + h * (j - 0.5));
                }

                // Richardson extrapolation
                _romD[1, 0] = 0.5 * (_romD[0, 0] + (h * sum));
                for (int k = 1, kpower = 4; k < i; k++, kpower *= 4)
                {
                    _romD[1, k] = (kpower * _romD[1, k - 1] - _romD[0, k - 1]) / (kpower - 1);
                }

                // Save the extrapolated values for the next iteration
                for (int j = 0; j < i; j++)
                {
                    _romD[0, j] = _romD[1, j];
                }
            }

            return(_romD[0, _order - 1]);
        }