Trapezoidal rule for numerical integration.

In numerical analysis, the trapezoidal rule (also known as the trapezoid rule or trapezium rule) is a technique for approximating the definite integral ∫_a^b(x) dx. The trapezoidal rule works by approximating the region under the graph of the function f(x) as a trapezoid and calculating its area. It follows that ∫_a^b(x) dx ~ (b - a) [f(a) - f(b)] / 2.

References: Wikipedia, The Free Encyclopedia. Trapezoidal rule. Available on: http://en.wikipedia.org/wiki/Trapezoidal_rule

Inheritance: INumericalIntegration, IUnivariateIntegration
示例#1
0
        /// <summary>
        ///   Computes the area of the function under the selected <see cref="Range"/>.
        ///   The computed value will be available at this object's <see cref="Area"/>.
        /// </summary>
        ///
        /// <returns>
        ///   True if the integration method succeeds, false otherwise.
        /// </returns>
        ///
        public bool Compute()
        {
            for (int i = 0; i < s.Length; i++)
            {
                s[i] = 1;
            }

            double sum = 0;
            double a   = range.Min;
            double b   = range.Max;

            for (int k = 0; k < s.Length; k++)
            {
                sum  = s[0];
                s[0] = TrapezoidalRule.Integrate(Function, a, b, 1 << k);

                for (int i = 1; i <= k; i++)
                {
                    int p = (int)Math.Pow(4, i);
                    s[k] = (p * s[i - 1] - sum) / (p - 1);

                    sum  = s[i];
                    s[i] = s[k];
                }
            }

            Area = s[s.Length - 1];

            return(true);
        }
示例#2
0
        /// <summary>
        ///   Creates a new object that is a copy of the current instance.
        /// </summary>
        ///
        /// <returns>
        ///   A new object that is a copy of this instance.
        /// </returns>
        ///
        public object Clone()
        {
            TrapezoidalRule clone = new TrapezoidalRule(
                this.Steps, this.Function,
                this.Range.Min, this.Range.Max);

            return(clone);
        }
        /// <summary>
        ///   Creates a new object that is a copy of the current instance.
        /// </summary>
        /// 
        /// <returns>
        ///   A new object that is a copy of this instance.
        /// </returns>
        /// 
        public object Clone()
        {
            TrapezoidalRule clone = new TrapezoidalRule(
                this.Steps, this.Function,
                this.Range.Min, this.Range.Max);

            return clone;
        }