Error in lu-decomposition function?
Ältere Kommentare anzeigen
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?
Antworten (1)
John D'Errico
am 14 Dez. 2015
Bearbeitet: John D'Errico
am 14 Dez. 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 Kommentare
Benjamin Lender
am 14 Dez. 2015
Bearbeitet: Benjamin Lender
am 14 Dez. 2015
John D'Errico
am 14 Dez. 2015
Bearbeitet: John D'Errico
am 14 Dez. 2015
I'm sorry, but if you think that your brute force LU is doing much better, unless it is written in higher precision than double precision, it will not do better in general.
Even then you will ALWAYS have floating point errors when working with floating point arithmetic. It is just the size of those errors. You need to learn about floating point arithmetic if you are working with it.
Note that even though you started with an integer matrix, the LU factors are NOT integers. And almost all fractions are not representable exactly in terms of a floating point number.
For example, consider the L factor as generated.
L
L =
1 0 0
0.57143 1 0
-0.42857 -0.4 1
Those numbers are not exact. That 0.57143 is only an approximation to the fraction 4/7. In fact, the actual number as stored as a double is:
0.5714285714285713968507707249955274164676666259765625000
See that it is not truly 4/7, but only an approximation.
Benjamin Lender
am 14 Dez. 2015
Bearbeitet: Benjamin Lender
am 14 Dez. 2015
John D'Errico
am 14 Dez. 2015
As far as the errors growing with size, again, this is completely expected. The errors involved in ANY linear algebra solve like an LU factor (not just that in MATLAB) will grow with the condition number of the matrix. And generally the condition number of some random matrix will tend to grow with the size of that matrix.
John D'Errico
am 14 Dez. 2015
Bearbeitet: John D'Errico
am 14 Dez. 2015
It IS just floating point error. The error that you found when you used your code was still non-zero, just arbitrarily different. I'm not sure what you mean by that 80% number. And of course, I have no way to know what your own code does. For example, perhaps you used higher precision to accumulate things. This takes more time of course.
You might also want to read section 5.1 of the MATLAB FAQ.
Walter Roberson
am 15 Dez. 2015
Your "brute-force" decomposition is going to have the same problem unless you convert all of your entries from floating point into rational values and use a symbolic solver that calculates in rational form.
For example the exact L matrix is [[1, 0, 0]; [4/7, 1, 0]; [-3/7, -2/5, 1]] and you know that there is no finite binary floating point representation for 1/7 so of course errors are going to accumulate.
Benjamin Lender
am 15 Dez. 2015
Bearbeitet: Benjamin Lender
am 15 Dez. 2015
Benjamin Lender
am 15 Dez. 2015
Benjamin Lender
am 15 Dez. 2015
Benjamin Lender
am 15 Dez. 2015
Benjamin Lender
am 15 Dez. 2015
Bearbeitet: Benjamin Lender
am 15 Dez. 2015
Christine Tobler
am 17 Dez. 2015
Bearbeitet: Christine Tobler
am 17 Dez. 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
am 17 Dez. 2015
Yes. the pivoted LU will be more accurate, although still not perfect. Nothing can achieve that, unlesss you use exact arithmetic.
Kategorien
Mehr zu Linear Algebra finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



