Пример #1
0
        public StructForMainSolution MainSolution(int n, double x0, double xn, double ksi)
        {
            StructForMainSolution structForMain = new StructForMainSolution();

            structForMain.chart_pointV     = new ChartPoint();
            structForMain.chart_pointV.x   = new List <double>();
            structForMain.chart_pointV.Vx  = new List <double>();
            structForMain.chart_pointV2    = new ChartPoint();
            structForMain.chart_pointV2.x  = new List <double>();
            structForMain.chart_pointV2.Vx = new List <double>();
            double[]    k             = new double[n + 1];
            double[]    k2            = new double[2 * n + 1];
            double[]    f             = new double[n];
            double[]    f2            = new double[2 * n + 1];
            double[]    q             = new double[n];
            double[]    q2            = new double[2 * n + 1];
            double[]    diagonal1     = new double[n - 1];
            double[]    diagonal1_2   = new double[2 * n - 1];
            double[]    diagonal2     = new double[n];
            double[]    diagonal2_2   = new double[2 * n];
            double[]    diagonal3     = new double[n - 1];
            double[]    diagonal3_2   = new double[2 * n - 1];
            double[]    right_column  = new double[n];
            double[]    right_column2 = new double[2 * n];
            double      h             = (xn - x0) / n;
            double      h2            = h / 2;
            MyFunctions myFunc        = new MyFunctions();

            for (int i = 0; i < n + 1; ++i)
            {
                structForMain.chart_pointV.x.Add(i * h);
            }

            for (int i = 0; i < 2 * n + 1; ++i)
            {
                structForMain.chart_pointV2.x.Add(i * h2);
            }

            for (int i = 1; i < n; ++i)
            {
                if (ksi >= (structForMain.chart_pointV.x[i] + 0.5 * h))
                {
                    f[i] = myFunc.f1(structForMain.chart_pointV.x[i]);
                    q[i] = myFunc.q1(structForMain.chart_pointV.x[i]);
                }
                else if (ksi <= (structForMain.chart_pointV.x[i] - 0.5 * h))
                {
                    f[i] = myFunc.f2(structForMain.chart_pointV.x[i]);
                    q[i] = myFunc.q2(structForMain.chart_pointV.x[i]);
                }
                else
                {
                    f[i] = n * ((ksi - (structForMain.chart_pointV.x[i] - 0.5 * h)) * myFunc.f1(0.5 * ((structForMain.chart_pointV.x[i] - 0.5 * h) + ksi)) + ((structForMain.chart_pointV.x[i] + 0.5 * h) - ksi) * myFunc.f2(0.5 * (ksi + (structForMain.chart_pointV.x[i] + 0.5 * h))));
                    q[i] = n * ((ksi - (structForMain.chart_pointV.x[i] - 0.5 * h)) * myFunc.q1(0.5 * (ksi + (structForMain.chart_pointV.x[i] - 0.5 * h))) + ((structForMain.chart_pointV.x[i] + 0.5 * h) - ksi) * myFunc.q2(0.5 * ((structForMain.chart_pointV.x[i] + 0.5 * h) + ksi)));
                }


                if (ksi >= structForMain.chart_pointV.x[i])
                {
                    k[i] = 2 * myFunc.k1(structForMain.chart_pointV.x[i]) * myFunc.k1(structForMain.chart_pointV.x[i - 1]) / (myFunc.k1(structForMain.chart_pointV.x[i - 1]) + myFunc.k1(structForMain.chart_pointV.x[i]));
                }
                else if (ksi <= structForMain.chart_pointV.x[i - 1])
                {
                    k[i] = 2 * myFunc.k2(structForMain.chart_pointV.x[i - 1]) * myFunc.k2(structForMain.chart_pointV.x[i]) / (myFunc.k2(structForMain.chart_pointV.x[i - 1]) + myFunc.k2(structForMain.chart_pointV.x[i]));
                }
                else
                {
                    k[i] = 2 * h * myFunc.k1(structForMain.chart_pointV.x[i - 1]) * myFunc.k1(ksi) * myFunc.k2(ksi) * myFunc.k2(structForMain.chart_pointV.x[i]) / ((ksi - structForMain.chart_pointV.x[i - 1]) * (myFunc.k1(ksi) + myFunc.k1(structForMain.chart_pointV.x[i - 1])) * myFunc.k2(ksi) * myFunc.k2(structForMain.chart_pointV.x[i]) + (structForMain.chart_pointV.x[i] - ksi) * (myFunc.k2(structForMain.chart_pointV.x[i]) + myFunc.k2(ksi)) * myFunc.k1(structForMain.chart_pointV.x[i - 1]) * myFunc.k1(ksi));
                }
            }
            k[n] = 2 * myFunc.k2(structForMain.chart_pointV.x[(int)n]) * myFunc.k2(structForMain.chart_pointV.x[n - 1]) / (myFunc.k2(structForMain.chart_pointV.x[n - 1]) + myFunc.k2(structForMain.chart_pointV.x[(int)n]));

            structForMain.chart_pointV.Vx.Add(x0);

            for (int i = 1; i < 2 * n; ++i)
            {
                if (ksi >= (structForMain.chart_pointV2.x[i] + 0.5 * h2))
                {
                    f2[i] = myFunc.f1(structForMain.chart_pointV2.x[i]);
                    q2[i] = myFunc.q1(structForMain.chart_pointV2.x[i]);
                }
                else if (ksi <= (structForMain.chart_pointV2.x[i] - 0.5 * h2))
                {
                    f2[i] = myFunc.f2(structForMain.chart_pointV2.x[i]);
                    q2[i] = myFunc.q2(structForMain.chart_pointV2.x[i]);
                }
                else
                {
                    f2[i] = 2 * n * ((ksi - (structForMain.chart_pointV2.x[i] - 0.5 * h2)) * myFunc.f1(0.5 * ((structForMain.chart_pointV2.x[i] - 0.5 * h2) + ksi)) + ((structForMain.chart_pointV2.x[i] + 0.5 * h2) - ksi) * myFunc.f2(0.5 * (ksi + (structForMain.chart_pointV2.x[i] + 0.5 * h2))));
                    q2[i] = 2 * n * ((ksi - (structForMain.chart_pointV2.x[i] - 0.5 * h2)) * myFunc.q1(0.5 * (ksi + (structForMain.chart_pointV2.x[i] - 0.5 * h2))) + ((structForMain.chart_pointV2.x[i] + 0.5 * h2) - ksi) * myFunc.q2(0.5 * ((structForMain.chart_pointV2.x[i] + 0.5 * h2) + ksi)));
                }


                if (ksi >= structForMain.chart_pointV2.x[i])
                {
                    k2[i] = 2 * myFunc.k1(structForMain.chart_pointV2.x[i]) * myFunc.k1(structForMain.chart_pointV2.x[i - 1]) / (myFunc.k1(structForMain.chart_pointV2.x[i - 1]) + myFunc.k1(structForMain.chart_pointV2.x[i]));
                }
                else if (ksi <= structForMain.chart_pointV2.x[i - 1])
                {
                    k2[i] = 2 * myFunc.k2(structForMain.chart_pointV2.x[i - 1]) * myFunc.k2(structForMain.chart_pointV2.x[i]) / (myFunc.k2(structForMain.chart_pointV2.x[i - 1]) + myFunc.k2(structForMain.chart_pointV2.x[i]));
                }
                else
                {
                    k2[i] = 2 * h2 * myFunc.k1(structForMain.chart_pointV2.x[i - 1]) * myFunc.k1(ksi) * myFunc.k2(ksi) * myFunc.k2(structForMain.chart_pointV2.x[i]) / ((ksi - structForMain.chart_pointV2.x[i - 1]) * (myFunc.k1(ksi) + myFunc.k1(structForMain.chart_pointV2.x[i - 1])) * myFunc.k2(ksi) * myFunc.k2(structForMain.chart_pointV2.x[i]) + (structForMain.chart_pointV2.x[i] - ksi) * (myFunc.k2(structForMain.chart_pointV2.x[i]) + myFunc.k2(ksi)) * myFunc.k1(structForMain.chart_pointV2.x[i - 1]) * myFunc.k1(ksi));
                }
            }
            k2[2 * n] = 2 * myFunc.k2(structForMain.chart_pointV2.x[2 * n]) * myFunc.k2(structForMain.chart_pointV2.x[2 * n - 1]) / (myFunc.k2(structForMain.chart_pointV2.x[2 * n - 1]) + myFunc.k2(structForMain.chart_pointV2.x[2 * n]));
            structForMain.chart_pointV2.Vx.Add(x0);
            for (int i = 1; i < n - 1; ++i)
            {
                diagonal1[i] = k[i + 1] * n * n;
                diagonal2[i] = -(n * n * (k[i] + k[i + 1]) + q[i]);
                diagonal3[i] = k[i + 1] * n * n;
            }
            diagonal2[(int)n - 1] = -(n * n * (k[n - 1] + k[n]) + q[n - 1]);

            for (int i = 1; i < n; i++)
            {
                right_column[i] = -f[i];
            }
            right_column[1]     -= x0 * k[1] * n * n;
            right_column[n - 1] -= xn * k[n] * n * n;
            double[] solutionForV = Sweep(diagonal1, diagonal2, diagonal3, right_column);
            for (int i = 1; i < n; ++i)
            {
                structForMain.chart_pointV.Vx.Add(solutionForV[i - 1]);
            }
            for (int i = 1; i < 2 * n - 1; ++i)
            {
                diagonal1_2[i] = k2[i + 1] * n * n * 4;
                diagonal2_2[i] = -(4 * n * n * (k2[i] + k2[i + 1]) + q2[i]);
                diagonal3_2[i] = k2[i + 1] * n * n * 4;
            }
            diagonal2_2[2 * n - 1] = -(4 * n * n * (k2[2 * n - 1] + k2[2 * n]) + q2[2 * n - 1]);

            for (int i = 1; i < 2 * n; i++)
            {
                right_column2[i] = -f2[i];
            }
            right_column2[1]         -= x0 * k2[1] * n * n * 4;
            right_column2[2 * n - 1] -= xn * k2[2 * n] * n * n * 4;
            double[] solutionForV2 = Sweep(diagonal1_2, diagonal2_2, diagonal3_2, right_column2);
            for (int i = 1; i < 2 * n; ++i)
            {
                structForMain.chart_pointV2.Vx.Add(solutionForV2[i - 1]);
            }
            structForMain.chart_pointV.Vx.Add(xn);
            structForMain.chart_pointV2.Vx.Add(xn);
            return(structForMain);
        }
Пример #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            chart1.Series[0].Points.Clear();
            chart1.Series[1].Points.Clear();
            chart2.Series[0].Points.Clear();
            dataGridView1.Rows.Clear();
            chart_point    = new ChartPoint();
            chart_point.x  = new List <double>();
            chart_point.y  = new List <double>();
            chart_point.Vx = new List <double>();
            List <double> max_abs = new List <double>();
            int           n       = Convert.ToInt32(numericUpDown1.Value);

            dataGridView1.ColumnCount = 5;
            dataGridView1.RowCount    = n + 1;
            StructForMainSolution structForMain = new StructForMainSolution();

            structForMain.chart_pointV     = new ChartPoint();
            structForMain.chart_pointV2    = new ChartPoint();
            structForMain.chart_pointV.x   = new List <double>();
            structForMain.chart_pointV.Vx  = new List <double>();
            structForMain.chart_pointV2.x  = new List <double>();
            structForMain.chart_pointV2.Vx = new List <double>();
            bool   flag = false;
            double max_UV;
            double max_UV_x;

            if (radioButton1.Checked)
            {
                chart_point = SolutionsTest1(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text), Convert.ToDouble(textBox3.Text), n, x0, xn);
                flag        = false;
            }
            if (radioButton2.Checked)
            {
                chart_point = SolutionsTest2(n, x0, xn, ksi);
                flag        = false;
            }
            if (radioButton3.Checked)
            {
                structForMain = MainSolution(n, x0, xn, ksi);
                flag          = true;
                dataGridView1.Columns[2].HeaderText = "v(x)";
                dataGridView1.Columns[3].HeaderText = "v2(x)";
                dataGridView1.Columns[4].HeaderText = "|v(x)-v2(x)|";
            }
            if (flag == true)
            {
                max_UV   = Math.Abs(structForMain.chart_pointV.Vx[0] - structForMain.chart_pointV2.Vx[0]);
                max_UV_x = structForMain.chart_pointV.x[0];
                for (int i = 0; i < structForMain.chart_pointV.x.Count; i++)
                {
                    chart1.Series[0].Points.AddXY(structForMain.chart_pointV.x[i], structForMain.chart_pointV.Vx[i]);
                    chart1.Series[1].Points.AddXY(structForMain.chart_pointV2.x[2 * i], structForMain.chart_pointV2.Vx[2 * i]);
                    dataGridView1.Rows[i].Cells[0].Value = i;
                    dataGridView1.Rows[i].Cells[1].Value = structForMain.chart_pointV.x[i];
                    dataGridView1.Rows[i].Cells[2].Value = structForMain.chart_pointV.Vx[i];
                    dataGridView1.Rows[i].Cells[3].Value = structForMain.chart_pointV2.Vx[2 * i];
                    dataGridView1.Rows[i].Cells[4].Value = Math.Abs(structForMain.chart_pointV.Vx[i] - structForMain.chart_pointV2.Vx[2 * i]);
                    if (Math.Abs(structForMain.chart_pointV.Vx[i] - structForMain.chart_pointV2.Vx[2 * i]) > max_UV)
                    {
                        max_UV   = Math.Abs(structForMain.chart_pointV.Vx[i] - structForMain.chart_pointV2.Vx[2 * i]);
                        max_UV_x = structForMain.chart_pointV.x[i];
                    }
                    chart2.Series[0].Points.AddXY(structForMain.chart_pointV.x[i], Math.Abs(structForMain.chart_pointV.Vx[i] - structForMain.chart_pointV2.Vx[2 * i]));
                }
                label8.Text = "Max(|v(x)-v2(x)|): " + max_UV;
            }
            else
            {
                max_UV   = Math.Abs(chart_point.y[0] - chart_point.Vx[0]);
                max_UV_x = chart_point.x[0];
                for (int i = 0; i < chart_point.x.Count; i++)
                {
                    chart1.Series[0].Points.AddXY(chart_point.x[i], chart_point.y[i]);
                    chart1.Series[1].Points.AddXY(chart_point.x[i], chart_point.Vx[i]);
                    dataGridView1.Rows[i].Cells[0].Value = i;
                    dataGridView1.Rows[i].Cells[1].Value = chart_point.x[i];
                    dataGridView1.Rows[i].Cells[2].Value = chart_point.y[i];
                    dataGridView1.Rows[i].Cells[3].Value = chart_point.Vx[i];
                    dataGridView1.Rows[i].Cells[4].Value = Math.Abs(chart_point.y[i] - chart_point.Vx[i]);
                    if (Math.Abs(chart_point.y[i] - chart_point.Vx[i]) > max_UV)
                    {
                        max_UV   = Math.Abs(chart_point.y[i] - chart_point.Vx[i]);
                        max_UV_x = chart_point.x[i];
                    }
                    chart2.Series[0].Points.AddXY(chart_point.x[i], Math.Abs(chart_point.y[i] - chart_point.Vx[i]));
                }
                label8.Text = "Max(|u(x)-v(x)|): " + max_UV;
            }

            label9.Text = "В точке: x= " + max_UV_x;
        }