my3495 于 2016.04.11 16:38 提问

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

2个回答

caozhy      2016.04.11 16:40
herozhangbz   2016.04.11 17:14
``````      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, "方程的解为：");

}
``````

``````            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);
``````