Code for determinant.
Mostra commenti meno recenti
I would like to built a code to find the determinant of a 24x24 matrix.I dont want to use the det(X) function,but a fuction that would be quick.
The one i created,whick is too slow for that.

2 Commenti
Stephen23
il 15 Nov 2018
"I dont want to use the det(X) function,but a function that would be quick."
Ah, the sweet, blind optimism of youth.
Have you actually looked at the definition of the determinant, and figured out how many calculations it would require for a 24x24 matrix? When is your estimated completion?
eden hazard
il 15 Nov 2018
Risposta accettata
Più risposte (1)
Reza Lashani
il 3 Feb 2021
Modificato: Reza Lashani
il 3 Feb 2021
0 voti
One of the fastest ways to determine the determinant of a matrix is doing row operation. For an invertible matrix we know that row operations finally reach to identity matrix which has determinant equal to 1. For calculating determinant we can write an efficient and of course fast code to do row operation and it is not necessary to make the first non-zero entry 1 by dividing it by itself. It cause we finally reach to a diagonal matrix (stead of identity matrix) and the determinant is equal to the product of pivots of the final matrix. Doing this algorithm on my ordinary system for a 24 × 24 matrix lates less than 1 second.
4 Commenti
John D'Errico
il 3 Feb 2021
Modificato: John D'Errico
il 3 Feb 2021
No. The best way to compute the determinant, If your matrix is non-symbolic is to use det. (det does use LU, so it is close to what you have suggested.) But even there, computing the determinant of a 24x24 matrix is just a silly thing to do. It is highly unstable.
Anyway, while you may THINK that your time of "less than a second" is a good thing. So it might be a smart idea to actually see how fast det is.
A = rand(24);
timeit(@() det(A))
ans =
4.8397e-06
So less than a second is correct. But in this case, MATLAB actually takes only a few MILLIONTHS of a second.
Writing your own code to do matrix operations is a BAD thing to do, especially if you don't really understand what is happening. And even if you do, it is STILL a bad thing. Never write your own code to perform computations when you already have high quality code available for the same purpose, written by people who are seriously good at what they do.
Consider that random matrix.
>> det(A)
ans =
0.030614
>> det(10*A)
ans =
3.0614e+22
>> det(A/10)
ans =
3.0614e-26
Is computing the determinant useful for ANYTHING? Not really, if you consider that simply scaling the matrix produces either a result that is huge, or incredibly tiny for the determinant. And that is almost always the reason people have to want to compute a determinant. Is the determinant zero?
Is that matrix singular? NO. Of course not. But if you use the determinant to decide if A/10 is singular, you might easily decide it is.
rank(A)
ans =
24
So while the best way to compute the determinant of a matrix is to use det, an even better way is to not compute the determinant at all. There is literally no good reason to compute the determinant of a 24x24 matrix.
Reza Lashani
il 3 Feb 2021
Modificato: Reza Lashani
il 3 Feb 2021
Yes you are right. Using det function or UL matrices is the best, most efficient and fastest way to compute determinant. I just wanted to show another way for computing matrix determinant and as you know the question is actually a practice and cause understand better MATLAB source code and time optimization for codes, the purpose is not using suggested algorithms.
mahdi moshtoghi
il 26 Feb 2021
hey, how to write the determinan 4*4 and 3*3 on matlab?
syms A [3 3]
syms B [4 4]
simplify(det(A))
simplify(det(B))
Categorie
Scopri di più su Linear Algebra in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!