Пример #1
0
        /// <summary>
        /// 実係数n次多項式p(x)の定積分 ∫_a^b{p(x)dx}を求める。
        /// </summary>
        /// <param name="p">実係数n次多項式p(x)。</param>
        /// <param name="a">区間a。</param>
        /// <param name="b">区間b。</param>
        /// <returns>∫_a^b{p(x)dx}</returns>
        public double Calc(RealPolynomial p, double a, double b)
        {
            // xの積分区間 [a b]
            // ξの積分区間 [-1 +1]

            double flipInterval = 1;

            if (b < a)
            {
                // swap a and b
                double t = a;
                a            = b;
                b            = t;
                flipInterval = -1;
            }

            int np     = NumberOfEvalPoints(p);
            var ξwList = GetξwList(np);

            // 定積分結果値r。
            double r = 0;

            for (int k = 0; k < np; ++k)
            {
                var ξ = ξwList[k].ξ;
                var w = ξwList[k].w;

                double x = ((a + b) + ξ * (b - a)) / 2.0;

                // 地点xでpを評価:p(x)を求める。
                double px = p.Evaluate(x);

                r += w * px;
            }

            // 積分変数をx→ξに置き換えたことによるスケーリング。
            r *= (b - a) / 2.0;

            r *= flipInterval;

            return(r);
        }
Пример #2
0
        /// <summary>
        /// Find one real root of specified real poly
        /// </summary>
        /// <param name="coeffs">poly coeffs. coef[0]: constant, coef[1] 1st degree coeff</param>
        /// <param name="initialX">initial estimate of the root position</param>
        /// <returns>p</returns>
        public static double FindRoot(RealPolynomial rpoly, double initialX, double kEpsilon, int loopCount)
        {
            var deriv = rpoly.Derivative();

            double prev = initialX;
            double x    = initialX;

            for (int i = 0; i < loopCount; ++i)
            {
                double f      = rpoly.Evaluate(x);
                double fPrime = deriv.Evaluate(x);
                x = x - f / fPrime;

                if (Math.Abs(prev - x) < kEpsilon)
                {
                    break;
                }

                prev = x;
            }

            return(x);
        }