There is an error in my code
1 次查看(过去 30 天)
显示 更早的评论
Here is the function:
function [L,U] = eluinv(A)
[~,n]=size(A);
[L,U] = lu(A)
LU = roundingfixer(L*U);
if (A == LU)
disp('LU factorization is valid')
else
disp('A mistake was made somewhere.')
end
if (rank(U) == rank(A))
disp('U is an echelon form of A')
else
disp('A mistake was made somewhere.')
end
if (rank(A) == n)
% Use rref to row reduce the matrices
invL = [L eye(n)];
invU = [U eye(n)];
invL = rref(invL);
invU = rref(invU);
% Cut off the first half of the matrix
invL = invL(:,(n+1:n*2));
invU = invU(:,(n+1:n*2));
invA = roundingfixer(invU * invL)
P = roundingfixer(inv(A))
if (invA == P)
disp('LU works for calculating the inverse for A')
else
disp('Error in calculating the inverse for A')
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 个)
Cris LaPierre
2021-3-18
编辑:Cris LaPierre
2021-3-18
Look at line 17: invL = [L eye(n)];
You are concatenating L and eye(n) horizontally. The error message is again telling you what the problem is. See this documentation page for more.
A = [1; 2]
B = [3 4 5]
C = [A B]
8 个评论
Cris LaPierre
2021-3-18
I am not aware of a function that will do this for you. I believe you will need to write your code in a way to either ensure the two matrices have the same number of rows.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!