Error in lu-decomposition function?

6 views (last 30 days)
Benjamin Lender
Benjamin Lender on 14 Dec 2015
Commented: John D'Errico on 17 Dec 2015
I need to do a lu-decomposition, receiving a lower triangular matrix with unit diagonal. The MATLAB-Function [L,R,P] = lu(A) should do exactly that.
A=[ 28 -35 21
-12 19 -15
16 -30 48];
[L,R,P]=lu(A);
Testing the result:
P*A==L*R
ans =
1 1 1
1 1 1
1 1 0
What am I overlooking, why do the results not match?

Answers (1)

John D'Errico
John D'Errico on 14 Dec 2015
Edited: John D'Errico on 14 Dec 2015
No. This is NOT an error in the lu function, but an error in how you think about floating point arithmetic.
Instead of testing for EXACT equality, why not subtract the two? What is the difference?
Do you want to make a bet that the differences are tiny numbers? In fact, it looks like only one of the elements was not exactly the same. I'll save you some time.
P*A-L*R
ans =
0 0 0
0 0 0
0 0 -1.7764e-15
So welcome to the world of floating point arithmetic, where you should virtually NEVER test for exact equality.
  13 Comments
Christine Tobler
Christine Tobler on 17 Dec 2015
Edited: Christine Tobler on 17 Dec 2015
The problem with this brute-force lu decomposition is that it doesn't do permutation, which can lead to completely wrong results. For example, try this matrix:
A = [0 0 1; 1 0 0; 0 1 0];
The brute-force LU will return matrices L and U with Inf and NaN values, while the lu function's output is still correct. In less extreme examples, not using permutation will simply lead to high levels of numerical error.
For the plots you show, I assume from your answer that the first is using your brute-force algorithm and the second the output from lu? In that case, I would assume there is something wrong with the way you are using L, U and P to solve the linear system. Can you post your formula for this?
If you only need to solve one linear system, you can also just use
x = A \ b;
which will solve the linear system A*x = b directly without any worries about the decomposition used.
John D'Errico
John D'Errico on 17 Dec 2015
Yes. the pivoted LU will be more accurate, although still not perfect. Nothing can achieve that, unlesss you use exact arithmetic.

Sign in to comment.

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!