实现方程组求解中的高斯消去法c++代码
时间: 2023-10-08 14:07:59 浏览: 46
以下是一个简单的高斯消去法的C++实现:
```c++
#include <iostream>
#include <cmath>
using namespace std;
const double EPSILON = 1e-10; // 用于判断浮点数是否为0
void printMatrix(double **matrix, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j <= n; j++) {
cout << matrix[i][j] << "\t";
}
cout << endl;
}
}
void swapRow(double **matrix, int i, int j, int n) {
double *temp = new double[n+1];
for (int k = 0; k <= n; k++) {
temp[k] = matrix[i][k];
matrix[i][k] = matrix[j][k];
matrix[j][k] = temp[k];
}
delete[] temp;
}
void gaussElimination(double **matrix, int n) {
for (int i = 0; i < n; i++) {
// 选择最大的系数作为主元
int maxRow = i;
for (int j = i+1; j < n; j++) {
if (abs(matrix[j][i]) > abs(matrix[maxRow][i])) {
maxRow = j;
}
}
if (maxRow != i) {
swapRow(matrix, i, maxRow, n);
}
// 消元
for (int j = i+1; j < n; j++) {
double factor = matrix[j][i] / matrix[i][i];
for (int k = i; k <= n; k++) {
matrix[j][k] -= factor * matrix[i][k];
}
}
}
}
void backSubstitution(double **matrix, int n, double *result) {
for (int i = n-1; i >= 0; i--) {
double sum = 0.0;
for (int j = i+1; j < n; j++) {
sum += matrix[i][j] * result[j];
}
result[i] = (matrix[i][n] - sum) / matrix[i][i];
}
}
void solveEquations(double **matrix, int n, double *result) {
gaussElimination(matrix, n);
backSubstitution(matrix, n, result);
}
int main() {
int n;
cout << "请输入未知数的个数:";
cin >> n;
double **matrix = new double*[n];
for (int i = 0; i < n; i++) {
matrix[i] = new double[n+1];
cout << "请输入第 " << i+1 << " 行系数:";
for (int j = 0; j <= n; j++) {
cin >> matrix[i][j];
}
}
double *result = new double[n];
solveEquations(matrix, n, result);
cout << "解为:";
for (int i = 0; i < n; i++) {
if (abs(result[i]) < EPSILON) {
result[i] = 0.0;
}
cout << result[i] << " ";
}
cout << endl;
for (int i = 0; i < n; i++) {
delete[] matrix[i];
}
delete[] matrix;
delete[] result;
return 0;
}
```
该代码中,`gaussElimination`和`backSubstitution`分别实现了高斯消元和回带代入两个步骤。其中,`swapRow`函数用于交换矩阵的两行,以使最大的系数尽可能靠前,避免除数为0的情况。`printMatrix`函数用于输出矩阵,方便调试。`EPSILON`是一个很小的数,用于判断浮点数是否为0。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)