2015-09-06 5 views
5

I war Gauß-Jordan-Eliminierung in C++ unter Verwendung eines Systems von linearen Gleichungen zu lösen. Code funktioniert gut. Frage mich, warum Linien 1,2,3 in void gauss() nicht durch die Linie 4 (immer falsch ausgegeben, nachdem tun so) ersetzt werden?Gauss-Jordan Elimination in C++

#include <iostream> 
using namespace std; 
class Gauss 
{ 
    float a[50][50]; 
    int n; 
public: 
    void accept() 
    { 
     cout<<"Enter no. of variables: "; 
     cin>>n; 
     for(int i=0;i<n;i++) 
     { 
      for(int j=0;j<n+1;j++) 
      { 
       if(j==n) 
        cout<<"Constant no."<<i+1<<" = "; 
       else 
        cout<<"a["<<i+1<<"]["<<j+1<<"] = "; 
       cin>>a[i][j]; 
      } 
     } 
    } 
    void display() 
    { 
     for(int i=0;i<n;i++) 
     { 
      cout<<"\n"; 
      for(int j=0;j<n+1;j++) 
      { 
       if(j==n) 
        cout<<" "; 
       cout<<a[i][j]<<"\t"; 
      } 
     } 
    } 

    void gauss()//converting augmented matrix to row echelon form 
    { 
     float temp;//Line 1 
     for(int i=0;i<n;i++) 
     { 
      for(int j=i+1;j<n;j++) 
      { 
       temp=a[j][i]/a[i][i];//Line 2 
       for(int k=i;k<n+1;k++) 
       { 
         a[j][k]-=temp*a[i][k];//Line 3 
        //a[j][k]-=a[j][i]*a[i][k]/a[i][i];//Line 4 
       } 
      } 
     } 
    } 

    void EnterJordan()//converting to reduced row echelon form 
    { 
     float temp; 
     for(int i=n-1;i>=0;i--) 
     { 

      for(int j=i-1;j>=0;j--) 
      { 
       temp=a[j][i]/a[i][i]; 
       for(int k=n;k>=i;k--) 
       { 
        a[j][k]-=temp*a[i][k]; 
       } 
      } 
     } 

     float x[n]; 
     for(int i=0;i<n;i++)//making leading coefficients zero 
      x[i]=0; 
     for(int i=0;i<n;i++) 
     { 
      for(int j=0;j<n+1;j++) 
      { 
       if(x[i]==0&&j!=n) 
        x[i]=a[i][j]; 
       if(x[i]!=0) 
        a[i][j]/=x[i]; 
      } 
     } 
    } 
    void credits() 
    { 
     for(int i=0;i<n;i++) 
     { 
      cout<<"\nx"<<i+1<<" = "<<a[i][n]<<endl; 
     } 
    } 

}; 

int main() 
{ 
    Gauss obj; 
    obj.accept(); 
    cout<<"\n\nAugmented matrix: \n\n\n"; 
    obj.display(); 
    obj.gauss(); 
    cout<<"\n\nRow Echelon form: \n\n\n"; 
    obj.display(); 
    obj.EnterJordan(); 
    cout<<"\n\nReduced row echelon form:\n\n\n"; 
    obj.display(); 
    cout<<"\n\nSolution: \n\n\n"; 
    obj.credits(); 
    return 0; 
} 

Hinweis: Mein Code braucht nicht berücksichtigt das Problem der Teilung, wenn die Dreh Null ist (ich bin das Diagonalelement als Drehpunkt jedes Mal wählen). Für das spezielle Beispiel versuchte ich jedoch, solch ein Fall wurde nicht angetroffen.

erweiterte Matrix ist:

2 1 -1 8 
-3 -1 2 -11  
-2 1 2 -3 

der Ausgangsmatrix ist:

1 0 0 2 
0 1 0 3 
0 0 1 -1 

, und die Lösung ist:

x1 = 2 

x2 = 3 

x3 = -1 

Verwendung Linie 4, die Ausgangsmatrix ist:

1 0 0 -0.75 
0 1 -0 8 
0 0 1 -1.5 

und die Lösung ist:

x1 = -0.75 

x2 = 8 

x3 = -1.5 
+1

Wo ist die Deklaration von 'a'? Es ist ziemlich wichtig, dass dieser Code funktioniert. –

+0

Oops sorry! Vergessen, es zu erwähnen. Es ist ein Float-Array, das ich in der Klasse (nicht gezeigt) als float a [50] [50] deklariert habe; – ByteMan2021

+1

Sie erhalten eine bessere und schnellere Antwort, wenn Sie den gesamten relevanten Code angeben, damit das Problem reproduziert werden kann. –

Antwort

2

Ihre Linie 4 liest aus a[j][i] viele Male, und das erste Mal durch die innere Schleife, wenn k == i ändert a[j][i] auf 0.0f, so dass die nächste n-i brechen Iterationen.

Neuordnen liest einer Variablen mit einem Schreib an der gleichen Stelle ist nicht sicher.

+0

Ahh, das erklärt, warum der Rest der Elemente in der Reihen-Staffelform unverändert blieb. Vielen Dank! – ByteMan2021