Exemple #1
0
        static void Main()
        {
            Console.WriteLine("Lab4: Tridiagonal algorithm       \n-----------------------------------");
            try
            {
                var system = new LSystem(STUDENT_NUMBER, GROUP_NUMBER, 4);
                system.GenerateTridiagonalSystem();

                Console.WriteLine("Our system:");
                system.Print();

                var solution = Tridiagonal.Calculate(system);
                Console.WriteLine("Solutions via Tridiagonal algorithm:");
                solution.Print();

                Console.WriteLine("Residuals: ");
                system.CalcResiduals(solution)
                .Print(15);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.Read();
            }


            Console.Read();
        }
Exemple #2
0
    private static void test03()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST03 uses POWER_METHOD2 on the TRIS matrix.
    //
    //  Discussion:
    //
    //    This matrix, despite having a single dominant eigenvalue, will generally
    //    converge only very slowly under the power method.  This has to do with
    //    the fact that the matrix has only 3 eigenvectors.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    30 August 2008
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int       i;
        int       it_num = 0;
        Complex   lambda = new();
        const int n      = 50;

        double[] x = new double[n];

        const double alpha = -1.0;
        const double beta  = 10.0;
        const double gamma = 8.0;

        double[] a = Tridiagonal.tris(n, n, alpha, beta, gamma);

        Complex[] v = new Complex[n];

        int seed = 123456789;

        UniformRNG.r8vec_uniform_01(n, ref seed, ref x);

        const int    it_max = 4000;
        const double tol    = 0.000001;

        Console.WriteLine("");
        Console.WriteLine("TEST03");
        Console.WriteLine("  Use POWER_METHOD2 on the TRIS (tridiagonal scalar) matrix.");
        Console.WriteLine("");
        Console.WriteLine("  Matrix order N         = " + n + "");
        Console.WriteLine("  Maximum iterations     = " + it_max + "");
        Console.WriteLine("  Error tolerance        = " + tol + "");

        DateTime ctime1 = DateTime.Now;

        PowerMethod.power_method2(n, a, x, it_max, tol, ref lambda, v, ref it_num);

        DateTime ctime2 = DateTime.Now;
        double   ctime  = (ctime2 - ctime1).Seconds;

        Console.WriteLine("");
        Console.WriteLine("  Number of iterations   = " + it_num + "");
        Console.WriteLine("  CPU time               = " + ctime + "");
        Console.WriteLine("  Estimated eigenvalue   = "
                          + lambda.Real.ToString("0.##############")
                          + "  " + lambda.Imaginary.ToString("0.##############") + "");

        Complex[] lambda_vec = Tridiagonal.tris_eigenvalues(n, alpha, beta, gamma);

        Complex lambda_max = lambda_vec[0];

        for (i = 1; i < n; i++)
        {
            if (Complex.Abs(lambda_max) < Complex.Abs(lambda_vec[i]))
            {
                lambda_max = lambda_vec[i];
            }
        }

        Console.WriteLine("  Correct max eigenvalue = "
                          + lambda_max.Real.ToString("0.##############")
                          + "  " + lambda_max.Imaginary.ToString("0.##############") + "");

        Console.WriteLine("  Error                  = " + Complex.Abs(lambda - lambda_max) + "");
    }