2 my3495 my3495 于 2016.04.11 16:38 提问

情各位大侠帮忙求解,C#多元1次方程求解
c#

求各位大侠帮忙。谢谢!

-382* X1+ 552 * X2 - 170* X3=12
-170* X3 + 340 * X4 - 170* X5=17
-170* X5 + 340 * X6 - 170* X7=17
......

2个回答

caozhy
caozhy   Ds   Rxr 2016.04.11 16:40
herozhangbz
herozhangbz   2016.04.11 17:14

网上百度的http://zhidao.baidu.com/link?url=GnbaGfjhl1FisfKzUcEpsF4M6tuuf2tfeJY-k4SHiHtyeKQlwAdEK4rcODteRi4fRXM-zsUOxknRDZzg7u-Xmq

      void gaoss(double[,] a)//高斯消元求未知数X,
        {
            int _rows = a.GetLength(0),_cols = a.GetLength(1);//_rows是指方程组行数;_cols是列数
            string print = "";
            int L = _rows - 1;
            int i, j, l, n, m, k = 0;
            double[] temp1 = new double[_rows];

            /*第一个do-while是将增广矩阵消成上三角形式*/
            do
            {
                n = 0;
                for (l = k; l < L; l++)

                    temp1[n++] = a[l + 1, k] / a[k, k];
                for (m = 0, i = k + 1; i < _rows; i++, m++)
                {
                    for (j = k; j < _cols; j++)
                        a[i, j] -= temp1[m] * a[k, j];
                }

                k++;

            } while (k < _rows);

            ///*第二个do-while是将矩阵消成对角形式,并且重新给k赋值,最后只剩下对角线和最后一列的数,其它都为0*/
            k = L - 1;

            do
            {
                n = 0;
                for (l = k; l >= 0; l--)
                    temp1[n++] = a[k - l, k + 1] / a[k + 1, k + 1];
                for (m = 0, i = k; i >= 0; i--, m++)
                {
                    for (j = k; j < _cols; j++)
                        a[k - i, j] -= temp1[m] * a[k + 1, j];
                }
                k--;

            } while (k >= 0);
            /*下一个for是解方程组*/
            for (i = 0; i < _rows; i++)
            {
                double value = a[i, _rows] / a[i, i];
                print += "X" + (i + 1) + "=" + value + " ";

            }
            MessageBox.Show(print, "方程的解为:");

        }

自己手动或者根据方程的string截取各个元素的前面的数字组成二维数组

            double[,] db =new double[4,8] { {-382,552,-170,0,0,0,0,12 },{0,0,-170,340,-170,0,0,17 }, {0,0,0,0,-170,340,-170,17 }, {0,0,0,0,0,0,0,0 } };
            gaoss(db);
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!