2012-03-18 78 views
-2

用戶將輸入2個方程,然後解決它的迭代(抱歉我的英語)。循環未被執行的問題。當et的值小於g的值時,代碼應該突然出現。任何人都可以幫助我循環我的程序(GAUSS SEIDEL METHOD)嗎?

代碼:

#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; 

    } 

回答

0

你不是應該使用

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

或類似的東西呢?

0

你應該使用更多的空格,以便更容易地看到你在做什麼。嚴重的是,這是主要問題。

終止條件應該是當最大誤差低於一定限度,因爲那時所有的變量都收​​斂「夠了。」事實上,你正在考慮變化率的總和,如果這些變化恰好相反,可能會導致過早終止,否則就會產生不均勻的收斂程度。

否定xe沒有幫助,因爲在一般情況下,它們可能會超調和反轉符號。

+0

et應該是xe和ye的平均值。 – 2012-03-18 06:48:45

+0

@BossJeric我誤解了變量......請使用更多的空格和註釋。這個答案是更新的,現在應該是有見地的。 – Potatoswatter 2012-03-18 06:56:34

+0

好吧,我會嘗試提出一些意見。 – 2012-03-18 07:00:18

相關問題