高斯消除期间分段错误(核心转储)

发布于 2024-10-20 21:46:24 字数 2382 浏览 0 评论 0原文

在运行该程序期间,我遇到了分段错误。我正在尝试使用从 .dat 文件接收的数字来求解线性方程。我可以在高斯函数之前到达该部分,但它会在之后转储。有什么帮助吗?谢谢。

    void prob2(void)
{
        FILE *matrix;

        matrix=fopen("matrix.dat", "r");
        double a1, a2, a3, a4, **A, *a, *rhs, length;
        int k, row;
        A=(double **)malloc(4*sizeof(double *));
        printf("\n");
        for(k=1;k<=4; k++) 
        {
                a=(double *)malloc(4*sizeof(double));
                fscanf(matrix, "%lf %lf %lf %lf", &a1, &a2, &a3, &a4);
                a[0]=a1;
                a[1]=a2;
                a[2]=a3;
                a[3]=a4;
                printf(" a[%d][] = %5.2f %5.2f %5.2f %5.2f\n", k, a[0], a[1], a[2], a[3]);
        }
        rhs=(double *)malloc(4*sizeof(double));
        fscanf(matrix, "%lf %lf %lf %lf", &a1, &a2, &a3, &a4);
        rhs[0]=a1;
        rhs[1]=a2;
        rhs[2]=a3;
        rhs[3]=a4;
        printf("\nb[]={  %.3f,   %.3f,  %.3f,   %.3f  }\n", rhs[0],rhs[1],rhs[2],rhs[3]);
        printf("hiii");
        gauss(4, A, rhs);
        /* print the solution x[] stored in rhs[]:*/
        printf("x[]={%7.3f, %7.3f, %7.3f, %7.3f  }\n", 
        rhs[0], rhs[1], rhs[2], rhs[3]);
        length=sqrt(pow(rhs[0],2)+pow(rhs[1],2)+pow(rhs[2],2)+pow(rhs[3],2));
        printf("The length of x[] is  %.6lf", length);
        /* free memory */
        for(row=0; row<4; row++) free(A[row]);
        free(A); 
        free(rhs);
        fclose(matrix);

}

void gauss(int n, double **A, double *rhs)
{
/* By Gauss elimination, solve a system of equations:
   A[][]*x[] = rhs[] where A[][] (n x n)
   and rhs[] (n x 1) are input */

/* x[] is stored in rhs[] */
printf("hiii");
double one = 1.0, zero=0.0;
double b, c, d;
int nm, row, col, krow;
nm = n - 1;
if(n == 1)
{
rhs[0] /= A[0][0]; A[0][0] = one;
return;
}
/* forward reduction */
for(row=0; row<nm; row++)
{
b = A[row][row]; A[row][row] = one;
for(col=row+1; col<n; col++) A[row][col] /= b;
rhs[row] /= b;
/* sweep rows of A[row+1][] to A[nm][] */
for(krow=row+1; krow<n; krow++)
{
c = A[krow][row]; A[krow][row] = zero;
for(col=row+1; col<n; col++) A[krow][col] -= c * A[row][col];
rhs[krow] -= c * rhs[row];
}
}
/* back substitution */

rhs[nm] /=A[nm][nm];
for(row=nm-1; row>=0; row--)
{
for(col=row+1; col<n; col++) rhs[row] -= A[row][col]*rhs[col];
}
return;
}

During the running of this program, i get a segmentation fault. Im trying to solve a linear equation, with numbers received from a .dat file. I can get to the part right before the gauss function, but it dumps afterwards. Any help? thanks.

    void prob2(void)
{
        FILE *matrix;

        matrix=fopen("matrix.dat", "r");
        double a1, a2, a3, a4, **A, *a, *rhs, length;
        int k, row;
        A=(double **)malloc(4*sizeof(double *));
        printf("\n");
        for(k=1;k<=4; k++) 
        {
                a=(double *)malloc(4*sizeof(double));
                fscanf(matrix, "%lf %lf %lf %lf", &a1, &a2, &a3, &a4);
                a[0]=a1;
                a[1]=a2;
                a[2]=a3;
                a[3]=a4;
                printf(" a[%d][] = %5.2f %5.2f %5.2f %5.2f\n", k, a[0], a[1], a[2], a[3]);
        }
        rhs=(double *)malloc(4*sizeof(double));
        fscanf(matrix, "%lf %lf %lf %lf", &a1, &a2, &a3, &a4);
        rhs[0]=a1;
        rhs[1]=a2;
        rhs[2]=a3;
        rhs[3]=a4;
        printf("\nb[]={  %.3f,   %.3f,  %.3f,   %.3f  }\n", rhs[0],rhs[1],rhs[2],rhs[3]);
        printf("hiii");
        gauss(4, A, rhs);
        /* print the solution x[] stored in rhs[]:*/
        printf("x[]={%7.3f, %7.3f, %7.3f, %7.3f  }\n", 
        rhs[0], rhs[1], rhs[2], rhs[3]);
        length=sqrt(pow(rhs[0],2)+pow(rhs[1],2)+pow(rhs[2],2)+pow(rhs[3],2));
        printf("The length of x[] is  %.6lf", length);
        /* free memory */
        for(row=0; row<4; row++) free(A[row]);
        free(A); 
        free(rhs);
        fclose(matrix);

}

void gauss(int n, double **A, double *rhs)
{
/* By Gauss elimination, solve a system of equations:
   A[][]*x[] = rhs[] where A[][] (n x n)
   and rhs[] (n x 1) are input */

/* x[] is stored in rhs[] */
printf("hiii");
double one = 1.0, zero=0.0;
double b, c, d;
int nm, row, col, krow;
nm = n - 1;
if(n == 1)
{
rhs[0] /= A[0][0]; A[0][0] = one;
return;
}
/* forward reduction */
for(row=0; row<nm; row++)
{
b = A[row][row]; A[row][row] = one;
for(col=row+1; col<n; col++) A[row][col] /= b;
rhs[row] /= b;
/* sweep rows of A[row+1][] to A[nm][] */
for(krow=row+1; krow<n; krow++)
{
c = A[krow][row]; A[krow][row] = zero;
for(col=row+1; col<n; col++) A[krow][col] -= c * A[row][col];
rhs[krow] -= c * rhs[row];
}
}
/* back substitution */

rhs[nm] /=A[nm][nm];
for(row=nm-1; row>=0; row--)
{
for(col=row+1; col<n; col++) rhs[row] -= A[row][col]*rhs[col];
}
return;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

强辩 2024-10-27 21:46:24

看起来矩阵 A 未分配。我认为分配 a 后你需要这个:

A[k-1] = a;

It looks like the matrix A is not assigned. I think you need this after allocating a:

A[k-1] = a;
微暖i 2024-10-27 21:46:24

当算法在对角线上遇到 0 时,通常会发生这种情况。您需要使用旋转来避免这种情况。有关详细信息,请参阅有关高斯消除的维基百科文章

This usually happens when the algorithm encounters a 0 on the diagonal. You need to use pivoting to avoid this. See the Wikipedia article on Gaussian elimination for details.

护你周全 2024-10-27 21:46:24

你永远不会在 A 中存储任何内容。你在 k = 1 ... 4 循环中进行 malloc,但你永远不会保存你 malloc 的内容。

You never store anything in A. You malloc in your k = 1 ... 4 loop but you never save what you malloc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文