用c语言设计函数用列主元高斯消去法、Jacobi 迭代法、Gauss-Seidel 迭代法解如下方程组输出方程组的解,输出矩阵 L 和 U
时间: 2024-02-11 21:05:24 浏览: 80
好的,我来为您解答这个问题。
首先,我们需要定义一个函数来实现列主元高斯消去法,Jacobi迭代法和Gauss-Seidel迭代法。下面是这个函数的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// 列主元高斯消去法
void gauss_elimination(double **A, double *b, int n, double *x) {
int i, j, k, max_row;
double max_elem, tmp;
// 高斯消元
for (k = 0; k < n - 1; k++) {
max_elem = fabs(A[k][k]);
max_row = k;
for (i = k + 1; i < n; i++) {
if (fabs(A[i][k]) > max_elem) {
max_elem = fabs(A[i][k]);
max_row = i;
}
}
if (max_elem == 0) {
printf("Error: Singular matrix!\n");
return;
}
// 交换k行和max_row行
for (j = k; j < n; j++) {
tmp = A[k][j];
A[k][j] = A[max_row][j];
A[max_row][j] = tmp;
}
tmp = b[k];
b[k] = b[max_row];
b[max_row] = tmp;
// 消元
for (i = k + 1; i < n; i++) {
tmp = A[i][k] / A[k][k];
for (j = k + 1; j < n; j++) {
A[i][j] = A[i][j] - tmp * A[k][j];
}
b[i] = b[i] - tmp * b[k];
}
}
// 回带求解
for (i = n - 1; i >= 0; i--) {
for (j = i + 1; j < n; j++) {
b[i] = b[i] - A[i][j] * x[j];
}
x[i] = b[i] / A[i][i];
}
}
// Jacobi迭代法
void jacobi_iteration(double **A, double *b, int n, double *x, double eps) {
int i, j, k;
double *x_new = (double *)malloc(n * sizeof(double));
double error;
// 初始化
for (i = 0; i < n; i++) {
x_new[i] = x[i];
}
// 迭代
for (k = 0; k < 10000; k++) {
error = 0;
for (i = 0; i < n; i++) {
x[i] = b[i];
for (j = 0; j < n; j++) {
if (j != i) {
x[i] = x[i] - A[i][j] * x_new[j];
}
}
x[i] = x[i] / A[i][i];
error = fmax(error, fabs(x_new[i] - x[i]));
}
if (error < eps) {
break;
}
for (i = 0; i < n; i++) {
x_new[i] = x[i];
}
}
free(x_new);
}
// Gauss-Seidel迭代法
void gauss_seidel_iteration(double **A, double *b, int n, double *x, double eps) {
int i, j, k;
double *x_new = (double *)malloc(n * sizeof(double));
double error;
// 初始化
for (i = 0; i < n; i++) {
x_new[i] = x[i];
}
// 迭代
for (k = 0; k < 10000; k++) {
error = 0;
for (i = 0; i < n; i++) {
x[i] = b[i];
for (j = 0; j < n; j++) {
if (j != i) {
x[i] = x[i] - A[i][j] * x[j];
}
}
x[i] = x[i] / A[i][i];
error = fmax(error, fabs(x_new[i] - x[i]));
x_new[i] = x[i];
}
if (error < eps) {
break;
}
}
free(x_new);
}
// 输出矩阵L和U
void output_LU(double **A, int n) {
int i, j;
double **L = (double **)malloc(n * sizeof(double *));
double **U = (double **)malloc(n * sizeof(double *));
for (i = 0; i < n; i++) {
L[i] = (double *)malloc(n * sizeof(double));
U[i] = (double *)malloc(n * sizeof(double));
}
// LU分解
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i <= j) {
U[i][j] = A[i][j];
for (k = 0; k < i; k++) {
U[i][j] = U[i][j] - L[i][k] * U[k][j];
}
if (i == j) {
L[i][j] = 1;
} else {
L[i][j] = 0;
}
} else {
L[i][j] = A[i][j] / U[j][j];
U[i][j] = 0;
}
}
}
// 输出L和U
printf("L = \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%lf ", L[i][j]);
}
printf("\n");
}
printf("U = \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%lf ", U[i][j]);
}
printf("\n");
}
// 释放内存
for (i = 0; i < n; i++) {
free(L[i]);
free(U[i]);
}
free(L);
free(U);
}
// 测试主函数
int main() {
double **A, *b, *x;
int n, i, j;
printf("请输入方程组的个数n:");
scanf("%d", &n);
// 初始化矩阵和向量
A = (double **)malloc(n * sizeof(double *));
b = (double *)malloc(n * sizeof(double));
x = (double *)malloc(n * sizeof(double));
for (i = 0; i < n; i++) {
A[i] = (double *)malloc(n * sizeof(double));
for (j = 0; j < n; j++) {
printf("请输入矩阵A[%d][%d]的值:", i, j);
scanf("%lf", &A[i][j]);
}
printf("请输入向量b[%d]的值:", i);
scanf("%lf", &b[i]);
x[i] = 0; // 初始化解向量
}
// 列主元高斯消去法求解
gauss_elimination(A, b, n, x);
printf("列主元高斯消去法求解结果:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = %lf\n", i, x[i]);
}
// Jacobi迭代法求解
jacobi_iteration(A, b, n, x, 1e-6);
printf("Jacobi迭代法求解结果:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = %lf\n", i, x[i]);
}
// Gauss-Seidel迭代法求解
gauss_seidel_iteration(A, b, n, x, 1e-6);
printf("Gauss-Seidel迭代法求解结果:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = %lf\n", i, x[i]);
}
// 输出矩阵L和U
output_LU(A, n);
// 释放内存
for (i = 0; i < n; i++) {
free(A[i]);
}
free(A);
free(b);
free(x);
return 0;
}
```
在这个函数中,我们首先定义了三个函数:`gauss_elimination`,`jacobi_iteration` 和 `gauss_seidel_iteration`,它们分别实现了列主元高斯消去法,Jacobi迭代法和Gauss-Seidel迭代法。然后,我们定义了一个输出矩阵L和U的函数`output_LU`。最后,在主函数中,我们输入了方程组的个数n,初始化了矩阵A和向量b,分别用列主元高斯消去法,Jacobi迭代法和Gauss-Seidel迭代法求解方程组,并输出了结果和矩阵L和U。
希望这个代码能够帮到您。
阅读全文