예제 #1
0
        /// <summary>
        /// Метод Хорд
        /// </summary>
        /// <param name="Function"></param>
        /// <param name="X0">x(n)</param>
        /// <param name="XLast">x(n-1) - not changed</param>
        /// <param name="Epsilon"></param>
        /// <returns>x(n+1)=y</returns>
        public double ChordMethod(string Function, double X0, double XLast, double Epsilon, ref string resultTEXT)
        {
            double y = X0 -
                       (X0 - XLast) * CalcMethods.ParseFunction(Function, X0) /
                       (CalcMethods.DerivativeOfFunction(Function, X0) -
                        CalcMethods.DerivativeOfFunction(Function, XLast));
            double k = 1;

            resultTEXT  = "Функция x = f(x) = " + Function + "\n";
            resultTEXT += "x0 = " + X0 + "  X1 = " + XLast + "   Epsilon =  " + Epsilon + "\n";
            while ((Math.Abs(y - X0) > Epsilon))
            {
                resultTEXT += "\n|у -x0| = " + (Math.Abs(y - X0));
                if (k > 500)
                {
                    throw new ArgumentException("k > 500");
                }
                resultTEXT += "\n y = " + y;
                resultTEXT += "\n k =" + k + "\n";
                k++;

                X0 = y;
                y  = X0 -
                     (X0 - XLast) * CalcMethods.ParseFunction(Function, X0) /
                     (CalcMethods.DerivativeOfFunction(Function, X0) - CalcMethods.DerivativeOfFunction(Function, XLast));
            }
            resultTEXT += "Результат =" + y + "\n";
            return(y);
        }
        /// <summary>
        /// Метод средних прямоугольников
        /// </summary>
        /// <param name="function">подинтегральная функция</param>
        /// <param name="a">начало отрезка</param>
        /// <param name="b">конец отрезка</param>
        /// <param name="n">количество разбиений</param>
        /// <returns></returns>
        public double MidleRectangles(string function, double a, double b, int n)
        {
            double S = 0;
            double h = (b - a) / n;

            for (int i = 0; i < n; i++)
            {
                S = S + h * (CalcMethods.ParseFunction(function, (a + (i + 0.5) * h)));
            }
            return(S);
        }
        /// <summary>
        /// метод левых прямоугольников
        /// </summary>
        /// <param name="function">подинтегральная функция</param>
        /// <param name="a">начало отрезка [a, b]</param>
        /// <param name="b">конец отрезка [a, b]</param>
        /// <param name="n">количество разбиений отрезка</param>
        /// <returns></returns>
        public double LeftRectangles(string function, double a, double b, int n)
        {
            double S = 0;
            double h = (b - a) / n;

            for (int i = 1; i <= n; i++)
            {
                S = S + h * (CalcMethods.ParseFunction(function, (a + i * h)));
            }
            return(S);
        }
        /// <summary>
        /// метод трапеций
        /// </summary>
        /// <param name="function">подинтегральная функция</param>
        /// <param name="a">начало отрезка [a, b]</param>
        /// <param name="b">конец отрезка [a, b]</param>
        /// <returns></returns>
        public double MethodOfTrapezoids(string function, double a, double b, int n)
        {
            double S = 0;
            double h = (b - a) / n;
            double x = a;

            for (double i = 0; i < n + 1; i++)
            {
                if (i == 0 || i == n)
                {
                    S = S + ((CalcMethods.ParseFunction(function, (x + i * h)))) * h / 2;
                }
                else
                {
                    S = S + ((CalcMethods.ParseFunction(function, (x + i * h)))) * h;
                }
            }
            return(S);
        }
        /// <summary>
        /// метод 3/8
        /// </summary>
        /// <param name="function">подинтегральная функция</param>
        /// <param name="a">начало отрезка [a, b]</param>
        /// <param name="b">конец отрезка [a, b]</param>
        /// <returns></returns>
        public double Method_3_8(string function, double a, double b, int n)
        {
            double h = (b - a) / (3 * n);
            double S = CalcMethods.ParseFunction(function, a) + CalcMethods.ParseFunction(function, b);
            int    m = 3 * n - 1;
            double x;

            for (int i = 1; i <= m; i++)
            {
                x = a + h * i;
                if (i % 3 == 0)
                {
                    S = S + 2 * CalcMethods.ParseFunction(function, x);
                }
                else
                {
                    S = S + 3 * CalcMethods.ParseFunction(function, x);
                }
            }
            return(S * 3 / 8 * h);
        }
예제 #6
0
        /// <summary>
        /// метед простой итерации
        /// </summary>
        /// <param name="Function">искомая функция</param>
        /// <param name="X0">начальное приближение</param>
        /// <param name="Epsilon">точность вычисления</param>
        /// <param name="resultTEXT"></param>
        /// <returns></returns>
        public double MethodOfSimpleIteration(string Function, double X0, double Epsilon, ref string resultTEXT)
        {
            double y = CalcMethods.ParseFunction(Function, X0);
            double k = 1;

            resultTEXT  = "Функция x = f(x) = " + Function + "\n";
            resultTEXT += "x0 = " + X0 + "   Epsilon =  " + Epsilon + "\n";
            while ((Math.Abs(y - X0) > Epsilon))
            {
                resultTEXT += "\n|у -x0| = " + (Math.Abs(y - X0));
                if (k > 500)
                {
                    throw new ArgumentException("k > 500");
                }
                resultTEXT += "\n y = " + y;
                resultTEXT += "\n k =" + k + "\n";
                k++;

                X0 = y;
                y  = CalcMethods.ParseFunction(Function, X0);
            }
            resultTEXT += "Результат =" + X0 + "\n";
            return(y);
        }
        /// <summary>
        /// метод парабол/симпсона
        /// </summary>
        /// <param name="function">подинтегральная функция</param>
        /// <param name="a">начало отрезка [a, b]</param>
        /// <param name="b">конец отрезка [a, b]</param>
        /// <returns></returns>
        public double MethodSimpsona(string function, double a, double b, int n)
        {
            double S = 0;
            double h = (b - a) / n;

            for (int i = 0; i <= n; i++)
            {
                if (i == 0 || i == n)
                {
                    S = S + ((CalcMethods.ParseFunction(function, (a + i * h)))) * h / 2;
                }
                else
                if (i % 2 == 0)
                {
                    S = S + (2 * CalcMethods.ParseFunction(function, (a + i * h))) * h / 3;
                }
                else
                {
                    S = S + (4 * CalcMethods.ParseFunction(function, (a + i * h))) * h / 3;
                }
            }

            return(S);
        }