C#求解三对角矩阵数值解法-追赶法

关键词:

使用追赶法求解三对角矩阵的数值解法

    class MatrixSolver
    {

        //系数矩阵
        private double[,] Matrix;
        private double[] Right;
        private double[] Result;
        private double[] Beta;
        private double[] Y;
        int N;

        public MatrixSolver(double[,] M, double[] F)
        {
            N = Right.Length;
            Matrix = M;
            Right = F;
            Result = new double[N];
            Beta = new double[N];
            Y = new double[N];
        }


        public double[] Caculate()
        {
            Beta[0] = Matrix[0, 1] / Matrix[0, 0];
            for (int i = 1; i < N-1; i++)
            {
                Beta[i] = Matrix[i, i + 1] / (Matrix[i, i] - Matrix[i, i - 1] * Beta[i - 1]);
            }
            Y[0] = Right[0] / Matrix[0, 0];
            for (int i = 1; i <N; i++)
            {
                Y[i] = (Right[i] - Matrix[i, i - 1] * Y[i-1]) / (Matrix[i,i] - Matrix[i,i-1] * Beta[i-1]);
            }
            Result[N - 1] = Y[N - 1];
            for (int i = N-2; i >0; i--)
            {
                Result[i] = Y[i]-Beta[i]*Result[i+1];
            }
            return Result;
        }






    }

作者:songshizhao 发表于:2017-09-11T01:06:57 访问量:1030
评论 [发表评论]