Need some help with my function.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Daniel Vonderhaar
el 8 de Jul. de 2021
Comentada: Daniel Vonderhaar
el 8 de Jul. de 2021
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.
0 comentarios
Respuesta aceptada
Image Analyst
el 8 de Jul. de 2021
You're calling nonsingular() recursively. That is, inside it, it's calling itself. However you define it to take 3 input arrays, yet when you call it you pass it only one instead of 3.
Why are you calling it recursively in the first place, and even if you did, why are you not passing the correct number of arguments? And you don't have any way to "back out" of the recursive calls so it will just go deeper and deeper.
Maybe you should just comment out the lines
inverse_A=nonsingular(A)
inverse_B=nonsingular(B)
inverse_C=nonsingular(C)
0 comentarios
Más respuestas (1)
KSSV
el 8 de Jul. de 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) ;
1 comentario
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!