Need some help with my function.

2 visualizaciones (últimos 30 días)
Daniel Vonderhaar
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.

Respuesta aceptada

Image Analyst
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)

Más respuestas (1)

KSSV
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
Daniel Vonderhaar
Daniel Vonderhaar el 8 de Jul. de 2021
Hello! Thank you for your help! now I have one last question...
Inverse of Matrix A
0.3571 0.0714
-0.2857 0.1429
Inverse_A =
[]
Matrix A does not have an inverse
Inverse of Matrix A
0.5000 0 0
0.6250 0.0000 -0.2500
-0.8750 0.5000 0.2500
as you can see when I run this code, (So glad it works this much!) this part
Inverse_A =
[]
Matrix A does not have an inverse
Inverse of Matrix A
0.5000 0 0
0.6250 0.0000 -0.2500
-0.8750 0.5000 0.2500
is supposed to look like this....
Inverse_B =
[]
Matrix B does not have an inverse
Inverse of Matrix C
0.5000 0 0
0.6250 0.0000 -0.2500
-0.8750 0.5000 0.2500
How do I make it look like that when I run it every time?

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by