Пример #1
0
        public static void applyMethod(Func <double, double> f, double eps, double a, double b)
        {
            bool isAImmovable = f(a) * Auxiliary.getDerivativeValue(f, 2, a) < 0;

            int    iteration = 1;
            double c = 0, prevX = 0;

            do
            {
                prevX = c;
                c     = a - (f(a) * (b - a)) / (f(b) - f(a));

                if (isAImmovable)
                {
                    b = c;
                }
                else
                {
                    a = c;
                }

                Console.WriteLine(iteration + ": xє[" + a + "; " + b + "];");

                iteration++;
            } while (Math.Abs(c - prevX) > eps);

            Console.WriteLine("\nRoot: " + c + ".");
        }
Пример #2
0
        public static void applyMethod(Func <double, double> f, double a, double b, double eps)
        {
            double x = 0, prevX = 0;

            if ((f(a) * Auxiliary.getDerivativeValue(f, 2, a)) > 0)
            {
                x = a;
            }
            else
            {
                x = b;
            }
            int iteration = 1;

            if (Math.Abs(f(x)) < 5 * eps)
            {
                SteffensenMethod.applyMethod(f, x, prevX, eps, iteration);
                return;
            }

            Console.WriteLine("<------Newton Method------>");
            do
            {
                prevX = x;
                x     = x - f(x) / Auxiliary.getDerivativeValue(f, 1, x);
                Console.WriteLine(iteration + ": x_k = " + x);
                iteration++;
                if (Math.Abs(f(x)) < 5 * eps)
                {
                    SteffensenMethod.applyMethod(f, x, prevX, eps, iteration);
                    return;
                }
            } while (Math.Abs(x - prevX) > eps);

            Console.WriteLine("\nRoot is " + x + ".");
        }