Beispiel #1
0
        public static double Newton(double eps, double left, double right, DelF Func)
        {
            var curr = left + (right - left) / 2;

            bool   flag     = false;
            double nextCurr = 0;
            double incr     = 0;
            double delta;

            do
            {
                if (flag)
                {
                    curr = nextCurr;
                }

                double der = (Func(curr + eps / 2) - Func(curr)) / (eps / 2);
                nextCurr = curr - Func(curr) / der;
                delta    = Math.Abs(nextCurr - curr);

                if ((delta <= incr) || !flag)
                {
                    incr = delta;
                }
                else
                {
                    return(Double.NaN);
                }

                flag = true;
            } while (delta > eps);
            return(nextCurr);
        }
Beispiel #2
0
        public static double PolDel(double eps, double left, double right, DelF func)
        {
            var    length = right - left;
            var    error  = length;
            double Fmin   = func(left);
            double Fmax   = func(right);

            if (Fmin * Fmax > 0)
            {
                return(Double.NaN);
            }
            while (error > eps)
            {
                double x  = (left + right) / 2;
                double Fx = func(x);
                if (Fmin * Fx < 0)
                {
                    right = x;
                }
                else
                {
                    left = x;
                    Fmin = Fx;
                }

                error = (right - left);
            }
            return((left + right) / 2);
        }
        public static double Iteration(double left, double right, double eps, DelF Func)
        {
            int    max_iter = 1000;
            double x1       = left + (right - left) / 2;
            double x0;

            int iterations = 0;

            do
            {
                x0 = x1;

                var M = -(Func(x1 + eps) - Func(x1 - eps)) / (2 * eps);
                x1 = x0 + Func(x0) / M;

                iterations++;
            } while ((Math.Abs(x1 - x0) >= eps) || (iterations > max_iter));

            if (Math.Abs(x1 - x0) <= eps)
            {
                return(x1);
            }
            else
            {
                return(double.NaN);
            }
        }