我必須設計一個算法作爲在矩陣上進行高斯約旦消除的前向消除的擴展。我的程序正在執行並創建數字的對角線,但它們並不全是1。它也不會訪問第一行和第一列來將它們更改爲0。最後一欄,答案應該是這樣的,不會改變。任何想法我可以做些什麼來接近解決方案?高斯 - 約旦消除
#include <cmath>
using namespace std;
double BetterForwardElimination(double A[8][9])
{
//Implements Gaussian elimination with partial pivoting
//Input: Matrix A[1..n,1..n] and column-vector b[1..n]
//Output: An equivalent upper-triangular matrix in place ofAand the
//corresponding right-hand side values in place of the (n+1)st column
//size of array
int n = 8;
//int n = sizeof(A)/sizeof(A[0]);
for (int i = 1; i<n; i++)
{
int pivotrow = i;
for (int j=i+1; j<n; j++)
{
if (A[j][i] > A[pivotrow][i])
{
pivotrow = j;
}
}
for (int k=i; k<n-1; k++)
{
swap(A[i][k], A[pivotrow][k]);
}
for (int j=i+1; j<n; j++)
{
//int temp = A[j][i]/A[i][i];
for (int k = i; k<n; k++)
{
A[j][k] = A[j][k] - A[i][k]*(A[j][i]/A[i][i]);
}
A[i][j] = 0;
}
}
return A[n][n];
}
我的輸出是這樣的:
1 1 1 1 1 1 1 1 0
1 2 0 0 0 0 0 0 0
1 0 3 0 0 0 0 0 0
1 0 0 4 0 0 0 0 0
11 0 0 0 5 0 0 0 20
1 0 0 0 0 1 0 0 34
1 0 0 0 0 0 1 0 -51
1 0 0 0 0 0 0 -1 -6
預期的輸出應該是:
1 0 0 0 0 0 0 0 2
0 1 0 0 0 0 0 0 3
0 0 1 0 0 0 0 0 5
0 0 0 1 0 0 0 0 7
0 0 0 0 1 0 0 0 -7
0 0 0 0 0 1 0 0 -5
0 0 0 0 0 0 1 0 -3
0 0 0 0 0 0 0 1 -2
預期產量是多少? – askewchan
@askewchan只是編輯它到第一篇文章 – xtheking