在 C++ 中求解 Ax = b, A = 下三角矩阵

5 c++ algorithm math linear-algebra equation-solving

我正在尝试做这个函数来解决线性系统,A*x = b,其中 A = 下三角矩阵,线性无关矩阵,并且只有一个解。但结果总是显示 0 0 0 0 ... 我已经打印了总和,s,它也总是显示 0 ...

 #include  <iostream>
 using namespace std;

void solve(int n, float a[][MAX], float b[], float x[]){
 int i,j;
 float s;

 for(i = 0; i < n; i++){
        s = 0;
        for(j = 0; j < n; j++){

            s = s + a[i][j]*x[j];
            cout<<s<<endl;
        }
    x[i] = (b[i] - s)/a[i][i];
 }
}
Run Code Online (Sandbox Code Playgroud)

4pi*_*ie0 3

void solve(int n, float a[][MAX], float b[], float x[]){
  int i,j;
  float s;

  for(i = 0; i < n; i++) {
        s = 0;
        for(j = 0; j < i; j++) {
                       ^
            s = s + a[ i][ j] * x[ j];
        }
        x[ i] = ( b[ i] - s) / a[ i][ i];
   }
}
Run Code Online (Sandbox Code Playgroud)

返回替换.pdf

编译的例子