Example #1
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
        }
Example #2
0
        private void Button1_Click(object sender, EventArgs e)
        {
            trackBarCurrentLayer.Maximum = int.Parse(textBox_t_count.Text) - 1;

            double a = Math.Sqrt(double.Parse(textBoxA2.Text));
            double L = double.Parse(textBoxL.Text);
            double T = double.Parse(textBoxT.Text);

            double h = double.Parse(textBox_h.Text);
            double t = double.Parse(textBox_t.Text);

            string cos1 = "cos(pi*x/" + Convert.ToString(L) + ")";
            string cos2 = "cos(2*pi*x/" + Convert.ToString(L) + ")";
            string sin1 = "sin(pi*x/" + Convert.ToString(L) + ")";
            string sin2 = "sin(2*pi*x/" + Convert.ToString(L) + ")";

            // phi
            string phi_coeff_1 = textBox_phi_coeff_1.Text;
            string phi_coeff_2 = textBox_phi_coeff_2.Text;
            string phi_coeff_3 = textBox_phi_coeff_3.Text;

            string formula_phi = phi_coeff_1 + "+" +
                                 phi_coeff_2 + "*" + cos1 + "+" +
                                 phi_coeff_3 + "*" + cos2;

            MathNet.Symbolics.SymbolicExpression expr_phi = MathNet.Symbolics.SymbolicExpression.Parse(formula_phi);


            // psi
            string psi_coeff_1 = textBox_psi_coeff_1.Text;
            string psi_coeff_2 = textBox_psi_coeff_2.Text;
            string psi_coeff_3 = textBox_psi_coeff_3.Text;

            string formula_psi = psi_coeff_1 + "+" +
                                 psi_coeff_2 + "*" + cos1 + "+" +
                                 psi_coeff_3 + "*" + cos2;

            MathNet.Symbolics.SymbolicExpression expr_psi = MathNet.Symbolics.SymbolicExpression.Parse(formula_psi);


            // b
            string b_coeff_1 = textBox_b_coeff_1.Text;
            string b_coeff_2 = textBox_b_coeff_2.Text;
            string b_coeff_3 = textBox_b_coeff_3.Text;
            string b_coeff_4 = textBox_b_coeff_4.Text;
            string b_coeff_5 = textBox_b_coeff_5.Text;

            string formula_b = b_coeff_1 + "+" +
                               b_coeff_2 + "*" + cos1 + "+" +
                               b_coeff_3 + "*" + sin1 + "+" +
                               b_coeff_4 + "*" + cos2 + "+" +
                               b_coeff_5 + "*" + sin2;

            MathNet.Symbolics.SymbolicExpression expr_b = MathNet.Symbolics.SymbolicExpression.Parse(formula_b);


            Func <double, double> phi = expr_phi.Compile("x");
            Func <double, double> psi = expr_psi.Compile("x");
            Func <double, double> b   = expr_b.Compile("x");

            chart.Series.Clear();
            chart.Series.Add("Начальное условие \u03D5(x)");
            chart.Series["Начальное условие \u03D5(x)"].ChartType   = SeriesChartType.Spline;
            chart.Series["Начальное условие \u03D5(x)"].Color       = Color.Blue;
            chart.Series["Начальное условие \u03D5(x)"].BorderWidth = 2;

            for (double i = 0; i <= L; i += h)
            {
                chart.Series["Начальное условие \u03D5(x)"].Points.AddXY(i, phi(i));
            }

            grid2D = new Grid2D(phi, psi, b, a, L, T, h, t);
            if (radioButtonHomogeneous.Checked == true)
            {
                grid2D.SolveHomogeneous();
            }
            else if (radioButtonNongomogeneous.Checked == true)
            {
                grid2D.SolveNonhomogeneous();
            }


            trackBarCurrentLayer.Value = int.Parse(textBox_t_count.Text) - 1;
            groupBoxCurrentLayer.Text  = String.Format("Текущий слой: {0} (последний u(x, T))", trackBarCurrentLayer.Value);
            string number_layer = "Слой " + trackBarCurrentLayer.Value;

            if (trackBarCurrentLayer.Value == int.Parse(textBox_t_count.Text) - 1)
            {
                number_layer += " (последний u(x, T))";
            }
            chart.Series.Add(number_layer);
            chart.Series[number_layer].ChartType   = SeriesChartType.Spline;
            chart.Series[number_layer].Color       = Color.Black;
            chart.Series[number_layer].BorderWidth = 2;


            double[] layer = grid2D.GetLastLayer();
            for (int i = 0; i < layer.Length; i++)
            {
                chart.Series[number_layer].Points.AddXY(i * h, layer[i]);
            }
        }