2014-11-26 2 views
0

그래서 정확하게 읽는 행렬 A을 읽으려고합니다. 벡터 B가 사용자에 의해 입력됩니다. 그런 다음 솔루션 벡터 x를 구하기 위해 Gaussian Elimination (Ax = b)을 수행하려고합니다. x 값은 -1입니다. # IND와 나는 왜 ... 나는 SystemSolution에서 뭔가 잘못되었다고 추측하고 있습니까?txt 파일에서 행렬을 읽어서 Gaussian Elimination (C++)

#include <iostream> 
#include <vector> 
#include <iomanip> 
#include <fstream> 
#include <string> 
#include <sstream> 

using namespace std; 

//this program does gaussian elimination for a matrix Ax=b 

vector<double> SystemSolution(vector<vector<double>> A, vector<double> b) 
{ 

    //Determine and test size of a matrix 
    int n = A.size(); 
    for (int i = 0; i < n; i++) 
     if (n != A[i].size()) 
      throw "Error! Number of rows and columns of matrix must be equal!"; 

    vector<double> x(b.size()); 
    //x is the vector of solutions 


    for (int i = 0; i < n - 1; i++) 
    { 
     for (int j = i + 1; j < n; j++) 
     { 
      //Finding pivot 
      double pivot = A[i][i]; 
      int index = i; 
      for (int k = i + 1; k < n; k++) 
      { 
       if (pivot > abs(A[k][i])) 
       { 
        index = k; 
        pivot = A[k][i]; 
       } 
      } 

      //Row exchange 
      for (int k = 0; k < n; k++) 
      { 
       double tmp = A[i][k]; 
       A[i][k] = A[index][k]; 
       A[index][k] = tmp; 
      } 

      //Elimination 
      double coefficient = -(A[j][i]/A[i][i]); 
      for (int k = i; k < n; k++) 
      { 
       A[j][k] += coefficient*A[i][k]; 
      } 

      b[j] += coefficient*b[i]; 
     } 
    } 

    //Back-substitution 
    x[n - 1] = b[n - 1]/A[n - 1][n - 1]; 
    for (int i = n - 2; i >= 0; i--) 
    { 
     double sum = 0; 
     for (int j = i; j < n; j++) 
     { 
      sum += x[j] * A[i][j]; 
     } 
     x[i] = (b[i] - sum)/A[i][i]; 
    } 

    return x; 
} 



void PrintVector(const vector<double> &b) 
{ 
    for (int i = 0; i < b.size(); i++) 
     cout << setiosflags(ios::showpoint | ios::fixed | ios::right) 
     << setprecision(4) 
     << setw(8) << b[i] << endl; 
} 

void PrintMatrix(const vector<vector<double> > &A) 
{ 
    for (int i = 0; i < A.size(); i++) 
    { 
     for (int j = 0; j < A[i].size(); j++) 
      cout << setiosflags(ios::showpoint | ios::fixed | ios::right) 
      << setprecision(4) 
      << setw(8) << A[i][j]; 
     cout << endl; 
    } 
} 
int main() 
{ 
    int n; 
    cout << "Please enter the number of rows/columns:"; 

    cin >> n; 

    ifstream matrixFile; 

    matrixFile.open("matrix.txt"); 

    if (matrixFile.is_open()){ 

     //matrix A values 
     vector<vector<double>> A(n, vector<double>(n)); 
     vector<double> b(n); 
     string line; 
     int col = 0; 
     int row = 0; 

     while (getline(matrixFile, line)){ 

      istringstream stream(line); 

      int x; 
      col = 0; //reset 

      while (stream >> x){ 
       A[row][col] = x; 
       col++; 
      } 

      row++; 

     } 





     cout << "Please enter vector b:"<<endl; 
     //vector b values 
     for (int i = 0; i < row; i++) 
     { 
      cout << "b[" << i + 1 << "]= "; 
      cin >> b[i];  
     } 



     vector<double> x = SystemSolution(A, b); 
     cout << "- SOLUTIONS -" << endl; 
     cout << "Matrix:" << endl; 
     PrintMatrix(A); 
     cout << "\nVector x:" << endl; 
     PrintVector(x); 

    } 
    else{ 
     cout << "File failed to open!"; 
    } 
    matrixFile.close(); 

    return 0; 
} 
+2

안녕하세요. I/O가 작동하고 입력 또는 정확한 출력을 문서화하지 않는다고 말합니다. 나는 두 가지 벡터를 최소한으로 하드 코딩하여 그 프로그램의 실제 출력과 기대했던 것, 그리고 차이를 진단 한 거리를 나열하는 해결되지 않은 문제에 대한 일러스트레이션을 만드는 것이 좋습니다. 즉, "x 값은 -1입니다. # IND"는 이해할 수 없습니다. –

+1

이것이 문제가되지 않는다면 왜'file-io'로 태그를 붙였습니까? –

+1

@Xar 제거의 디버그 프린트를 추가하고 (각 단계에 주석이 달려 있음) 문제가있는 곳을 시각적으로 확인하십시오. 특정 문제 업데이트 질문을 찾을 때 ... – Spektre

답변

0

코드에서 0으로 일부 부서를가있을 수 있습니다 : 여기

double coefficient = -(A[j][i]/A[i][i]); 
/* .. */ 
x[n - 1] = b[n - 1]/A[n - 1][n - 1]; 
/* .. */ 
x[i] = (b[i] - sum)/A[i][i]; 

체크 아웃 가우스 - 제거 : Square Matrix Inversion in C

검토 및 디버그 당신.

관련 문제