Example #1
0
        /// <summary>
        /// 建立电学方程并求解
        /// </summary>
        /// <returns>线性方程组求解结果</returns>
        private double[] SolveEquation()
        {
            int branchCount = branchList.Count;
            //有一个零电势点是默认的,所以矩阵少一个维度。
            int    nodeCount = nodeList.Count - 1;
            double EMP       = source.GetEMF();

            //串联电路
            if (branchCount == 1)
            {
                double I = EMP / branchList[0].R;
                return(new double[] { I });
            }

            double[,] equations = new double[branchCount + nodeCount, branchCount + nodeCount];
            for (int i = 0; i < branchCount + nodeCount; i++)
            {
                for (int j = 0; j < branchCount + nodeCount; j++)
                {
                    equations[i, j] = 0;
                }
            }
            for (int i = 0; i < branchCount; i++)
            {
                if (branchList[i].PP.index != nodeCount)
                {
                    equations[branchList[i].PP.index, i] = 1;
                    equations[i + nodeCount, branchList[i].PP.index + branchCount] = 1;
                }
                if (branchList[i].NP.index != nodeCount)
                {
                    equations[branchList[i].NP.index, i] = -1;
                    equations[i + nodeCount, branchList[i].NP.index + branchCount] = -1;
                }
                equations[nodeCount + i, i] = -branchList[i].R;
            }

            Matrix A = new Matrix(equations);

            double[] B = new double[nodeCount + branchCount];
            for (int i = 0; i < nodeCount + branchCount; i++)
            {
                B[i] = 0;
            }
            B[nodeCount] = -EMP;

            GaussElimination gauss = new GaussElimination(A, B, false);

            return(gauss.X);
        }