作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我必须设计一种算法作为正向消元法的扩展,在矩阵上进行高斯约旦消元法。我的程序正在执行并创建数字的对角线,但它们并不都是 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
最佳答案
根据算法,A[j][i] > A[pivotrow][i]
应该是 |A[j][i]| > |A[pivotrow][i
]|,两者都是绝对值。你的交换功能在哪里?我认为 C++ 没有自己的 swap(int[][] a, int[][]b)
。
关于c++ - 高斯-若尔当消元法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15752364/
我是一名优秀的程序员,十分优秀!