Matlab debugging for LU decomposition

Click For Summary
SUMMARY

This discussion focuses on debugging MATLAB code for LU decomposition with partial pivoting and substitution. The provided functions include LUdecomp.m, decompose.m, pivot.m, and substitute.m. Key issues identified include error handling when the pivot element is below a specified tolerance (tol) and the need for proper initialization of arrays o and s. The conversation emphasizes the importance of isolating the problematic function for effective debugging.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of LU decomposition and its mathematical principles
  • Knowledge of error handling in MATLAB
  • Experience with matrix operations in MATLAB
NEXT STEPS
  • Review MATLAB's debugging tools to identify issues in the provided functions
  • Study the implementation of LU decomposition in MATLAB documentation
  • Learn about MATLAB's error handling mechanisms to improve robustness
  • Explore optimization techniques for matrix operations in MATLAB
USEFUL FOR

Mathematics students, MATLAB programmers, and software developers working on numerical methods or linear algebra applications will benefit from this discussion.

joehardy
Messages
1
Reaction score
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
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
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 10 ·
Replies
10
Views
1K