C++ 高斯消去的逻辑误差

C++ 高斯消去的逻辑误差,c++,c,math,C++,C,Math,高斯消去码的逻辑错误问题…这段代码来自1990年代我的数值方法文本。代码是从书中输入的-没有产生正确的输出 样本运行: SOLUTION OF SIMULTANEOUS LINEAR EQUATIONS USING GAUSSIAN ELIMINATION This program uses Gaussian Elimination to solve the system Ax = B, where A is the matrix of known c

高斯消去码的逻辑错误问题…这段代码来自1990年代我的数值方法文本。代码是从书中输入的-没有产生正确的输出

样本运行:

         SOLUTION OF SIMULTANEOUS LINEAR EQUATIONS
         USING GAUSSIAN ELIMINATION

 This program uses Gaussian Elimination to solve the
 system Ax = B, where A is the matrix of known
 coefficients, B is the vector of known constants
 and x is the column matrix of the unknowns.
 Number of equations: 3

 Enter elements of matrix [A]
 A(1,1) = 0
 A(1,2) = -6
 A(1,3) = 9
 A(2,1) = 7
 A(2,2) = 0
 A(2,3) = -5
 A(3,1) = 5
 A(3,2) = -8
 A(3,3) = 6

 Enter elements of [b] vector
 B(1) = -3
 B(2) = 3
 B(3) = -4

         SOLUTION OF SIMULTANEOUS LINEAR EQUATIONS

         The solution is
         x(1) = 0.000000
         x(2) = -1.#IND00
         x(3) = -1.#IND00
         Determinant = -1.#IND00
Press any key to continue . . .
从文本中复制的代码

//Modified Code from C Numerical Methods Text- June 2009

#include <stdio.h>
#include <math.h>
#define MAXSIZE 20

//function prototype
int gauss (double a[][MAXSIZE], double b[], int n, double *det);

int main(void)
{
    double a[MAXSIZE][MAXSIZE], b[MAXSIZE], det;
    int i, j, n, retval;

    printf("\n \t SOLUTION OF SIMULTANEOUS LINEAR EQUATIONS");
    printf("\n \t USING GAUSSIAN ELIMINATION \n");
    printf("\n This program uses Gaussian Elimination to solve the");
    printf("\n system Ax = B, where A is the matrix of known");
    printf("\n coefficients, B is the vector of known constants");
    printf("\n and x is the column matrix of the unknowns.");

    //get number of equations
    n = 0;
    while(n <= 0 || n > MAXSIZE)
    {
        printf("\n Number of equations: ");
        scanf ("%d", &n);
    }

    //read matrix A
    printf("\n Enter elements of matrix [A]\n");
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
        {
            printf(" A(%d,%d) = ", i + 1, j + 1);
            scanf("%lf", &a[i][j]);
        }
    //read {B} vector
    printf("\n Enter elements of [b] vector\n");
    for (i = 0; i < n; i++)
    {
        printf(" B(%d) = ", i + 1);
        scanf("%lf", &b[i]);
    }

    //call Gauss elimination function
    retval = gauss(a, b, n, &det);

    //print results
    if (retval == 0)
    {
        printf("\n\t SOLUTION OF SIMULTANEOUS LINEAR EQUATIONS\n");
        printf("\n\t The solution is");
        for (i = 0; i < n; i++)
            printf("\n \t x(%d) = %lf", i + 1, b[i]);
        printf("\n \t Determinant = %lf \n", det);
    }
    else
        printf("\n \t SINGULAR MATRIX \n");

    return 0;
 }

/* Solves the system of equations [A]{x} = {B} using       */
/* the Gaussian elimination method with partial pivoting.  */
/* Parameters:                                             */
/*      n         - number of equations                    */
/*      a[n][n]   - coefficient matrix                     */
/*      b[n]      - right-hand side vector                 */
/*      *det      - determinant of [A]                     */

int gauss (double a[][MAXSIZE], double b[], int n, double *det)
{
    double tol, temp, mult;
    int npivot, i, j, l, k, flag;

    //initialization
    *det = 1.0;
    tol = 1e-30;        //initial tolerance value
    npivot = 0;
        //mult = 0;

    //forward elimination
    for (k = 0; k < n; k++)
    {
        //search for max coefficient in pivot row- a[k][k] pivot element
        for (i = k + 1; i < n; i++)
        {
            if (fabs(a[i][k]) > fabs(a[k][k]))
            {
                //interchange row with maxium element with pivot row
                npivot++;
                for (l = 0; l < n; l++)
                {
                   temp = a[i][l];
                   a[i][l] = a[k][l];
                   a[k][l] = temp;
                }
                temp = b[i];
                b[i] = b[k];
                b[k] = temp;
            }
        }
        //test for singularity
        if (fabs(a[k][k]) < tol)
        {
           //matrix is singular- terminate
           flag = 1;
           return flag;
        }
        //compute determinant- the product of the pivot elements
        *det = *det * a[k][k];

        //eliminate the coefficients of X(I)
        for (i = k; i < n; i++)
        {
            mult = a[i][k] / a[k][k];
            b[i] = b[i] - b[k] * mult;  //compute constants
            for (j = k; j < n; j++)     //compute coefficients
                a[i][j] = a[i][j] - a[k][j] * mult;
        }
    }
    //adjust the sign of the determinant
    if(npivot % 2 == 1)
      *det = *det * (-1.0);

    //backsubstitution
    b[n] = b[n] / a[n][n];
    for(i = n - 1; i > 1; i--)
    {
        for(j = n; j > i + 1; j--)
            b[i] = b[i] - a[i][j] * b[j];
        b[i] = b[i] / a[i - 1][i];
    }
    flag = 0;
    return flag;
}
//修改了C数值方法文本中的代码-2009年6月
#包括
#包括
#定义最大尺寸20
//功能原型
整数高斯(双a[][MAXSIZE],双b[],整数n,双*det);
内部主(空)
{
双a[MAXSIZE][MAXSIZE],b[MAXSIZE],det;
int i,j,n,retval;
printf(“\n\t联立线性方程组的解”);
printf(“\n\t使用高斯消去法”);
printf(“\n此程序使用高斯消去法来解决此问题”);
printf(“\n system Ax=B,其中A是已知参数的矩阵”);
printf(“\n系数,B是已知常数的向量”);
printf(“\n和x是未知数的列矩阵。”);
//得到方程的个数
n=0;
while(n最大尺寸)
{
printf(“\n方程数:”);
scanf(“%d”和“&n”);
}
//读取矩阵A
printf(“\n输入矩阵[A]\n]的元素”;
对于(i=0;ifabs(a[k][k]))
{
//将行与最大元素交换为轴行
npivot++;
对于(l=0;l1;i--)
{
对于(j=n;j>i+1;j--)
b[i]=b[i]-a[i][j]*b[j];
b[i]=b[i]/a[i-1][i];
}
flag=0;
返回标志;
}
溶液应为:1.058824、1.823529、0.882353,det为-102.000000


任何洞察都是值得赞赏的…

这可能不会以您想要的方式回答您的问题,但是编程您自己的数值稳定矩阵算法与自己动手一样是明智的


有一个非常好的库,它是由一个著名的源代码(NIST)调用的,它在C++中做初等矩阵运算。要求解Ax=B,首先使用因子A(这是一种很好的通用方法,可以使用LU,但它在数值上不太稳定),然后调用
solve(B)
。这既适用于精确的平方矩阵(取决于数值计算问题),也适用于超定系统,在超定系统中,您可以得到最小二乘答案。

这可能无法以您预期的方式回答您的问题,但是,编写自己的数值稳定矩阵算法和自己动手做手术一样明智

//eliminate the coefficients of X(I)
for (i = k; i < n; i++)
有一个非常好的库,它是由一个著名的源代码(NIST)调用的,它在C++中做初等矩阵运算。要求解Ax=B,首先使用因子A(这是一种很好的通用方法,可以使用LU,但它在数值上不太稳定),然后调用
solve(B)
。这既适用于精确的平方矩阵(取决于数值计算问题),也适用于超定系统,可以得到最小二乘答案。

//消除X(I)的系数
//eliminate the coefficients of X(I)
for (i = k; i < n; i++)
对于(i=k;i
这应该是吗

for (i = k + 1; i < n; i++)
(i=k+1;i 按照现在的方式,我相信这将导致您将pivot行本身分割,并将其归零。

//消除X(I)的系数
对于(i=k;i
这应该是吗

for (i = k + 1; i < n; i++)
(i=k+1;i 现在的情况是,我相信这将导致你用I来划分枢轴行