C 用Gauss-Seidel解系统的函数

C 用Gauss-Seidel解系统的函数,c,function,gaussian,C,Function,Gaussian,我试图写一个函数,用高斯-赛德尔方法来解方程组。到目前为止,我的功能还不能完成这项工作。代码在“输入初始猜测…”后停止给我输出,并且无法解决我输入的函数。这是我的函数还是代码的其他部分的问题 /* code to solve a nxn system using the Gauss-Seidel method */ #include <stdio.h> #include <math.h> #include <stdlib.h> #define MAX_DIM

我试图写一个函数,用高斯-赛德尔方法来解方程组。到目前为止,我的功能还不能完成这项工作。代码在“输入初始猜测…”后停止给我输出,并且无法解决我输入的函数。这是我的函数还是代码的其他部分的问题

/* code to solve a nxn system using the Gauss-Seidel method */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAX_DIM 100
#define MAX_ITER 500
#define TOLERANCE 1.e-6

void gauss_seidel(double a[][MAX_DIM], double b[], double x[], int n);
void main()
{       
    int i, j, n;
    int violation_counter, answer;
    int violation_rows[MAX_DIM];
    double sum;
    double a[MAX_DIM][MAX_DIM];
    double b[MAX_DIM], x[MAX_DIM];

    /* read in data */
    n = MAX_DIM + 1;
    while (n > MAX_DIM) {
        printf("Enter the dimension of the system to be solved: ");
            scanf_s("%d", &n);
    }
    printf("\n");
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            printf("Enter a[%d][%d] element of the system matrix: ",i,j);
                scanf_s("%lf", &a[i][j]);
        }
        printf("Enter b[%d] of the right-hand-side vector: ", i);
        scanf_s("%lf", &b[i]);
        printf("\n");
    }

    /* test the convergence criterion */
    violation_counter = 0;
    for (i = 0; i < n; i++) {
        sum = 0.0;
        for (j = 0; j < n; j++)
            if (i != j)
                sum = sum + fabs(a[i][j]);
        if (fabs(a[i][i]) < sum) {
            violation_rows[violation_counter] = i;
            violation_counter = violation_counter + 1;
        }
        if (a[i][i] == 0.0) {
            printf("Found diagonal element equal to zero; rearrange equations; exiting ...\n");
                exit(0);
        }
    }
    if (violation_counter > 0) {
        printf("The Gauss-Seidel convergence criterion is violated in %d rows out of %d\n", violation_counter, n);
            printf("Specifically, it was violated in rows:\n");
        for (i = 0; i < violation_counter; i++)
            printf("%d ", violation_rows[i]);
        printf("\n");
        printf("Enter 1 if you want to continue; any other number to abort : ");
            scanf_s("%d", &answer);
        if (answer != 1)
            exit(1);
        printf("Check results carefully\n\n");
    }

    /* initialize the solution vector -- initial guesses */
    for (i = 0; i < n; i++) {
        printf("Enter an initial guess for x[%d] of the solution vector : ",i);
            scanf_s("%lf", &x[i]);
    }

    /* solve the system */
    gauss_seidel(a, b, x, n);

    /* output solution */
    for (i = 0; i < n; i++)
        printf("x[%d]=%f\n", i, x[i]);
    printf("\n");
}

/* function to solve a system using Gauss-Seidel */
void gauss_seidel(double a[][MAX_DIM], double b[], double x[], int n)
{
    double maxerror = 1.0;
    double iteration_error;
    double e, sum, temp;
    int i, j;

    while (maxerror > 1.e-6) {
        iteration_error = 0.0;

        for (i = 0; i < n; i++) {
            sum = 0.0;
            for (j = 0; j < n; j++) {
                if (i != j)
                    sum = sum + (a[i][j] * x[j]);
            }
        }

        temp = (a[i][n] - sum) / a[i][i];
        e = fabs((temp - x[i]) / x[i]);
        x[i] = temp;

        if (e > iteration_error)
            iteration_error = e;
    }
    maxerror = iteration_error;
}
/*使用Gauss-Seidel方法求解nxn系统的代码*/
#包括
#包括
#包括
#定义最大尺寸100
#定义最大ITER 500
#定义公差1.e-6
void gauss_seidel(双a[][MAX_DIM],双b[],双x[],整数n);
void main()
{       
inti,j,n;
计数器,回答;
整数行[最大值];
双和;
双a[最大尺寸][最大尺寸];
双b[MAX_DIM],x[MAX_DIM];
/*读入数据*/
n=最大尺寸+1;
而(n>最大值){
printf(“输入要求解的系统的维度:”);
扫描频率(“%d”和“&n”);
}
printf(“\n”);
对于(i=0;i0){
printf(“在%d\n中的%d行中违反了Gauss-Seidel收敛标准”,违反计数器,n);
printf(“具体地说,它在以下行中被违反:\n”);
对于(i=0;i1.e-6){
迭代误差=0.0;
对于(i=0;i迭代错误)
迭代误差=e;
}
最大误差=迭代误差;
}

请改进代码的缩进。在目前的状态下很难阅读。你说的“它停在……”是什么意思?你是在调试器中运行的吗?您收到错误消息了吗?在gauss_seidel()中,您只在while循环之后更改maxerror,因此while循环永远不会完成。@Georg这就是为什么输出在“输入初始猜测…”处停止的原因?。那么我假设这是函数中的一个错误?每次使用它时,都应该检查
scanf_s()
中的返回值。是的,如果带有“输入初始猜测”的循环完成,下一步将跳转到
gauss_seidel()
,正如Georg所说,函数不会返回,因为1.0E0>1.0E-6是永久性的。