Computing determinant of matrix A using LU factorization

조회 수: 11 (최근 30일)
Emily Gallagher
Emily Gallagher 2019년 10월 8일
댓글: the cyclist 2019년 10월 8일
I must compute the determinant of an nxn matrix A using LU factorization. I have already calculated L and U in a different 'lufact' function. For some reason though, my funcion for computing the determinant of A, which is just equal to the product of the diagonal entries of matrix U is not working.
seed = 7;
rng(seed)
A = randn(n,n);
[L,U] = lufact(A);
function f = determ(A)
%Computing determinant of A
f = prod(diag(U));
end
  댓글 수: 1
the cyclist
the cyclist 2019년 10월 8일
One weirdness here. If your function definition is really
function f = determ(A)
%Computing determinant of A
f = prod(diag(U));
end
then that is going to throw an error, because you don't pass in U. So, maybe you are not really calculating with the inputs you expect?

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

답변 (1개)

the cyclist
the cyclist 2019년 10월 8일
편집: the cyclist 2019년 10월 8일
If I use the lu function from MATLAB (instead of your lufact function), I get the expected result. So, I'd double-check your LU factorization code.
seed = 7;
n = 5;
rng(seed)
A = randn(n,n);
[L,U] = lu(A);
f = prod(diag(U))
d = det(A)
f =
-8.0745
d =
-8.0745

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by