问题描述
- 情各位大侠帮忙求解,C#多元1次方程求解
-
求各位大侠帮忙。谢谢!-382* X1+ 552 * X2 - 170* X3=12
-170* X3 + 340 * X4 - 170* X5=17
-170* X5 + 340 * X6 - 170* X7=17
......
解决方案
http://blog.csdn.net/asxinyu_usst/article/details/50703555
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);
时间: 2024-10-26 21:47:54