0


以下のリンクで言及されているpdfによると、後方置換を使用して、上三角行列の行列'A'と行列'b'が与えられた場合に行列'x'を計算する必要があります。

リンク: http: //www.mathcs.emory.edu/~haber/math315/chap3.pdf

実際、私は1次元配列のみを使用する必要があります。また、ロジックを開発して
コンパイルしようとしましたが、実際には「式」と「初期化」のエラーがほとんどありません| 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?
4

2 に答える 2

0

変数を宣言したかどうかはわかりません。宣言していない場合は、宣言してください。for以下に記述したように、2番目のループを作成する必要があります...

for(int j=0,int k=s-p; j<s-i, k>0; j++, k--)

複数の初期化、条件、および増分はコンマで区切る必要があるためです。

于 2012-10-25T05:42:20.283 に答える
0

答えがまだ必要だといいのですが。コードの大まかな考え方は正しいですが、いくつかの未定義の変数(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での実際の実装が含まれています。楽しみ。

于 2013-01-25T05:57:55.350 に答える