Need some help with my function.
Afficher commentaires plus anciens
Hello!
I have this function as shown below. Please note that from inverse_(A,B,C) to the end if the if statements are correct.
function [inverse_A, inverse_B, inverse_C]=nonsingular(A, B, C)
inverse_A=nonsingular(A)
A=[2 -1 ; 4 5];
Determinant_A=det(A);
if Determinant_A==0
inverse_A=[]
disp('Matrix A does not have an inverse')
else
Inverse_A=inv(A);
disp('Inverse of Matrix A')
disp(Inverse_A)
end
inverse_B=nonsingular(B)
B=[4 2 ;2 1];
Determinant_B=det(B);
if Determinant_B==0
inverse_B=[]
disp('Matrix B does not have an inverse')
else
Inverse_B=inv(B);
disp('Inverse of Matrix B')
disp(Inverse_B)
end
inverse_C=nonsingular(C)
C=[2 0 0;1 2 2;5 -4 0];
Determinant_C=det(C);
if Determinant_C==0
inverse_C=[]
disp('Matrix C does not have an inverse')
else
Inverse_C=inv(C);
disp('Inverse of Matrix C')
disp(Inverse_C)
end
end
And I need this function to be called nonsingular and be a seperate m file. This m file will be called in my code as shown here.
A=[2 -1 ; 4 5];
B=[4 2 ;2 1];
C=[2 0 0;1 2 2;5 -4 0];
% This is the function for Matrix A
nonsingular(A)
% This is the function for Matrix B
nonsingular(B)
% This is the function for Matrix C
nonsingular(C)
Do you all know what im doing wrong? Please let me know.
Réponse acceptée
Plus de réponses (1)
KSSV
le 8 Juil 2021
function inverse_A=nonsingular(A)
Determinant_A=det(A);
if Determinant_A==0
inverse_A=[]
disp('Matrix A does not have an inverse')
else
Inverse_A=inv(A);
disp('Inverse of Matrix A')
disp(Inverse_A)
end
Save the above in a file. This your function. Call the function as shown below.
A=[2 -1 ; 4 5];
B=[4 2 ;2 1];
C=[2 0 0;1 2 2;5 -4 0];
% This is the function for Matrix A
invA = nonsingular(A) ;
% This is the function for Matrix B
invB = nonsingular(B) ;
% This is the function for Matrix C
invC = nonsingular(C) ;
Catégories
En savoir plus sur Operators and Elementary Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!