使用给定矩阵 'a' 和矩阵 'b' 的上三角矩阵的后向替换来计算 vector x

标签 c

根据下面链接中提到的pdf,我需要计算给定矩阵“A”的矩阵“x”和
使用向后替换的上三角矩阵的矩阵“b”。

链接:http://www.mathcs.emory.edu/~haber/math315/chap3.pdf

实际上我只需要使用一维数组。我还为其开发了逻辑并尝试
编译它,但实际上它有一些“表达式”和“初始化”错误 |1 2 3| |x0| |b0| |0 4 5| x|x1| =|b1| |0 0 6| |x2| |b2| 方程:
1) 6*x2 = b2 2) 4*x1 + 5*x2 = b1 3) 1*x0 + 2*x1 + 3*x3 = b0

<小时/>
This is my code:
//here: 's' is size of matrix eg: for 3x3 it is 3
//x[] i need to get the result
//b[] it is to be multiplied with cm to get 
//cm[] is the matrix which will be multiplied with b[] to get x[]

    for(int p=s-1; p>0; p--)
        { 
        if(p==s-1)
            {
            x[p] = b[p]/cm[s*s];                        // computing x2
            }
        else
        {
                for(int j=0; int k=s-p; j<s-i; k>0; j++; k--)
                    {
                        c[p]+ = cm[s*p - j]*x[p+k];
                }
            }

        x[p] = (b[p] - c[p])/cm[s*p-(s-i)];
        }
Errors: 
1) variable 'x' may not be initialized
2) variable 'c' may not be initialized
3) expression for(int j=10; intk=s-p;j<s-i;k>0;j++;k--) has no effect
4) expected a ")" in point 3.
5) expected an expression in line  c[p]+ = cm[s*p - j]*x[p+k];
6) variable 'x' was set but never used

Please help me how to solve these errors?
Also let me know is my logic correct?

最佳答案

我希望仍然需要答案。代码的粗略想法是正确的,但是有一些 undefined variable (例如 i)以及一些与列主矩阵操作相关的错误:行

x[p] = b[p]/cm[s*s];

实际上必须是:

x[p] = b[p]/cm[s*s- 1];

只要我们用 C 语言交谈。无论如何,我只是发布有效的代码:

void backward_substitution (double *A, double *b, double *x, int rows)
{
//here: 'rows' is size of matrix eg: for 3x3 it is 3
//x[] where we put the result of backward substitution
//b[] it is the vector to be used.
//A[] is the square triangular matrix.

register int i,j;

// x(m,1) = b(m)/A(m,m); %% this is for the last row.
x[rows-1] = b[rows-1]/A[(rows-1)*rows + (rows-1)];

// for i = m-1:-1:1     % i starts at 2, end at 4, w/ steps of 1
//    x(i,1) = ( b(i)- A(i,i+1:m)*x(i+1:m))  /  A(i,i);
// end

 for (i=(rows-2); i>=0; i--)
    {
      x[i] = b[i];        
        for (j=i+1; j<rows; j++)
        {
             x[i] -= A[(j)*rows + i]*x[j];
        };             
      x[i] = x[i] / A[(i)*rows + i];
    }; //for (i=1; i<rows; i++)
}; //void forward_substitution(double *A, double b*, int n)

为了您的方便,注释包含 MATLAB 中的实际实现。享受吧。

关于使用给定矩阵 'a' 和矩阵 'b' 的上三角矩阵的后向替换来计算 vector x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13062128/

相关文章:

c - 我怎样才能修复 c 中的这些错误?

c++ - "uFlags &= ~CHN_PANNING"命令有什么作用?

c - 如何在C中删除二叉树中的元素?

python - 在 python 中打印出 c 类型的二进制表示

MySQL:我可以更新当前结果集中的记录还是需要 2 个并行查询/连接

c - win32 从 ListView 中检索所有选定项目的索引

c - 在 C 中,普通数组和使用 malloc 创建的数组有什么区别?

c++ - 快速排序无限循环数值食谱

为 arm cortex-m4 交叉编译 libwebsockets 时出现 cmake 错误

c - 为什么在初始化多维数组时不能完全省略维度?