本文主要是介绍高斯-塞德尔迭代法Gauss-Seidel_解线性方程组的迭代法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
高斯-塞德尔迭代法Gauss-Seidel_解线性方程组的迭代法
标签:计算方法实验
#include <stdio.h>
#include <math.h>#define maxn 3int main()
{double a[maxn][maxn + 1], x[maxn] = {0};double eps = 1e-9;int n, k, kmax = 100;freopen("gauss.txt", "r", stdin);scanf("%d", &n);for(int i = 0; i < n; i++)for(int j = 0; j < n + 1; j++)scanf("%lf", &a[i][j]);//for(int i = 0; i < n; i++)//{//for(int j = 0; j < n + 1; j++) printf("%-15f", a[i][j]);//printf("\n");//}for(k = 0; k < kmax; k++){double norm = 0;for(int i = 0; i < n; i++){double x0 = x[i];double sum = 0;for(int j = 0; j < n; j++) if(j != i) sum += a[i][j] * x[j]; ///x[i] = (a[i][n] - sum) / a[i][i]; //nif(fabs(x[i] - x0) > norm) norm = fabs(x[i] - x0); //norm}printf("\nk = %2d x = ", k + 1);for(int i = 0; i < n; i++) printf("%-15f", x[i]);if(norm < eps) break;}if(k < kmax){printf("\n\nk = %d\n", k + 1);for(int i = 0; i < n; i++) printf("x%d = %-15f\n", i + 1, x[i]);}else printf("\n\nfailed\n");return 0;
}
数据文件
实验结果
这篇关于高斯-塞德尔迭代法Gauss-Seidel_解线性方程组的迭代法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!