Esempio n. 1
0
        public VectorR GaussSeidel(MatrixR A, VectorR b, int MaxIterations, double tolerance)
        {
            int     n = b.GetSize();
            VectorR x = new VectorR(n);

            for (int nIteration = 0; nIteration < MaxIterations; nIteration++)
            {
                VectorR xOld = x.Clone();
                for (int i = 0; i < n; i++)
                {
                    double db = b[i];
                    double da = A[i, i];
                    if (Math.Abs(da) < epsilon)
                    {
                        throw new ArgumentException("Diagonal element is too small!");
                    }
                    for (int j = 0; j < i; j++)
                    {
                        db -= A[i, j] * x[j];
                    }
                    for (int j = i + 1; j < n; j++)
                    {
                        db -= A[i, j] * xOld[j];
                    }
                    x[i] = db / da;
                }
                VectorR dx = x - xOld;
                if (dx.GetNorm() < tolerance)
                {
                    //MessageBox.Show(nIteration.ToString());
                    return(x);
                }
            }
            return(x);
        }
Esempio n. 2
0
        private static MatrixR Jacobian(MFunction f, VectorR x)
        {
            double  h        = 0.0001;
            int     n        = x.GetSize();
            MatrixR jacobian = new MatrixR(n, n);
            VectorR x1       = x.Clone();

            for (int j = 0; j < n; j++)
            {
                x1[j] = x[j] + h;
                for (int i = 0; i < n; i++)
                {
                    jacobian[i, j] = (f(x1)[i] - f(x)[i]) / h;
                }
            }
            return(jacobian);
        }
Esempio n. 3
0
        public static VectorR TridiagonalEigenvector(double s, double tolerance, out double lambda)
        {
            int n = Alpha.GetLength(0);

            double[] gamma = (double[])Beta.Clone();
            double[] beta  = (double[])Beta.Clone();
            double[] alpha = new double[n];
            for (int i = 0; i < n; i++)
            {
                alpha[i] = Alpha[i] - s;
            }
            double[] gamma1, alpha1, beta1;
            LUDecomposition(gamma, alpha, beta, out gamma1, out alpha1, out beta1);
            VectorR x      = new VectorR(n);
            Random  random = new Random();

            for (int i = 0; i < n; i++)
            {
                x[i] = random.NextDouble();
            }
            x.Normalize();
            VectorR x1 = new VectorR(n);;
            double  sign;

            do
            {
                x1 = x.Clone();
                LUSolver(gamma1, alpha1, beta1, x);
                x.Normalize();
                if (VectorR.DotProduct(x1, x) < 0.0)
                {
                    sign = -1.0;
                    x    = -x;
                }
                else
                {
                    sign = 1.0;
                }
            }while ((x - x1).GetNorm() > tolerance);
            lambda = s + sign / x.GetNorm();
            return(x);
        }