How I write a code calculating a matrix determinant recursively without using build-in operation?
41 views (last 30 days)
Show older comments
Hello I get a project to do and I need to know how to write recursive matrix determinanent without using build-in operation?
3 Comments
Walter Roberson
on 3 Jul 2022
https://en.m.wikipedia.org/wiki/Determinant#Calculation
When recursive calculation is asked for, that typically means that you are expected to do the calculation by The Method of Expansion Of Minors, which takes time proportional to factorial(n)
Answers (1)
Gyan Vaibhav
on 8 Nov 2023
Hi Omer,
I understand that you want to write a recursive function that calculates the determinant without using the inbuilt MATLAB function, and then verify it using the inbuilt function.
The “myDet” function seems correct. It's a recursive function to calculate the determinant of a matrix using the Laplace expansion.
However, the comparison part has some issues. You're calculating the determinant twice: once using the “myDet” function and once using MATLAB's built-in det function. But in the first calculation, you're not using “myDet” directly on the matrix a, instead you're calculating the determinant inside a loop, which is unnecessary because “myDet” is already a recursive function.
Here is the modified code where I have removed the first loop where you were calculating the determinant manually. This is unnecessary as “myDet” function already does that.
Though you have used “randi” to generate the matrix, which only populates the matrix with integers. However, for the cases of floating-point numbers, I have replaced the equality check with a tolerance check. This is due to the nature of floating-point arithmetic, the two determinants might not be exactly equal, but they could be very close. Checking if their difference is less than a small tolerance value is a common way to compare floating point numbers.
%create random 3X3 'a' matrix
a=randi(3,3)
% Calculate determinant using your function
determinant = myDet(a);
% Calculate determinant using built-in function
builtin_det = det(a);
% Print both determinants
disp(['Our calculation: ', num2str(determinant)]);
disp(['MATLAB built-in calculation: ', num2str(builtin_det)]);
% Compare the two results
if abs(determinant - builtin_det) < 1e-6 % Use a tolerance instead of equality check
disp('The determinant calculation is the same as the built-in matrix MATLAB operation')
else
disp('The determinant calculation is not the same as the built-in matrix MATLAB operation')
end
Below is the required function.
function det = myDet(A)
if isscalar(A)
det = A;
return
end
det = 0;
top_row = A(1,:);
A(1,:) = [];
for i = 1:size(A,2)
A_i = A;
A_i(:,i) = [];
det = det + (-1)^(i+1)*top_row(i)*myDet(A_i);
end
end
Hope this helps and resolves your issue.
Thanks
Gyan
0 Comments
See Also
Categories
Find more on Logical 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!