Help with nxn matrices
이전 댓글 표시
I am having a bit of trouble with an nxn matrix problem.
The problem is: Write a user-defined MATLAB function that calculates the determinant of a square ( _n x n _ ) matrix, where n can be 2, 3, or 4. For function name and arguments, use D= Determinant(A). The input argument A is the matrix whose determinant is calculate. The function Determinant show first check if the matrix is a square. If it is not, the output D should be the message "The matrix must be square." Use Determinant to calculate the determinant of the following matrices.
It then goes on to give two different matrices, however, that is not what I need help with. My function is what is giving me difficulties. I entered the 3x3 matrix that I was given first, and came up with an error in the 34th line of my code saying that it was incomplete or incorrect. I have italicized this line to make it easier to find. Any help is greatly appreciated. Thank you!
Here is my code:
function D = Determinant( A )
% The function calculates the determinant of an nxn matrix A,
% where n can be 2, 3, or 4.
[n m]=size(A); % n= # of rows in A, m= # of columns in A
if n ~= m % check if A is a square matrix
d ='ERROR' % if not, display error message
disp('The matrix has to be square')
elseif n > 4 % otherwise, check if A is larger than 4x4
d ='ERROR' % if so, display error message
disp('The matrix size cannot be larger than 4 by 4')
elseif n == 2 % otherwise, check if A is 2x2
D=det2by2(A); % if so, call the subfunction for a 2x2 determinant
elseif n == 3 % otherwise, check if A is 3x3
D=det3by3(A); % if so, call the subfunction for a 3x3 determinant
elseif n == 4 % otherwise, check if A is 4x4
D=det4by4(A); % if so, call the subfunction for a 4x4 determinant
end % end the program
%Subfunctions
function D4 = det4by4( A )
% The function calculates the determinant of a 4 by 4 matrix.
Sa=A(2:4,2:4);
Sb=A(2:4,[1 3 4]);
Sc=A(2:4,[1 2 4]);
Sd=A(2:4,1:3);
%
_D4=A(1,1)*det3by3(Sa)-A(1,2)*det3by3(Sb)+A(1,3)*det3by3(Sc)-
A(1,4)*det3by3(Sd);_
%
function d3=det3by3(A)
%
% Evaluating the determinant by expanding using the first row
%
S1=A(2:3,2:3);
%
S2=A(2:3,[1 3]);
%
S3=A(2:3,1:2);
%
d3=A(1,1)*det2by2(S1)-A(1,2)*det2by2(S2)+A(1,3)*det2by2(S3);
%
% Subfunction
%
function d2=det2by2(B)
d2=B(1,1)*B(2,2)-B(1,2)*B(2,1);
end
end
댓글 수: 4
Claire
2015년 1월 29일
Claire
2015년 1월 29일
Star Strider
2015년 1월 29일
To format your code, highlight it, then use the [{}Code] button. Alternatively, put two spaces between the left window margin and each line of your code.
Claire
2015년 1월 29일
채택된 답변
추가 답변 (1개)
Iboi Lucky
2018년 10월 19일
0 개 추천
Nice job! Can I please get a similar code that would solve determinant above 4 x 4 matrix please!!
댓글 수: 2
Steven Lord
2018년 10월 19일
If this is not a homework assignment, read the Limitations section on the documentation page for the det function. Unless you absolutely positively need the determinant and understand those limitations, use cond or rcond instead. If you do absolutely need the determinant, call det.
If this is a homework assignment, your textbook probably has some pseudocode that you can implement in MATLAB. If not, you could use the idea shown at the start of the Wikipedia article for the determinant to write the determinant of an n-by-n matrix as sums of multiples of the determinants of n (n-1)-by-(n-1) matrices.
Iboi Lucky
2018년 10월 24일
It's actually my project work, my supervisor asked me to write a code that would solve n by n determinant which is actually my project topic and to be honest, I can't seem to find it anywhere online.
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!