2012-03-18 8 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の平均でなければなりません。 –

+0

@BossJeric私は変数を誤解しています...もっと空白とコメントを使用してください。この回答は更新されました。今は洞察力が必要です。 – Potatoswatter

+0

ええ、私はいくつかのコメントを入れてみます。 –

関連する問題