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

标签 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];
 }
}

最佳答案

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];
   }
}

BackSubstitution.pdf

compiled example

关于c++ - 在 C++ 中求解 Ax = b, A = 下三角矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22237322/

相关文章:

algorithm - 做一个算法,但我的智商太低了

c++ - 为什么 LeetCode 给出错误 : AddressSanitizer: heap-buffer-overflow?

c++ - KMP算法的时间复杂度

python - 快速替换字符串中的字符并检查子字符串是否是回文

ruby - 选择随机配对、安排比赛的算法

objective-c - 正态分布最佳方法

c++ - 如何迭代枚举?

c++ - 如何用它的内容替换字符串变量?

c++ - 动态二维数组的运算符重载(加法)

php - php中数组的数学运算