Help with LU Decomposition in C

  • Thread starter Thread starter iasc
  • Start date Start date
  • Tags Tags
    Decomposition
Click For Summary
The discussion centers around issues with a provided LU decomposition code in C that is not functioning correctly. The user seeks assistance in troubleshooting the code, which includes setting up matrices L and U and performing calculations to decompose matrix A. Specific problems mentioned involve the initialization of matrices and the calculation loops for L and U. Participants are encouraged to format their code for better readability. The thread highlights the importance of clear code presentation for effective problem-solving in programming discussions.
iasc
Messages
17
Reaction score
0
Hey, I have this code for lu decomposition but It doesn't quite work. If anyone could help me with the problem I'd be very appreciative.

Code:
for(j=0; j<N; j++)
                for(i=j+1; i<N; i++)
                  U[i][j]=0;

        for(j=0; j<N; j++)
                for(i=j+1; i<N; i++)
                        L[j][i]=0;

        for(i=0; i<N; i++)
          U[i][i]=1;
          for(i=0; i<N; i++)
          L[i][0]=A[i][0];

        for(j=1; j<N; j++)
                U[0][j]=A[0][j]/A[0][0];

        for(i=0; i<N; i++)
        {
                for(j=0; j<=i; j++)
                {
                        sum=0;
                        for(k=0; k<j; k++)
                                sum+=(L[i][k]*U[k][j]);
                        L[i][j]=A[i][j]-sum;
                }
                for(j=N-1; j>=i; j--)
                {
                      if(j!=0)
                        {
                        sum=0;
                        for(k=0; k<i; k++)
                                sum+=(L[i][k]*U[k][j]);
                        if(L[i][i]!=0)
                                U[i][j]=(A[i][j]-sum)/L[i][i];
                        }
                }
        }
 
Last edited:
Physics news on Phys.org
While there is still time to edit your post, put code tags around it so that it is readable.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
8K