Trying to write a program that calculates the inverse of a 3x3 matrix. My program works for some matrices, but not for all. Can someone please look at my code and assist me?

17 visualizaciones (últimos 30 días)
%calculate determinant
m = input('Enter a 3x3 matrix using bracket notation: ');
a = m(1,1);
b = m(1,2);
c = m(1,3);
d = m(2,1);
e = m(2,2);
f = m(2,3);
g = m(3,1);
h = m(3,2);
i = m(3,3);
detA = a*(e*i-f*h);
detB = b*(d*i-f*g);
detC = c*(d*h-e*g);
det = detA - detB + detC;
%calculate cofactor
a2 = e*i-h*f;
b2 = -d*i-g*f;
c2 = d*h-g*e;
d2 = -(b*i-h*c);
e2 = a*i-g*c;
f2 = -(a*h-g*b);
g2 = b*f-e*c;
h2 = -(a*f-d*c);
i2 = a*e-d*b;
detA2 = a2*(e2*i2-f2*h2);
detB2 = b2*(d2*i2-f2*g2);
detC2 = c2*(d2*h2-e2*g2);
cof = detA2 - detB2 + detC2;
%cofactor matrix
n = [a2 b2 c2; d2 e2 f2; g2 h2 i2];
%calculate transpose of cofactor
v(:,1) = n(1,:);
v(:,2) = n(2,:);
v(:,3) = n(3,:);
%calculate inverse
if(det ~= 0)
inv = (1/det)*v;
disp(inv);
else
fprintf('Matrix is not invertible.\n');
end;
  9 comentarios
Walter Roberson
Walter Roberson el 19 de Sept. de 2015
I get exactly the same values.
Enter a 3x3 matrix using bracket notation: [1 9 8; 3 2 1; 4 5 6]
-0.111111111111111 0.222222222222222 0.111111111111111
0.349206349206349 0.412698412698413 -0.365079365079365
-0.111111111111111 -0.492063492063492 0.396825396825397
>> clear inv %badly named variable!
>> inv(m)
ans =
-0.111111111111111 0.222222222222222 0.111111111111111
0.222222222222222 0.412698412698413 -0.365079365079365
-0.111111111111111 -0.492063492063492 0.396825396825397
dpb
dpb el 19 de Sept. de 2015
Walter, ans(2,1) isn't within roundoff altho rest are...a quick glance might overlook that.

Iniciar sesión para comentar.

Respuesta aceptada

the cyclist
the cyclist el 19 de Sept. de 2015
Your definition of b2 has a sign error.

Más respuestas (1)

Zeus
Zeus el 15 de Jun. de 2018
Editada: Zeus el 15 de Jun. de 2018
This code works for any square and invertible matrix of any order
function A_inverse=inverse(A)
% A_inverse as the name suggests is the inverse of A
% A is a square matrix which is invertible
% if A is not a square matrix or A is square matrix but not invertible,then the output is not equal to inverse of A
a=length(A); % the order of A is axa
I=eye(a);
augmat=[A I]; % AUGMENTED MATRIX
% GAUSSIAN ELMINATION METHOD:
% when A is invertible, [A I] is row equivalent to [I inv(A)]
% in other words, the row operations that convert A to I also converts I to inv(A)
% I is identity matrix of order axa, inv(A) is the inverse of A of order axa
% Converting A to its Echelon form
for i=1:a-1
m=augmat(i,i);
augmat(i,:)=augmat(i,:)/m; % normalization,so that pivot values will be equal to 1
for j=i:a-1
augmat(j+1,:)=augmat(j+1,:) - augmat(i,:)*augmat(j+1,i); % making the elements below the pivots as zero
end
end
augmat(a,:)=augmat(a,:)/augmat(a,a); % noralization of the last row of A
% Converting A from its Echelon form to Row Reduced Echelon form
for k=2:a
for g=(k-1):-1:1
augmat(g,:)=augmat(g,:)-augmat(k,:)*augmat(g,k); % makes the elements above pivots to be row
end
end
%We end up with A converted to I and I will be converted to inv(A)
A_inverse=augmat(:,a+1:2*a); % extracting inv(A) from augmented matrix [I inv(A)]
end

Categorías

Más información sobre Matrix Indexing 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