Matlab debugging for LU decomposition

In summary, these are four MATLAB files for performing LU decomposition with partial pivoting and substitution. The first file, LUdecomp.m, takes in a matrix A, vector b, and other parameters and uses the other three files to decompose A into a lower triangular matrix L and an upper triangular matrix U, and then solves the system of equations using substitution. The decompose.m file calculates the pivot element and performs Gaussian elimination, while the pivot.m file identifies the pivot row. Finally, the substitute.m file solves the system of equations using backward substitution.
  • #1
joehardy
1
0
these are my mfiles for Lu decomposition with partial pivoting and substitution

1.LUdecomp.m
function [x]=LUdecomp(a,b,n,tol,er,x)
for i=1:n
o(i)=0;
s(i)=0;
end
er=0;
decompose(a,n,tol,o,s,er)
if er~=-1
substitute(a,o,n,b,x);
end
end

2.decompose.m
function [a]=decompose(a,n,tol,o,s,er)
for i=1:n
o(i)=i;
s(i)=abs(a(i,1));
for j=2:n
if abs(a(i,j))>s(i)
s(i)=abs(a(i,j));
end
end
end
for k=1:n-1
pivot(a,o,s,n,k)
if abs(a(o(k),k)/s(o(k)))<tol
er=-1;
abs(a(o(k),k)/s(o(k)));
break
end
for i=k+1:n
factor=a(o(i),k)/a(o(k),k);
a(o(i),k)=factor;
for j=k+1:n
a(o(i),j)=a(o(i),j)-factor*a(o(k),j);
end
end
end
if abs(a(o(k),k)/s(o(k)))<tol
er=-1;
abs(a(o(k),k)/s(o(k)));
end
end

3.pivot.m
function [o]=pivot(a,o,s,n,k)
p=k;
big=abs(a(o(k),k)/s(o(k)));
for i=k+1:n
dummy=abs(a(o(i),k)/s(o(i)));
if dummy>big
big=dummy;
p=i;
end
end
dummy=o(p);
o(p)=o(k);
o(k)=dummy;
end

4.substitute.m

function [x]=substitute(a,o,n,b,x)
for i=2:n
sum=b(o(i));
for j=1:i-1
sum=sum-a(o(i),j)*b(o(j));
end
b(o(i))=sum;
end
x(n)=b(o(n))/a(o(n),n);
for i=n-1:-1:1
sum=0;
for j=i+1:n
sum=sum+a(o(i),j)*x(j);
end
x(i)=(b(o(i))-sum)/a(o(i),i);
end
end
thank you
 
Physics news on Phys.org
  • #2
What's your question? From the thread title, you seem to be looking for help in debugging this code. Can you narrow down which function is not working correctly?

Also, it's a good idea to surround your code with [ code] and [ /code] tags (omit the leading space.
joehardy said:
these are my mfiles for Lu decomposition with partial pivoting and substitution

1.LUdecomp.m
Code:
function [x]=LUdecomp(a,b,n,tol,er,x)
for i=1:n
    o(i)=0;
    s(i)=0;
end
er=0;
decompose(a,n,tol,o,s,er)
if er~=-1
    substitute(a,o,n,b,x);
end
end
2.decompose.m
Code:
function [a]=decompose(a,n,tol,o,s,er)
    for i=1:n
        o(i)=i;
        s(i)=abs(a(i,1));
        for j=2:n
            if abs(a(i,j))>s(i)
            s(i)=abs(a(i,j)); 
            end
        end
    end
for k=1:n-1
    pivot(a,o,s,n,k)
    if abs(a(o(k),k)/s(o(k)))<tol
        er=-1;
        abs(a(o(k),k)/s(o(k)));
    break
    end
    for i=k+1:n
        factor=a(o(i),k)/a(o(k),k);
        a(o(i),k)=factor;
        for j=k+1:n
            a(o(i),j)=a(o(i),j)-factor*a(o(k),j);
        end
    end
end
if abs(a(o(k),k)/s(o(k)))<tol
        er=-1;
        abs(a(o(k),k)/s(o(k)));
end
end

3.pivot.m
Code:
function [o]=pivot(a,o,s,n,k)
p=k;
big=abs(a(o(k),k)/s(o(k)));
for i=k+1:n
    dummy=abs(a(o(i),k)/s(o(i)));
    if dummy>big
        big=dummy;
        p=i;
    end
end
dummy=o(p);
o(p)=o(k);
o(k)=dummy;
end
4.substitute.m
Code:
function [x]=substitute(a,o,n,b,x)
for i=2:n
    sum=b(o(i));
    for j=1:i-1
        sum=sum-a(o(i),j)*b(o(j));
    end
    b(o(i))=sum;
end
x(n)=b(o(n))/a(o(n),n);
for i=n-1:-1:1
    sum=0;
    for j=i+1:n
        sum=sum+a(o(i),j)*x(j);
    end
    x(i)=(b(o(i))-sum)/a(o(i),i);
end
end
thank you
 

1. What is LU decomposition in Matlab?

LU decomposition is a method used in Matlab to decompose a square matrix into a lower triangular matrix (L) and an upper triangular matrix (U). This decomposition allows for easier manipulation and solving of linear equations.

2. How do I debug my code for LU decomposition in Matlab?

To debug your code for LU decomposition in Matlab, you can use the debugging tools provided by the software such as breakpoints, stepping through code, and viewing variables at each step. You can also use the "dbstop if error" command to stop the code at a specific line if an error occurs.

3. What are common errors encountered when using LU decomposition in Matlab?

Some common errors that may occur when using LU decomposition in Matlab include: division by zero, matrix dimensions not matching, and singular matrices. These errors can often be resolved by double checking the input matrices and making sure they meet the requirements for LU decomposition.

4. Can I use LU decomposition for non-square matrices in Matlab?

No, LU decomposition can only be used for square matrices in Matlab. If you need to decompose a non-square matrix, you can use other methods such as QR decomposition.

5. How can I improve the efficiency of LU decomposition in Matlab?

To improve the efficiency of LU decomposition in Matlab, you can use built-in functions such as "lu()" instead of writing your own code. You can also optimize your code by preallocating memory for large matrices and avoiding unnecessary loops.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
943
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
810
  • Engineering and Comp Sci Homework Help
Replies
1
Views
958
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
12
Views
963
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
6K
Back
Top