使用Gauss-Jordan 消元法的计算

**学校的作业,让程序最后输出成如图这样,代码看了半天不知道怎么搞,自己的代码输出后是第三张图那样,一堆乱数,实在不明白,哪位高手来替小弟解下惑!

**

图片说明

图片说明

图片说明

图片说明

代码~~~~~~~

#include

#define N 100

void output(int n, int st, double a[100][N], double b[100])

{

int i, j;

printf("\n\nStep  %3d\n", st);

for (i = 1;i <= n;i++) {

for (j = 1;j <= n;j++)

printf(" %8.2f", a[i][j]);

printf(" %8.2f\n", b[i]);

}

}

int main(void)

{

int i, j, k, n, q;

double p;

double a[N][N], b[N], x[N];

printf("namber of demension(n) =");

scanf("%d", &n);

for (i = 1;i <= n;i++) {

for (j = 1;j <= n;j++) {

printf("a[%d][%d] =", i, j);

scanf("%lf", &a[i][j]);

}

printf("b[%d] =", i);

scanf("%lf", &b[i]);

}

printf("\n\nGauss eliminaition method\n\n");

printf("input data\n");

for (j = 1;j <= n;j++)

printf(" x%d", j);

printf(" rhs\n");

for (i = 1;i <= n;i++) {

for (j = 1;j <= n;j++)

printf(" %8.2f", a[i][j]);

printf(" %8.2f\n", b[i]);

}

for (i = 1;i <= n;i++) {

p = a[i][i];

for (j = i;j <= n;j++)

a[i][j] = a[i][j] / p;

b[i] = b[i] / p;

for (k = 1;k <= n;k++) {

if (k != i) {

q = a[k][j];

for (j = i;j <= n;j++)

a[k][j] = a[k][j] - q * a[i][j];

b[k] = b[k] - q * b[i];

}

}

output(n, i, a, b);

}

printf("\n\n solution :\n\n");

for (i = 1; i <= n; i++)

printf(" x[%d] =b[%d] %8.2f¥n", i, x[i], b[i]);

return 0;

}

回答

https://blog.csdn.net/zhangxaochen/article/details/8020668

以上是 使用Gauss-Jordan 消元法的计算 的全部内容, 来源链接: utcz.com/a/38565.html

回到顶部