Exemplo n.º 1
0
        /// <summary>
        /// Perform one iteration of searching for a root with Newton-Raphson method
        /// </summary>
        /// <param name="f"></param>
        /// <param name="df"></param>
        /// <param name="value"></param>
        /// <param name="precision"></param>
        /// <returns></returns>
        private static ComplexNumber NewtonIter(FastExpression f, FastExpression df, ComplexNumber value, int precision)
        {
            ComplexNumber prev          = value;
            int           minCheckIters = (int)Math.Sqrt(precision);

            for (int i = 0; i < precision; i++)
            {
                if (i == precision - 1)
                {
                    prev = value.Copy();
                }
                try // TODO: remove try catch in for
                {
                    value -= (f.Substitute(value) / df.Substitute(value)) as ComplexNumber;
                }
                catch (MathSException)
                {
                    throw new MathSException("Two or more variables in SolveNt is forbidden");
                }
                if (i > minCheckIters && prev == value)
                {
                    return(value);
                }
            }
            if (Number.Abs(prev - value) > MathS.Settings.PrecisionErrorCommon)
            {
                return(RealNumber.NaN());
            }
            else
            {
                return(value);
            }
        }