- #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: