【问题标题】:Gauss-Seidel method to compute 3 systems of linear equationsGauss-Seidel 方法计算 3 个线性方程组
【发布时间】:2011-12-12 17:07:23
【问题描述】:

我的任务是开发一个程序来计算 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;
}

【问题讨论】:

    标签: c++ linear-algebra


    【解决方案1】:

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

    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
    }
    

    典型的编程实践是to

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

    而不是

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

    【讨论】:

    • 有三个变量,所以我必须为每个变量计算?
    【解决方案2】:

    这看起来像是一个错字?

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多