コード例 #1
0
        private void btQCalc_Click(object sender, EventArgs e)
        {
            double x1, x2, x3, x4;

            x1 = double.Parse(listView2.Items[3].SubItems[1].Text);
            x2 = double.Parse(listView2.Items[4].SubItems[1].Text);
            x3 = double.Parse(listView2.Items[5].SubItems[1].Text);
            x4 = double.Parse(listView2.Items[6].SubItems[1].Text);
            double[] x = new double[25] {
                1, 0, 0, 0, 0,
                1, x1, x1 *x1, x1 *x1 *x1, x1 *x1 *x1 *x1,
                1, x2, x2 *x2, x2 *x2 *x2, x2 *x2 *x2 *x2,
                1, x3, x3 *x3, x3 *x3 *x3, x3 *x3 *x3 *x3,
                1, x4, x4 *x4, x4 *x4 *x4, x4 *x4 *x4 *x4,
            };
            double[] Q = new double[5] {
                0, 70, 350, 3500, 7000
            };
            Matrix           A  = Matrix.Create(5, 5, x);
            GaussElimination ge = new GaussElimination(A, Q);

//             double[] Q1 = new double[4];
//             Q1[0] = ge.X[0] + ge.X[1] * x1 + ge.X[2] * x1 * x1 + ge.X[3] * x1 * x1 * x1;
//             Q1[1] = ge.X[0] + ge.X[1] * x2 + ge.X[2] * x2 * x2 + ge.X[3] * x2 * x2 * x2;
//             Q1[2] = ge.X[0] + ge.X[1] * x3 + ge.X[2] * x3 * x3 + ge.X[3] * x3 * x3 * x3;
//             Q1[3] = ge.X[0] + ge.X[1] * x4 + ge.X[2] * x4 * x4 + ge.X[3] * x4 * x4 * x4;
            if (!WFNetLib.WFGlobal.OpenSerialPort(ref serialPort1, "调试串口"))
            {
                return;
            }
            byte[] a0B = BitConverter.GetBytes(((float)ge.X[1]));
            byte[] a1B = BitConverter.GetBytes(((float)ge.X[2]));
            byte[] a2B = BitConverter.GetBytes(((float)ge.X[3]));
            byte[] a3B = BitConverter.GetBytes(((float)ge.X[4]));

            byte[] tx = new byte[16];
            for (int i = 0; i < 4; i++)
            {
                tx[i]      = a0B[i];
                tx[4 + i]  = a1B[i];
                tx[8 + i]  = a2B[i];
                tx[12 + i] = a3B[i];
            }
            CP68Packet ret = CP68Packet.CP68ComProc(ref serialPort1, CommandConst.CP68_UserCommand_WriteQCalc, tx);

            if (ret != null)
            {
                if (ret.Header.ControlCode == (byte)(CommandConst.CP68_UserCommand_WriteTCalc | 0x80))
                {
                    MessageBox.Show("设定成功");
                }
                else
                {
                    MessageBox.Show("设定失败");
                }
            }
            serialPort1.Close();
        }
コード例 #2
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);
        }
コード例 #3
0
        public void SolveEquationTest()
        {
            Matrix A = new Matrix(3, 3);

            A[0, 0] = 1; A[0, 1] = 2; A[0, 2] = 3;
            A[1, 0] = -1; A[1, 1] = 1; A[1, 2] = 0;
            A[2, 0] = 1; A[2, 1] = 1; A[2, 2] = 2;

            Vector b        = new Vector(0, 0, 0);
            Vector expected = null;
            Vector actual;

            actual = GaussElimination.SolveEquation(A, b);
            Assert.AreEqual(expected, actual);
            //Assert.Inconclusive("Verify the correctness of this test method.");
        }
コード例 #4
0
ファイル: Matrix.cs プロジェクト: chargen/AGI-X0
        public Matrix inverse()
        {
            Trace.Assert(width == height, "Inverse only valid for nxn matrix!");
            uint size = width;

            Matrix internalMatrix = new Matrix(size * 2, size);

            // transfer input to internal matrix
            for (int row = 0; row < size; row++)
            {
                for (int column = 0; column < size; column++)
                {
                    internalMatrix[row, column] = this[row, column];
                }
            }
            // transfer identity to internal matrix
            for (int row = 0; row < size; row++)
            {
                for (int column = 0; column < size; column++)
                {
                    internalMatrix[row, column + (int)size] = (double)0.0;
                }
            }

            for (int i = 0; i < size; i++)
            {
                internalMatrix[i, i + (int)size] = (double)1.0;
            }

            GaussElimination.standardGaussElimination(internalMatrix);

            Matrix result = new Matrix(size, size);

            // transfer result from matrix to result
            for (int row = 0; row < size; row++)
            {
                for (int column = 0; column < size; column++)
                {
                    result[row, column] = internalMatrix[row, column + (int)size];
                }
            }

            return(result);
        }
コード例 #5
0
        public void InvertMatrixTest()
        {
            Matrix A = new Matrix(3, 3);

            A[0, 0] = 1; A[0, 1] = 2; A[0, 2] = 0;
            A[1, 0] = 2; A[1, 1] = 3; A[1, 2] = 0;
            A[2, 0] = 3; A[2, 1] = 4; A[2, 2] = 1;

            Matrix Ai = new Matrix(3, 3);

            Ai[0, 0] = -3; Ai[0, 1] = 2; Ai[0, 2] = 0;
            Ai[1, 0] = 2; Ai[1, 1] = -1; Ai[1, 2] = 0;
            Ai[2, 0] = 1; Ai[2, 1] = -2; Ai[2, 2] = 1;

            Matrix actual;

            actual = GaussElimination.InvertMatrix(A);
            Assert.AreEqual(Ai, actual);
        }