Help with LU Decomposition in C

  • Thread starter Thread starter iasc
  • Start date Start date
  • Tags Tags
    Decomposition
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 2K views
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