You are on page 1of 2

9/27/19 4:33 AM Z:\Untitled.

m 1 of 2

clc
clear all
disp('LU factorization');
m=input(' Input the number of rows: ');
n=input(' Input the number of columns: ');
for i=1:m
for j=1:n
A(i,j)=input('Input the entries: ')
end
end
A=reshape(A,m,n)
[n,m]=size(A);
L=zeros(length(A));
U=zeros(length(A));
if n==m
for j=1:length(A)
for i=1:length(A)
sum=0;
q=j-1;
for k=1:q
sum=sum+(L(i,k)*U(k,j));
end
if i==j
U(i,j)=1;
end
if i >=j
L(i,j)=A(i,j)-sum;
else
U(i,j)=(1/L(i,i))*(A(i,j)-sum);
end
end
end
else
end
if n==m
L
U
LU=L*U
else
disp('The matrix is not square')
end
X=pinv(U);%U inverse
Y=pinv(L);%L inverse
disp('The inverse of L is ')
X
disp('The inverse of U is ')
Y
C=X*Y;%calculates inverse of B in terms L and U inverse
disp('So the inverse of A is ')
C
A_det=det(L)*det(U);
9/27/19 4:33 AM Z:\Untitled.m 2 of 2

disp('The determinant of the matrix is ')


A_det

You might also like