コード例 #1
0
ファイル: Program.cs プロジェクト: juliavav/NumMethLab
        private static void Lab1()
        {
            var newton = new NewtonMethod(F, Df, Constants.A, Constants.B);

            Console.WriteLine(newton.GetAnswer());

            var simple = new SimpleIterations(F, Phi, Dphi, Constants.A, Constants.B);

            Console.WriteLine(simple.GetAnswer());

            //var simpleV2 = new SimpleIterationsV2(F, Phi, Dphi, Constants.Av2, Constants.Bv2);
            //Console.WriteLine(simpleV2.GetAnswer());
        }
コード例 #2
0
        public void ExecuteCalculate()
        {
            Text = "";
            ResX = "";
            double[][] a;
            double[]   b;
            ToArrays(out a, out b);

            int n = Matrix.Count;

            double[][] a0 = new double[n][];
            double[]   b0 = new double[n];
            for (int i = 0; i < n; i++)
            {
                a0[i] = new double[n];
                for (int j = 0; j < n; j++)
                {
                    a0[i][j] = a[i][j];
                }
                b0[i] = b[i];
            }


            for (int i = 0; i < n; i++)
            {
                if (a[i][i] == 0)
                {
                    Text = "Эл-ты на главной диагонали не должны быть нулевыми";
                    MessageBox.Show(n + "");
                    return;
                }
            }

            if (IsIt)
            {
                SimpleIterations si = new SimpleIterations(n);

                si.A = a0;
                si.B = b0;

                double eps = TextEps.HasValue ? TextEps.Value : 0.001;

                double[] res = si.Calculate(eps);

                if (res == null)
                {
                    Text = si.Log[0];
                    return;
                }

                for (int i = 0; i < si.Log.Count; i++)
                {
                    Text += si.Log[i];
                }

                ResX += "Решение найдено за " + (si.Log.Count - 1) + " итераций\n\n";
                for (int i = 0; i < n; i++)
                {
                    ResX += "x" + (i + 1) + " = " + Math.Round(res[i], (eps % 1).ToString().Length - 2) + '\n';
                }
                ResX += "\n******ПРОВЕРКА******\n";
                double[] check = new double[n];
                double   temp  = 0;
                int      tr;
                for (int i = 0; i < n; i++)
                {
                    temp = 0;
                    for (int j = 0; j < n; j++)
                    {
                        temp += a[i][j] * res[j];
                    }
                    tr = (b[i] % 1).ToString().Length - 2;
                    if (tr < 0 || tr > 15)
                    {
                        tr = 2;
                    }
                    check[i] = Math.Round(temp, tr);
                    b[i]     = Math.Round(b[i], tr);
                }
                bool ans = true;
                for (int i = 0; i < n; i++)
                {
                    ResX += check[i];
                    if (check[i] == b[i])
                    {
                        ResX += " = ";
                    }
                    else
                    {
                        ResX += " != ";
                        ans   = false;
                    }
                    ResX += b[i] + "\n";
                }
                if (ans)
                {
                    ResX += "\nПроверка пройдена.";
                }
                else
                {
                    ResX += "\nПроверка не пройдена.";
                }
            }
            else
            {
                n    = 3;
                ga   = new Gauss(n);
                ga.A = a0;
                ga.B = b0;

                double eps = TextEps.HasValue ? TextEps.Value : 0.001;

                double[] res = ga.Calculate();

                for (int i = 0; i < n; i++)
                {
                    ResX += "x" + (i + 1) + " = " + Math.Round(res[i], (eps % 1).ToString().Length - 2) + '\n';
                }
                CurStep = 2;
                ResX   += "\n******ПРОВЕРКА******\n";
                double[] check = new double[n];
                double   temp  = 0;
                int      tr;
                for (int i = 0; i < n; i++)
                {
                    temp = 0;
                    for (int j = 0; j < n; j++)
                    {
                        temp += a[i][j] * res[j];
                    }
                    tr = (b[i] % 1).ToString().Length - 2;
                    if (tr < 0 || tr > 15)
                    {
                        tr = 2;
                    }
                    check[i] = Math.Round(temp, tr);
                    b[i]     = Math.Round(b[i], tr);
                }
                bool ans = true;
                for (int i = 0; i < n; i++)
                {
                    ResX += check[i];
                    if (check[i] == b[i])
                    {
                        ResX += " = ";
                    }
                    else
                    {
                        ResX += " != ";
                        ans   = false;
                    }
                    ResX += b[i] + "\n";
                }
                if (ans)
                {
                    ResX += "\nПроверка пройдена.";
                }
                else
                {
                    ResX += "\nПроверка не пройдена.";
                }
            }
        }