Help with LU Decomposition in C

In summary, a code for lu decomposition is provided but it is not functioning properly. The code includes several for loops to set values for matrix U and L, and to calculate the sum and values for U and L based on the given matrix A. Further assistance is requested to resolve the issue.
  • #1
iasc
17
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
  • #2
While there is still time to edit your post, put code tags around it so that it is readable.
 

Related to Help with LU Decomposition in C

1. What is LU Decomposition in C?

LU Decomposition is a method used to solve systems of linear equations by breaking down a matrix into two components: a lower triangular matrix (L) and an upper triangular matrix (U). This method is commonly used in scientific computing and is particularly useful for solving large systems of equations.

2. How is LU Decomposition implemented in C?

In C, LU Decomposition can be implemented using various algorithms such as Gaussian Elimination or Crout's Algorithm. These algorithms involve manipulating the matrix using basic arithmetic operations to obtain the desired L and U matrices.

3. What are the benefits of using LU Decomposition in C?

One of the main benefits of using LU Decomposition in C is that it allows for the efficient solving of large systems of linear equations. It also reduces the amount of computation needed compared to other methods, making it a popular choice for scientific applications.

4. Are there any drawbacks to using LU Decomposition in C?

One potential drawback of using LU Decomposition in C is that it requires a significant amount of memory, as it involves storing two separate matrices. Additionally, the algorithm may not work for all types of matrices, such as those with zero or extremely small diagonal elements.

5. Can LU Decomposition be used for other types of matrices besides square matrices?

Yes, LU Decomposition can also be used for rectangular matrices. In this case, the resulting L and U matrices will have different dimensions, but the overall process remains the same. However, rectangular matrices may also present some challenges in terms of memory and computational efficiency.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
839
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
976
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
1K
Back
Top