Пример #1
0
        public HalfDeviation(double a, double b, Func <double, double> f)
        {
            Console.WriteLine("\n\n\nМетод деления пополам: ");
            var iterationNumber = Math.Ceiling(Math.Log((3 - 2) / Math.Pow(10, -5), 2) - 1);

            Console.WriteLine($"Итераций: {iterationNumber}");

            PrintTable.PrintRow("n", "an", "xn", "bn", "f(an)", "f(xn)", "f(bn)");

            double x = (a + b) / 2;
            double fa = 0, fx = 0, fb = 0;

            do
            {
                fa = f(a);
                fx = f(x);
                fb = f(b);

                PrintTable.PrintRow(iterationNumber, a, x, b, fa, fx, fb);

                if (fx == 0)
                {
                    break;
                }

                a = SameSign(fa, fx) ? x : a;
                b = SameSign(fb, fx) ? x : b;
                x = (a + b) / 2;
            } while (iterationNumber-- > 0);

            Console.WriteLine($"Х є околу: {x}");
        }
Пример #2
0
        public Newton(double a, double b, double eps, Func <double, Double> f, Func <double, double> f1, Func <double, double> f2)
        {
            Console.WriteLine("\n\n\nМетод Ньютона");
            Console.WriteLine($"f'({a}) = {f1(a)}");
            Console.WriteLine($"f'({b}) = {f1(b)}");

            double m1 = Math.Min(f1(a), f1(b));

            Console.WriteLine($"m1 = min{{f'({a}); f'({b})}} = {m1}");
            Console.WriteLine();
            Console.WriteLine($"f''({a}) = {f2(a)}");
            Console.WriteLine($"f''({b}) = {f2(b)}");

            double M2 = Math.Max(f2(a), f2(b));

            Console.WriteLine($"M1 = max{{f''({a}); f''({b})}} = {M2}");

            double delta = Math.Sqrt(2 * m1 * eps / M2);

            Console.WriteLine($"Delta = {delta}");

            var x = a;

            if (f(x) * f2(x) <= 0)
            {
                x = b;
            }

            while (f(x) * f2(x) <= 0)
            {
                x -= eps;
            }

            PrintTable.PrintRow("n", "Xn", "|Xn-X(n-1)|", "|Xn-X(n-1)| < delta");
            PrintTable.PrintRow(0, x, "-", "-");

            double xPrev;
            var    i = 1;

            do
            {
                xPrev = x;
                x     = xPrev - f(x) / f1(x);

                PrintTable.PrintRow(i, x, Math.Abs(x - xPrev), Math.Abs(x - xPrev) < delta);
                i++;
            } while (Math.Abs(x - xPrev) >= delta);

            Console.WriteLine($"Х є околу: {x}");
        }
Пример #3
0
        /// <summary>
        /// Метод простой итерации
        /// </summary>
        /// <param name="a">Левая граница</param>
        /// <param name="b">Правая граница</param>
        /// <param name="f">Функция</param>
        /// <param name="f1">Первая производная</param>
        public SimpleIteration(double a, double b, double eps, Func <double, Double> f, Func <double, double> f1)
        {
            Console.WriteLine("\n\n\nМетод простой итерации");
            // Переходим от заданого уравнения к уравнению удобному для итерирования: x = x - alpha*f(x)
            var M1 = Math.Max(f1(a), f1(b));
            var m1 = Math.Min(f1(a), f1(b));

            if (f1(a) > f1(b))
            {
                M1 = f1(a);
                m1 = f1(b);
                Console.WriteLine($"M1 = f({a}) = {f1(a)}");
                Console.WriteLine($"m1 = f({b}) = {f1(b)}");
            }
            else
            {
                M1 = f1(b);
                m1 = f1(a);
                Console.WriteLine($"M1 = f({b}) = {f1(b)}");
                Console.WriteLine($"m1 = f({a}) = {f1(a)}");
            }

            var alpha = 2 / (M1 + m1);

            Console.WriteLine($"Alpha = {alpha}");

            double x         = a;
            double stopValue = Math.Abs(f(x)) / m1;
            int    i         = 0;

            PrintTable.PrintRow("n", "x", "|f(xn)|/m1", "epsilon", "Stop");
            while (stopValue > eps)
            {
                PrintTable.PrintRow(i, x, Math.Abs(f(x)) / m1, eps, stopValue < eps);
                x         = x - alpha * f(x);
                stopValue = Math.Abs(f(x)) / m1;

                i++;
            }

            PrintTable.PrintRow(i, x, Math.Abs(f(x)) / m1, eps, stopValue < eps);

            Console.WriteLine($"Х є околу: {x}");
        }
Пример #4
0
        public Chords(double a, double b, double eps, Func <double, Double> f, Func <double, double> f1, Func <double, double> f2)
        {
            Console.WriteLine("\n\n\nМетод хорд");
            Console.WriteLine($"f'({a}) = {f1(a)}");
            Console.WriteLine($"f'({b}) = {f1(b)}");
            var M1 = Math.Max(f1(a), f1(b));
            var m1 = Math.Min(f1(a), f1(b));

            Console.WriteLine($"M1 = {M1}");
            Console.WriteLine($"m1 = {m1}");

            var gamma = m1 * eps / (M1 - m1);

            Console.WriteLine($"Gamma = {gamma}");

            var x = a;

            // while (f(x) * f2(x) >= 0)
            // {
            x = x + eps;
            // }

            PrintTable.PrintRow("n", "Xn", "|Xn-X(n-1)|", "|Xn-X(n-1)| < gamma");
            PrintTable.PrintRow(0, x, "-", "-");

            double prevX;
            int    i = 1;

            do
            {
                prevX = x;
                x     = prevX - f(prevX) * (3 - prevX) / (f(3) - f(prevX));
                PrintTable.PrintRow(i, x, Math.Abs(x - prevX), Math.Abs(x - prevX) < gamma);

                i++;
            } while (Math.Abs(x - prevX) >= gamma);

            Console.WriteLine($"Х є околу: {x}");
        }