简体   繁体   中英

Lower triangular matrix and upper triangular matrix give me wrong answer

I'm working in LU Decomposition in C.My code is very simple Algorithm can be parallelized using two loops one for updating lower triangular matrix and one for updating upper triangular matrix ,but it seems I miss understand something :(

      for (i=0 ; i<N ; i++){
 // A[i][i]=1;
  for (j=i+1 ;j<N ;j++){
      A[j][i] = A[j][i]/A[i][i]; //*Update L*//
  }
  for (j=i+1;j<N;j++){
      for(k=i+1 ;k<N;k++){

          A[j][k] = A[j][k] - A[i][k] * A[j][i];//*Update U*//
       }
    }
 }

  printf("\n Matrix after U transformation: \n");
  print_matrix(); 

for (i=0 ; i<N ; i++){
   A[i][i]=1;
  for (j=i+1 ;j<N ;j++){
      A[j][i] = A[j][i]/A[i][i]; //*Update L*//
  }
  for (j=i+1;j<N;j++){
      for(k=i+1 ;k<N;k++){

          A[j][k] = A[j][k] - A[i][k] * A[j][i];//*Update U*//
         }
      }
     }

     printf("\n Matrix after L transformation: \n");
     print_matrix(); 

This is what I should to get ?! what I'm doing wrong

L =

1.0000         0         0         0         0
0.2000    1.0000         0         0         0
0.2000    0.1667    1.0000         0         0
0.2000    0.1667    0.1429    1.0000         0
0.2000    0.1667    0.1429    0.1250    1.0000


U =

 50.0000   10.0000   10.0000   10.0000   10.0000
     0   48.0000    8.0000    8.0000    8.0000
     0         0   46.6667    6.6667    6.6667
     0         0         0   45.7143    5.7143
     0         0         0         0   45.0000

but what I got is ,,,,L not should be like this ?!

Source Matrix :
50      10      10      10      10
10      50      10      10      10
10      10      50      10      10
10      10      10      50      10
10      10      10      10      50

Matrix after U transformation: 
 50      10      10      10      10
  0      48       8       8       8
  0       0      47       7       7
  0       0       0      46       6
  0       0       0       0      45

 Matrix after L transformation: 
   1      10      10      10      10
   0       1       6       6       6
   0      -2       1      16      16
   0      -2       9       1    -129
   0      -2       9    -134       1

Thanks

Your U matrix is correct, beside that these are integers and not floats. The diagonal of your L matrix also is correct (you're setting it's values), but not the rest. After checking the code against the answer of " LU Decomposition from Numerical Recipes not working; what am I doing wrong? ", which is (changed it a bit, added some braces):

for (i = 0; i < N; i++) {
    // compute U
    for (j = i; j < N; j++) {
        for (k = 0; k < i-2; k++) {
            A[i,j] -= A[i,k] * A[k,j];
        }
    }

    // compute L
    for (j = i+1; j < N; j++) {
        for (k = 0; k < i-2; k++) {
            A[j,i] -= A[j,k] * A[k,i];
        }
    }
}

I noticed that you are missing a loop in your code, which should be the problem. Also take a look at the mentioned SO question which provides some more useful hints.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM