How to use the lu decomposition to make matrix become invers

How to use the lu decomposition to make matrix become inverse matrix in Matlab. plz show the code

Solution

%demo.m

n=input(\'Enter the size of matrix: \');
A=rand(n,n);
[L,U,P] = lu(A);

% Solve linear system for Identity matrix
I=eye(size(A));
s=size(A,1);
Ainv=zeros(size(A));
for i=1:s
b=I(:,i);
Ainv(:,i)=TriangleBackwardSub(U,TriangleForwardSub(L,P*b));
end
disp(\'Matrix A\');
disp(A);
disp(\'Matrix Inverse(A)\');
disp(inv(A));
disp(Ainv);

%TriangleBackwardSub.m

function C=TriangleBackwardSub(U,b)
s=length(b);
C=zeros(s,1);
C(s)=b(s)/U(s,s);
for j=(s-1):-1:1
C(j)=(b(j) -sum(U(j,j+1:end)\'.*C(j+1:end)))/U(j,j);
end

%TriangleForwardSub.m

function C=TriangleForwardSub(L,b)

s=length(b);
C=zeros(s,1);
C(1)=b(1)/L(1,1);
for j=2:s
C(j)=(b(j) -sum(L(j,1:j-1)\'.*C(1:j-1)))/L(j,j);
end

How to use the lu decomposition to make matrix become inverse matrix in Matlab. plz show the codeSolution%demo.m n=input(\'Enter the size of matrix: \'); A=rand

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site