Exemple #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 float Integrate(MathFunctions.FloatUnaryFunction f, float a, float b)
        {
            if (a > b)
            {
                return(-Integrate(f, b, a));
            }

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

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

            return(sum);
        }
Exemple #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 float Integrate(MathFunctions.FloatUnaryFunction f, float a, float b)
        {
            // Check the _romD field is initialized correctly.
            if ((_romF == null) || (_romF.GetLength(1) == _order))
            {
                _romF = new float[1, _order];
            }

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

            float h = (b - a);

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

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

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

            return(_romF[0, _order - 1]);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="FloatUnaryFunction"/> class.
 /// </summary>
 /// <param name="f">A function delegate that takes a float value as a parameter and returns a float value.</param>
 /// <param name="d">The <see cref="IDifferentiator"/> to use.</param>
 /// <param name="i">The <see cref="IIntegrator"/> to use.</param>
 public FloatUnaryFunction(MathFunctions.FloatUnaryFunction f, IDifferentiator d, IIntegrator i)
 {
     _function       = f;
     _differentiator = d;
     _integrator     = i;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FloatUnaryFunction"/> class.
 /// </summary>
 /// <param name="f">
 /// A function delegate that takes a float value as a parameter and returns a float value.
 /// </param>
 public FloatUnaryFunction(MathFunctions.FloatUnaryFunction f)
 {
     _function       = f;
     _differentiator = null;
     _integrator     = null;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FloatUnaryFunction"/> class.
 /// </summary>
 /// <param name="f">A function delegate that takes a float value as a parameter and returns a float value.</param>
 /// <param name="d">The <see cref="IDifferentiator"/> to use.</param>
 /// <param name="i">The <see cref="IIntegrator"/> to use.</param>
 public FloatUnaryFunction(MathFunctions.FloatUnaryFunction f, IDifferentiator d, IIntegrator i)
 {
     _function = f;
     _differentiator = d;
     _integrator = i;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FloatUnaryFunction"/> class.
 /// </summary>
 /// <param name="f">
 /// A function delegate that takes a float value as a parameter and returns a float value.
 /// </param>
 public FloatUnaryFunction(MathFunctions.FloatUnaryFunction f)
 {
     _function = f;
     _differentiator = null;
     _integrator = null;
 }