예제 #1
0
        private static void ParseFunction()
        {
            while (true)
            {
                InputFunction();
                try
                {
                    _parsedFunction = Expr.Parse(_function);
                    break;
                }
                catch
                {
                    Console.WriteLine("Neteisingai įvesta funkcija, bandykite įvesti iš naujo.");
                    Console.WriteLine();
                }
            }

            Console.WriteLine("Sėkmingai pasirinkta funkcija: " + _parsedFunction.ToString());
            Console.WriteLine();
        }
예제 #2
0
        static void Main(string[] args)
        {
            //объявление переменных
            var x  = Expr.Variable("x");
            var y1 = Expr.Variable("y1");
            var y2 = Expr.Variable("y2");

            #region Zadanie 1
            //Метод последовательных приближений для решения системы

            //Объявление системы
            Expr expr_dy1 = (x + y1 * y2);
            Expr expr_dy2 = (x * x - y1 * y1);
            Func <double, double, double, double> dy1 = (expr_dy1).Compile("x", "y1", "y2");
            Func <double, double, double, double> dy2 = (expr_dy2).Compile("x", "y1", "y2");

            //Первое приближение
            Expr expr_y1_1             = (1 + x * x / 2);
            Expr expr_y2_1             = (-x + x * x * x / 3);
            Func <double, double> y1_1 = expr_y1_1.Compile("x");
            Func <double, double> y2_1 = expr_y2_1.Compile("x");
            //Второе приближение
            Expr expr_y1_2             = (1 - (x.Pow(4)) / 24 + (x.Pow(6)) / 36);
            Expr expr_y2_2             = (-x - x.Pow(5) / 20);
            Func <double, double> y1_2 = expr_y1_2.Compile("x");
            Func <double, double> y2_2 = expr_y2_2.Compile("x");

            //диапозон значений x
            double[] section_x = new double[2] {
                0, 1
            };
            //Количество делений
            int divisions = 10;
            //Определение величны шага
            double h = (section_x[1] - section_x[0]) / divisions;

            double[] x_val = new double[divisions + 1];

            x_val[0] = section_x[0];
            for (int i = 1; i <= divisions; i++)
            {
                x_val[i] = x_val[i - 1] + h;
            }

            //массивы для хранения значений y1 и y2
            double[] y1_val = new double[divisions + 1];
            double[] y2_val = new double[divisions + 1];

            y1_val[0] = 1;
            y2_val[0] = 0;

            //Печать начальных условий
            Console.WriteLine($"y1' = {expr_dy1.ToString()}");
            Console.WriteLine($"y2' = {expr_dy2.ToString()}");
            Console.WriteLine("Начальные условия: ");
            Console.WriteLine("y1(0) = 1; y2(0) = 0;");

            //Печать приближений
            Console.WriteLine("Исходя из формулы y_n = y0 + integrate (f(x, y_(n-1)))dx from 0 to x , получаем:\n");
            Console.WriteLine($"y1_1 = 1 + integrate (x + 0)dx from 0 to x = {expr_y1_1.ToString()}");
            Console.WriteLine($"y2_1 = 1 + integrate (x^2 - 1)dx from 0 to x = {expr_y2_1.ToString()}\n");
            Console.WriteLine($"y1_2 = 1 + integrate (x + (1 + x^2/2)*(-x + x^3/3))dx from 0 to x = {expr_y1_2.ToString()}");
            Console.WriteLine($"y2_2 = 1 + integrate (x^2 + (1 + x^2 + x^4/4))dx from 0 to x = {expr_y2_2.ToString()}\n");

            //Результат
            Console.WriteLine($"x\ty1_1\ty2_1\ty1_2\ty2_2");

            for (int i = 0; i <= divisions; i++)
            {
                Console.Write($"{x_val[i]:0.00}\t");
                Console.Write($"{y1_1(x_val[i]):0.0000}\t");
                Console.Write($"{y2_1(x_val[i]):0.0000}\t");
                Console.Write($"{y1_2(x_val[i]):0.0000}\t");
                Console.WriteLine($"{y2_2(x_val[i]):0.0000}\t");
            }

            Console.WriteLine();

            #endregion



            Expr expr_f1 = -2 * y1 + 4 * y2;
            Func <double, double, double, double> f1 = (expr_f1).Compile("x", "y1", "y2");
            Expr expr_f2 = -y1 + 3 * y2;
            Func <double, double, double, double> f2 = (expr_f2).Compile("x", "y1", "y2");

            Console.WriteLine($"Уравнение:\ny1' = {expr_f1.ToString()}\ny2' = {expr_f2.ToString()}\n");
            Console.WriteLine("Начальные условия: ");
            Console.WriteLine("y1(0) = 3; y2(0) = 0;\n");

            #region Zadanie 2

            Miln(f1, f2, 10, 0, 1, 3, 0);

            #endregion

            #region Zadanie 3

            Adams(f1, f2, 10, 0, 1, 3, 0);

            #endregion
        }
예제 #3
0
 private void AssertParse(ASTNode node, Expr expected)
 => this.AssertParse(node, expected.ToString());
 public static void GetAllFactors(MathNet.Symbolics.SymbolicExpression sym, List <string> list)
 {
     if (sym.Factors().Count() == 1)
     {
         try { var tmp = sym.RealNumberValue; }
         catch
         {
             if (sym.Expression.IsPower)
             {
                 var hh = sym.Expression as MathNet.Symbolics.Expression.Power;
                 if (hh.Item1.IsIdentifier)
                 {
                     if (!list.Contains((hh.Item1 as MathNet.Symbolics.Expression.Identifier).Item.Item.ToString()))
                     {
                         list.Add((hh.Item1 as MathNet.Symbolics.Expression.Identifier).Item.Item.ToString());
                     }
                 }
                 else
                 {
                     var sym1 = new MathNet.Symbolics.SymbolicExpression(hh.Item1);
                     GetAllFactors(sym1, list);
                 }
                 if (hh.Item2.IsIdentifier)
                 {
                     if (!list.Contains((hh.Item2 as MathNet.Symbolics.Expression.Identifier).Item.Item.ToString()))
                     {
                         list.Add((hh.Item2 as MathNet.Symbolics.Expression.Identifier).Item.Item.ToString());
                     }
                 }
                 else
                 {
                     var sym1 = new MathNet.Symbolics.SymbolicExpression(hh.Item2);
                     GetAllFactors(sym1, list);
                 }
             }
             else if (sym.Expression.IsFunction)
             {
                 var hh = sym.Expression as MathNet.Symbolics.Expression.Function;
                 if (hh.Item2.IsIdentifier)
                 {
                     if (!list.Contains((hh.Item2 as MathNet.Symbolics.Expression.Identifier).Item.Item.ToString()))
                     {
                         list.Add((hh.Item2 as MathNet.Symbolics.Expression.Identifier).Item.Item.ToString());
                     }
                 }
                 else
                 {
                     var sym1 = new MathNet.Symbolics.SymbolicExpression(hh.Item2);
                     GetAllFactors(sym1, list);
                 }
             }
             else if (!list.Contains(sym.ToString()))
             {
                 list.Add(sym.ToString());
             }
         }
     }
     else
     {
         foreach (var item in sym.Factors())
         {
             GetAllFactors(item, list);
         }
     }
 }