Gauss-Seidel方法计算3个线性方程组

时间:2011-12-12 17:07:24

标签: c++ linear-algebra

我的目标是开发一个程序来计算3个线性方程组:程序必须允许用户输入系数和常数,迭代次数和可接受误差的级别。我似乎无法将迭代次数和错误级别都包括为停止循环的参数并显示变量的最终值。这是我到目前为止所做的:

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    cout<<"Welcome. This is  Problem 1. "<<endl;
    cout<<"computing systems of  three linear equations through gauss-seidel method"<<endl;

    float coefEqxn1[3];
    for (int x=0; x<3;)
    {
        for ( int eq1=1; eq1<=3; eq1++)
        {
            cout<<"Please enter Coefficient " <<eq1<< " of equation 1 : ";
            cin>>coefEqxn1[x];
            x++;
        }
    }

    float coefEqxn2[3];
    for (int x=0; x<3;)
    {
        for ( int eq2=1; eq2<=3; eq2++)
        {
            cout<<"Please enter Coefficient " <<eq2<<" of equation 2 :" ;
            cin>>coefEqxn2[x];
            x++;
        }
    }

    float coefEqxn3[3];
    for (int x=0; x<3;)
    {
        for ( int eq3=1; eq3<=3; eq3++)
        {
            cout<<"Please enter Coefficient "<<eq3<<" of equation 3 :";
            cin>>coefEqxn3[x];
            x++;
        }
    }

    float constants[3];
    for (int y=0; y<3;)
    {
        for (int con=1; con<=3; con++)
        {
            cout<<"Please enter the contant of equation "<<con<<" : ";
            cin>>constants[y];
            y++;
        }
    }

    cout<<"Calculating through Cramer's Rule..."<<endl;

    int iteration=0;
    cout<<"enter # iteration"<<endl;
    cin>>iteration;

    int stopC=0;
    cout<<"enter level of error"<<endl;
    cin>>stopC;

    float matrixArray[3][4];
    {
        for ( int y=0; y<3;)
        {
            for (int x=0; x<=3;x++)
                matrixArray[0][y]=coefEqxn1[y];
            y++;
        }

        matrixArray[0][3]=constants[0];

        for ( int y=0; y<3;)
        {
            for (int x=0; x<=3;x++)
                matrixArray[1][y]=coefEqxn2[y];
            y++;
        }

        matrixArray[1][3]=constants[1];
        for ( int y=0; y<3;)
        {
            for (int x=0; x<=3;x++)
                matrixArray[2][y]=coefEqxn3[y];
            y++;
        }

        matrixArray[2][3]=constants[2];
    }

    for(int a=0; a<3; a++)
    {
        for(int b=0; b<=3; b++)
            cout<<"matrixArray["<<a<<"]["<<b<<"]: "<<matrixArray[a][b]<<endl;   
    }

    float valueOfX[100], valueOfY[100], valueOfZ[100];

    for( int i=1; i<iteration; )
    {
        valueOfX[0]=0, valueOfY[0]=0, valueOfZ[0]=0;

        valueOfX[i]=(matrixArray[0][3]-(matrixArray[0][2]*valueOfZ[i-1]+matrixArray[0][1]*valueOfY[i-1]))/matrixArray[0][0];
        valueOfY[i]=(matrixArray[1][3]-(matrixArray[1][2]*valueOfZ[i-1]+matrixArray[1][0]*valueOfX[i]))/matrixArray[1][1];
        valueOfZ[i]=(matrixArray[2][3]-(matrixArray[2][1]*valueOfY[i]+matrixArray[2][0]*valueOfX[i]))/matrixArray[2][2];

        float reX=0, reY=0, reZ=0;

        reX=((valueOfX[i+1]-valueOfX[i])/valueOfX[i+1])*100;
        reY=((valueOfY[i+1]-valueOfY[i])/valueOfY[i+1])*100;
        reX=((valueOfZ[i+1]-valueOfZ[i])/valueOfZ[i+1])*100;

        if (reX<=inputErrorLevel)
            break;

        if (reY<=inputErrorLevel)
            break;

        if (reZ<=inputErrorLevel)
            break;

        cout<<"reX = "<<reX<<endl;
        cout<<"reY = "<<reY<<endl;
        cout<<"reY = "<<reX<<endl;

        i++;
    }

    cout<<"x = "<<valueOfX[iteration-1]<<endl;
    cout<<"y = "<<valueOfY[iteration-1]<<endl;
    cout<<"z = "<<valueOfZ[iteration-1]<<endl;
}

2 个答案:

答案 0 :(得分:0)

根据你的需要你应该有类似的东西

double inputErrorLevel; //Read from user input

// The for loop guarantees that the loop will run at max iteration times.
// Noticed I changed it to start from zero otherwise it will only run
// iteration -1 times 
for( int i=0; i<iteration; ++i )
{

  //Do heavy computation stuff.

  double currentErrorLevel = ComputeErrorAtCurrentIteration();
  if ( currentErrorLevel < inputErrorLevel )
       break; // break will ensure more iterations are not done
              // if error level is acceptable
}

典型的编程实践是

for ( int i = 0 ; i < 5; ++i )
{
}

而不是

for ( int i = 0 ; i < 5; )
{
 ++i;
}

答案 1 :(得分:0)

这看起来像是一个错字?

reX=((valueOfZ[i+1]-valueOfZ[i])/valueOfZ[i+1])*100;

不应该是:

reZ=((valueOfZ[i+1]-valueOfZ[i])/valueOfZ[i+1])*100;

同样在这里:

cout<<"reY = "<<reX<<endl;

但是要回答你的问题,这应该是使用i来索引结果而不是迭代。迭代变量将始终保持不变,因此无论错误如何,它都将始终提供该结果。

像:

cout<<"x = "<<valueOfX[i-1]<<endl;
cout<<"y = "<<valueOfY[i-1]<<endl;
cout<<"z = "<<valueOfZ[i-1]<<endl;