How can I choose certain parameters to choose different ways to calculate the determinant?

1 visualizzazione (ultimi 30 giorni)
% 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?

Risposte (1)

Voss
Voss il 9 Mag 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)
The matrix is Square
detM = -2
detM = get_det(M,'B')
The matrix is Square
detM = -2
detM = get_det(M,'C')
The matrix is Square For method C, matrix must be 3-by-3. Using method A instead.
detM = -2
detM = get_det(M,'foo')
The matrix is Square Unrecognized method: foo
detM = NaN
M = [5 64 32; 43 51 23; 70 8 14];
detM = get_det(M)
The matrix is Square
detM = -36070
detM = get_det(M,'B')
The matrix is Square For method B, matrix must be 2-by-2. Using method A instead.
detM = -36070
detM = get_det(M,'C')
The matrix is Square
detM = -36070
detM = get_det()
The matrix is Square
detM = -779
detM = get_det([3 34; 23 1])
The matrix is Square
detM = -779
detM = get_det([1 2 3])
Error using solution>get_det
The matrix is not square
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 Commenti
Jonas Morgner
Jonas Morgner il 10 Mag 2022
Hey, first of all thank you so much for the code, however I still get an error when entering the code:
Error: Function definition are not supported in this
context. Functions can only be created as local or
nested functions in code files.
How can I fix this?
Voss
Voss il 10 Mag 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

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by