Error in lu-decomposition function?

조회 수: 5 (최근 30일)
Benjamin Lender
Benjamin Lender 2015년 12월 14일
댓글: John D'Errico 2015년 12월 17일
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?

답변 (1개)

John D'Errico
John D'Errico 2015년 12월 14일
편집: John D'Errico 2015년 12월 14일
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
Christine Tobler
Christine Tobler 2015년 12월 17일
편집: Christine Tobler 2015년 12월 17일
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 2015년 12월 17일
Yes. the pivoted LU will be more accurate, although still not perfect. Nothing can achieve that, unlesss you use exact arithmetic.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by