用列主元高斯消去法、Jacobi 迭代法、Gauss-Seidel 迭代法解如下方程组输出方程组的解,及矩阵 L 和 U。
时间: 2024-02-25 15:55:27 浏览: 85
好的,这是一个更加复杂的问题,需要分步骤来实现。以下是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 3
#define MAX_ITER 10000
#define TOLERANCE 1e-5
void printMatrix(double matrix[N][N+1]) {
for(int i=0; i<N; i++) {
for(int j=0; j<N+1; j++) {
printf("%.2f ", matrix[i][j]);
}
printf("\n");
}
}
void swapRow(double matrix[N][N+1], int i, int j) {
for(int k=0; k<N+1; k++) {
double temp = matrix[i][k];
matrix[i][k] = matrix[j][k];
matrix[j][k] = temp;
}
}
void LUPDecomposition(double matrix[N][N+1], double L[N][N], double U[N][N]) {
for(int i=0; i<N; i++) {
for(int j=0; j<N; j++) {
if(j<i) {
L[j][i] = 0;
} else {
L[j][i] = matrix[j][i]/matrix[i][i];
for(int k=0; k<i; k++) {
matrix[j][k] = matrix[j][k] - L[j][i]*matrix[i][k];
}
}
}
for(int j=i; j<N; j++) {
U[i][j] = matrix[i][j];
}
}
}
int gaussianElimination(double matrix[N][N+1], double solution[N]) {
double L[N][N], U[N][N];
LUPDecomposition(matrix, L, U);
printf("L:\n");
printMatrix(L);
printf("\nU:\n");
printMatrix(U);
printf("\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(i != maxRow) {
swapRow(matrix, i, maxRow);
}
if(matrix[i][i] == 0) {
return 0;
}
for(int j=i+1; j<N; j++) {
double factor = matrix[j][i]/matrix[i][i];
for(int k=i+1; k<N+1; k++) {
matrix[j][k] = matrix[j][k] - factor*matrix[i][k];
}
}
}
for(int i=N-1; i>=0; i--) {
solution[i] = matrix[i][N];
for(int j=i+1; j<N; j++) {
solution[i] = solution[i] - matrix[i][j]*solution[j];
}
solution[i] = solution[i]/matrix[i][i];
}
return 1;
}
int jacobi(double matrix[N][N+1], double solution[N]) {
double x[N], x_new[N];
for(int i=0; i<N; i++) {
x[i] = 0;
}
int iter = 0;
double error = TOLERANCE + 1;
while(iter < MAX_ITER && error > TOLERANCE) {
for(int i=0; i<N; i++) {
double sum = 0;
for(int j=0; j<N; j++) {
if(j != i) {
sum = sum + matrix[i][j]*x[j];
}
}
x_new[i] = (matrix[i][N] - sum)/matrix[i][i];
}
error = 0;
for(int i=0; i<N; i++) {
error = error + pow(x_new[i] - x[i], 2);
x[i] = x_new[i];
}
error = sqrt(error);
iter++;
}
if(iter == MAX_ITER) {
return 0;
} else {
for(int i=0; i<N; i++) {
solution[i] = x[i];
}
return 1;
}
}
int gaussSeidel(double matrix[N][N+1], double solution[N]) {
double x[N];
for(int i=0; i<N; i++) {
x[i] = 0;
}
int iter = 0;
double error = TOLERANCE + 1;
while(iter < MAX_ITER && error > TOLERANCE) {
for(int i=0; i<N; i++) {
double sum = 0;
for(int j=0; j<N; j++) {
if(j != i) {
sum = sum + matrix[i][j]*solution[j];
}
}
solution[i] = (matrix[i][N] - sum)/matrix[i][i];
}
error = 0;
for(int i=0; i<N; i++) {
error = error + pow(solution[i] - x[i], 2);
x[i] = solution[i];
}
error = sqrt(error);
iter++;
}
if(iter == MAX_ITER) {
return 0;
} else {
return 1;
}
}
int main() {
double matrix[N][N+1] = {{2, -1, 1, 8}, {-3, -1, 2, -11}, {-2, 1, 2, -3}};
double solution[N];
if(gaussianElimination(matrix, solution)) {
printf("Gaussian elimination:\n");
for(int i=0; i<N; i++) {
printf("%.2f\n", solution[i]);
}
printf("\n");
} else {
printf("No unique solution.\n");
}
if(jacobi(matrix, solution)) {
printf("Jacobi:\n");
for(int i=0; i<N; i++) {
printf("%.2f\n", solution[i]);
}
printf("\n");
} else {
printf("Jacobi method failed.\n");
}
if(gaussSeidel(matrix, solution)) {
printf("Gauss-Seidel:\n");
for(int i=0; i<N; i++) {
printf("%.2f\n", solution[i]);
}
printf("\n");
} else {
printf("Gauss-Seidel method failed.\n");
}
return 0;
}
```
这个程序通过列主元高斯消去法、Jacobi迭代法和Gauss-Seidel迭代法求解一个3x3的线性方程组,并输出方程组的解和矩阵L和U。你可以将输入的系数矩阵和常数向量修改为你自己的数据。注意:如果矩阵不可逆,则会输出“No unique solution.”。如果Jacobi或Gauss-Seidel方法无法收敛,则会输出“Jacobi method failed.”或“Gauss-Seidel method failed.”。
阅读全文