本文介绍了对于高斯消元逻辑错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的逻辑错误问题,高斯消元code ...这code是从1990年的我的数值方法的文字。在code从输入以下资料─在不产生正确的输出...

运行示例:

 解决线性方程组的
         使用高斯消元 这个程序使用高斯消元来解决
 系统Ax = b的,其中A是已知的矩阵
 系数,B是已知常数的矢量
 和x是未知的柱基质。
 方程数:3 输入矩阵的元素[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 输入并[b]向量的元素
 B(1)= -3
 B(2)= 3
 B(3)= -4         解线性方程组的         解决的办法是
         ×(1)= 0.000000
         ×(2)= - 1。#IND00
         X(3)= -1。#IND00
         行列式= -1。#IND00
preSS任意键继续。 。 。

从文本复制的code ...

  //修改$ C $从C的数值方法2009年文本 - 六月Ç#包括LT&;&stdio.h中GT;
#包括LT&;&math.h中GT;
#定义MAXSIZE 20//函数原型
INT高斯(双A [] [MAXSIZE],重B [],诠释N,双* DET);INT主要(无效)
{
    双A [MAXSIZE] [MAXSIZE],B [MAXSIZE],DET;
    INT I,J,N,RETVAL;    输出(的线性方程组\\ n \\ T溶液);
    的printf(\\ n \\ T使用高斯消元\\ n);
    的printf(\\ n这个程序使用高斯消元,解决了);
    的printf(\\ n系统AX = B,其中A是已知矩阵);
    的printf(\\ n个系数,B是已知常数的矢量);
    的printf(\\ n和x是未知的柱基质。);    //获取方程的数
    N = 0;
    而(N< = 0 || N'GT; MAXSIZE)
    {
    的printf(方程\\ N多:);
    scanf函数(%d个,&安培; N);
    }    //读取矩阵A
    的printf(\\ n输入矩阵的元素[A] \\ n);
    对于(i = 0; I< N;我++)
    为(J = 0; J< N; J ++)
    {
    的printf(A(%D,%D)=,I + 1,J + 1);
    scanf函数(%LF,&安培; A [I] [J]);
    }
    //读取{B}矢量
    的printf(\\ n输入[B]载体\\ n个元素);
    对于(i = 0; I< N;我++)
    {
    的printf(B(%D)=,I + 1);
    scanf的(%LF,和b由[i]);
    }    //调用高斯消元函数
    RETVAL =高斯(A,B中,n,&放大器; DET);    //打印结果
    如果(RETVAL == 0)
    {
    的printf(线性方程组的\\ n \\ n \\ T溶液);
    的printf(\\ n \\ t的解决方案是);
    对于(i = 0; I< N;我++)
    的printf(\\ n \\ t X(%D)=%LF,I + 1,B [I]);
    的printf(\\ n \\ t =行列式LF%\\ n,DET);
    }
    其他
    的printf(\\ n \\ t奇异矩阵\\ n);    返回0;
 }/ *解决了方程[A] {X} =使用{B}的系统* /
/ *高斯消元法的部分回转。 * /
/ *参数:* /
/ * N - 方程式数* /
/ *一个[n]的[n]的 - 系数矩阵* /
/ * B [N] - 右侧向量* /
/ * * DET - 行列式[A] * /INT高斯(双A [] [MAXSIZE],重B [],诠释N,双* DET)
{
    双TOL,温度,MULT;
    INT npivot,I,J,L,K,标志;    //初始化
    * DET = 1.0;
    TOL = 1E-30; //初始公差值
    npivot = 0;
        // MULT = 0;    //消除前进
    对于(K = 0; K< N; k ++)
    {
    //搜索枢轴最大系数行向A [k]的[K]主元
    对于(I = K + 1; I< N;我++)
    {
    如果(晶圆厂(A [I] [K])GT;晶圆厂(A [k]的[K]))
    {
    //交换行与主元行最大信号元
    npivot ++;
    为(L = 0; L&下; N,L ++)
    {
    TEMP =一个[我] [1];
    一个由[i] [1] = A [k]的[L];
    一个[k]的[1] =温度;
    }
    温度= B [I]
    B〔Ⅰ〕= B [k]的;
    B〔K] =温度;
    }
    }
    //测试奇点
    如果(晶圆厂(A [k]的[K])LT; TOL)
    {
           //矩阵是singular-终止
    标志= 1;
    返回标志;
    }
        //计算determinant-枢轴元件的产物
    * DET = * DET * A [k]的[K];    //消除的X(Ⅰ)的系数
    对(我= K,I< N;我++)
    {
    MULT =一个由[i] [k]的/一个[k]的[K];
    B〔Ⅰ〕= B [Ⅰ] - B [k]的* MULT; //计算常数
    为(J = K,J< N; J ++)//计算系数
    一个[I] [J] =一[I] [J] - 一个[K] [J] * MULT;
    }
    }
    //调整行列式的符号
    如果(npivot%2 == 1)
      * DET = * DET *(-1.0);    //回代
    B〔N] = B [n]的/一个[n]的[N];
    为(ⅰ= N - 1; I&大于1;我 - )
    {
    为(J = N; J> i + 1的; j--)
    B〔I] = B [I] - 一个由[i] [j]的* B [J]。
    B〔Ⅰ〕= B [I] /一个[I - 1] [I];
    }
    标志= 0;
    返回标志;
}

解决方案应该是:1.058824,1.823529,0.882353与DET为-102.000000

任何有识之士为AP preciated ...


解决方案

  //消除的X(I)的系数
对(我= K,I< N;我++)

如果这也许是

 为(I = K + 1; I< N;我++)

现在是这样的,我相信这将导致你本身分割的支点排,零吧。

Logic error problem with the Gaussian Elimination code...This code was from my Numerical Methods text in 1990's. The code is typed in from the book- not producing correct output...

Sample Run:

         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 . . .

The code as copied from the text...

//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;
}

The solution should be: 1.058824, 1.823529, 0.882353 with det as -102.000000

Any insight is appreciated...

解决方案
//eliminate the coefficients of X(I)
for (i = k; i < n; i++)

Should this maybe be

for (i = k + 1; i < n; i++)

The way it is now, I believe this will cause you to divide the pivot row by itself, zeroing it out.

这篇关于对于高斯消元逻辑错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 23:00