Please help me solve such problem. I know why I get this error (because it is not a square matrix). However, the data (4*3 matrix) given by my tutor cannot be changed, how to make it run? Thank you.
    조회 수: 8 (최근 30일)
  
       이전 댓글 표시
    

here is the function:
function [L,U] = eluinv(A)
[m,n]=size(A);
[L,U] = lu(A)
LU = roundingfixer(L*U);
if (A == LU)
 disp('Yes, I got a factorization')
end
if (rank(U) == rank(A))
 disp('U is an echelon form of A')
else
 disp('U is not an echelon form of A? What is wrong?!')
end
if (rank(A) == n)
 invL = [L eye(n)];
 invU = [U eye(n)];
 invL = rref(invL);
 invU = rref(invU);
 invL = invL(:,(n+1:n*2));
 invU = invU(:,(n+1:n*2));
 invA = roundingfixer(invU * invL)
 P = roundingfixer(inv(A))
 disp('the inverse of A calculated using LU factorization is')
 disp(invA)
 if (P==invA)
 disp('Yes, LU factorization works for calculating the inverses')
 else
 disp('LU factorization does not work for me!?')
 end
else
 sprintf('A is not invertible')
 invA = [];
end
end
roundingfixer.m:
function B = roundingfixer(A)
[m,n]=size(A);
A=closetozeroroundoff(A,7)
for i=1:m
 for j=1:n
 A(i,j) = round(A(i,j),8);
 end
end
B=A;
end
댓글 수: 0
답변 (1개)
  Walter Roberson
      
      
 2021년 3월 19일
        You need to decide whether to use eye(m) or eye(m,n). The choice will affect 
 P = roundingfixer(inv(A))
That is going to fail. Your A is not square, so inv(A) is going to fail.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

