How can I choose certain parameters to choose different ways to calculate the determinant?
1 view (last 30 days)
Show older comments
% A
A = [3 34; 23 1] % Create matrix
det(A) % Calculate determinant
%B
ad = 3*1 % Defining first diagonal AD
cb = 23*34 % Defining second diagonal CB
ad-cb % Calculating the determinant
%C
% Defining variables
a = 5
b = 64
c = 32
d = 43
e = 51
f = 23
g = 70
h = 8
i = 14
% Creating Matrix M from variables
M = [a b c; d e f; g h i]
% Defining the forward diagonals
aei = a*e*i
bfg = b*f*g
cdh = c*d*h
% Defining the backward diagonals
afh = a*f*h
bdi = b*d*i
ceg = c*e*g
% Calculating determinant
detM = aei+bfg+cdh-afh-bdi-ceg
% D
if ndims (A)==2 && (size(A,1)==size(A,2)) % checking if the dimensions are squared
disp("The matrix is Square") % success message
det(A)
else disp("The matrix is not square") % error message
end
In exercise D I need to have two input variables. first one the matrix and second one a variable to select the way the determinant is calculated.
I am struggeling with finding a way to input the second variable.
Additionally I first need to check if the array is sqared, and depending if it is square I need to calculate the determinant. (I ve already done this I think haha).
Can some one help me?
0 Comments
Answers (1)
Voss
on 9 May 2022
Below is a function get_det, made by adapting your code, which takes a matrix as its first input and a character vector specifying a method as its second input.
Here are some lines of code calling the function with different inputs:
M = [1 2; 3 4];
detM = get_det(M)
detM = get_det(M,'B')
detM = get_det(M,'C')
detM = get_det(M,'foo')
M = [5 64 32; 43 51 23; 70 8 14];
detM = get_det(M)
detM = get_det(M,'B')
detM = get_det(M,'C')
detM = get_det()
detM = get_det([3 34; 23 1])
detM = get_det([1 2 3])
The function:
function out = get_det(A,method)
if ~nargin
A = [3 34; 23 1]; % Create default matrix, if none given
end
if ndims (A)==2 && (size(A,1)==size(A,2)) % checking if the matrix is square
disp("The matrix is Square") % success message
else
% disp("The matrix is not square") % error message
error("The matrix is not square") % error message
end
if nargin < 2
method = 'A';
end
if strcmp(method,'B') && size(A,1) ~= 2
disp('For method B, matrix must be 2-by-2. Using method A instead.')
method = 'A';
elseif strcmp(method,'C') && size(A,1) ~= 3
disp('For method C, matrix must be 3-by-3. Using method A instead.')
method = 'A';
end
switch method
case 'A' %A
out = det(A); % Calculate determinant
case 'B' %B
ad = A(1,1)*A(2,2); % Defining first diagonal AD
cb = A(1,2)*A(2,1); % Defining second diagonal CB
out = ad-cb; % Calculating the determinant
case 'C' %C
% Defining the forward diagonals
aei = A(1,1)*A(2,2)*A(3,3);
bfg = A(1,2)*A(2,3)*A(3,1);
cdh = A(1,3)*A(2,1)*A(3,2);
% Defining the backward diagonals
afh = A(1,1)*A(2,3)*A(3,2);
bdi = A(1,2)*A(2,1)*A(3,3);
ceg = A(1,3)*A(2,2)*A(3,1);
% Calculating determinant
out = aei+bfg+cdh-afh-bdi-ceg;
otherwise
disp(sprintf('Unrecognized method: %s',method));
out = NaN;
end
end
2 Comments
Voss
on 10 May 2022
The function get_det should be in an m-file.
Probably it's best to make a new m-file, copy the definition of the function get_det (i.e., everything in my answer from the line function out = get_det(A,method) until the end), paste that into the m-file, and save the file as get_det.m
See Also
Categories
Find more on Develop Apps Using App Designer 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!