Beispiel #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(MathFunctions.DoubleUnaryFunction f, double a, double b)
        {
            if (a > b)
            {
                return(-Integrate(f, b, a));
            }

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

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

            return(sum);
        }
Beispiel #2
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(MathFunctions.DoubleUnaryFunction 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; i < 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]);
        }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DoubleUnaryFunction"/> class.
 /// </summary>
 /// <param name="f">A function delegate that takes a double value as a parameter and returns a double value.</param>
 /// <param name="d">The <see cref="IDifferentiator"/> to use.</param>
 /// <param name="i">The <see cref="IIntegrator"/> to use.</param>
 public DoubleUnaryFunction(MathFunctions.DoubleUnaryFunction f, IDifferentiator d, IIntegrator i)
 {
     _function       = f;
     _differentiator = d;
     _integrator     = i;
 }
Beispiel #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DoubleUnaryFunction"/> class.
 /// </summary>
 /// <param name="f">
 /// A function delegate that takes a double value as a parameter and returns a double value.
 /// </param>
 public DoubleUnaryFunction(MathFunctions.DoubleUnaryFunction f)
 {
     _function       = f;
     _differentiator = null;
     _integrator     = null;
 }
Beispiel #5
0
 public OneVariableFunction(MathFunctions.DoubleUnaryFunction function)
 {
     _function = function;
 }