Here is the function:
function [] = eluinv(A)[~,n]=size(A);[L,U] = lu(A);format compact%have to round to supress extra zeroes
if (closetozeroroundoff(A) == closetozeroroundoff(round(L * U))) disp('Yes, I have got LU factorization')end%comparing the reduced row echelon forms of each
if closetozeroroundoff(rref(U)) == closetozeroroundoff(rref(A)) disp('U is an echelon form of A')else disp('Something is wrong')end%if is invertible, code will continue and inversion will occur
if rank(A) ~= min(size(A)) sprintf('A is not invertible') invA=[]; returnelse eyeForL = [L eye(n)]; eyeForU = [U eye(n)]; aux = rref(eyeForL); aux2 = rref(eyeForU); invL = aux(:,(n+1):size(aux,2)); invU = aux2(:,(n+1):size(aux2,2)); invA = invU*invL; P = inv(A); if (closetozeroroundoff(invA - P) == zeros(size(A))) disp('Yes, LU factorization works for calculating the inverses') else disp('LU factorization does not work for me?') endend
Best Answer