任何人都可以帮助我循环我的程序(GAUSS SEIDEL METHOD)吗?

时间:2012-03-18 06:30:50

标签: c++ visual-c++ iteration

用户将输入2个等式然后求解其迭代(对不起我的英语)。循环未执行的问题。当et的值小于g的值时,代码应该突破。

代码:

#include <iostream>
#include<stdlib.h>
using namespace std;

 long double g=0.0010;
 int main()
 {

    long double xe,ye,et,k,x,y,x1,x2,y1,y2,c1,c2,a,b;
    //for the input
    cout<<"EQUATION 1:\n";
    cout<<"Input your desired numerical coefficient for x:"<<endl;
    cin>>x1;
    cout<<"Input your desired numerical coefficient for y:"<<endl;
    cin>>y1;
    cout<< "Input your constant's value:"<<endl;
    cin>>c1;
    system("CLS");
    cout<<"EQUATION 2:\n";
    cout<<"Input your desired numerical coefficient for x:"<<endl;
    cin>>x2;
    cout<<"Input your desired numerical coefficient for y:"<<endl;
    cin>>y2;
    cout<< "Input your constant's value:"<<endl;
    cin>>c2;
    system("CLS");
    //to show the equation made
    cout<<"Your EQUATION 1 is:\n"<<x1<<"x + <"<<y1<<"y)"<<" = "<<c1<<endl<<endl;
    cout<<"Your EQUATION 2 is:\n"<<x2<<"x + ("<<y2<<"y)"<<" = "<<c2<<endl<<endl;

    //first value of x and y
    x=c1/x1;
    y=(c2)/y2;
    //show the values
    cout<<"\nx="<<x<<endl;
    cout<<"y="<<y<<endl;
    //this is where the iteration starts
    for(k=1;g>et;k++)
{

    a=(c1+y)/x1;
    b=(c2-x)/y2;
    xe=((a-y)/a)*-1;
    ye=((b-x)/b);
    et=((xe+ye)/2);
    cout<<"k="<<k;
    cout<<"\nx="<<a<<endl;
    cout<<"y="<<b<<endl;
    cout<<"\nxe="<<xe;
    cout<<"\nye="<<ye;
    cout<<"\net="<<et<<endl;
    }


    return 0;

    }

2 个答案:

答案 0 :(得分:0)

你不应该使用

while (std::abs(et) > g)

或类似的东西?

答案 1 :(得分:0)

您应该使用更多的空格,以便更容易看到您正在做的事情。说真的,这是主要问题。

终止条件应该是最大错误低于某个限制时,因为所有变量都已“收敛”。事实上,你正在考虑变化率的总和,如果它们碰巧有相反的符号,可能会导致提前终止,否则会出现不均匀的收敛程度。

否定xe无济于事,因为在一般情况下,它们可以超调和反转符号。