0% found this document useful (0 votes)
35 views2 pages

Matlan LU Code

This document contains MATLAB code to perform LU factorization on a user-input square matrix. It calculates the L and U matrices, checks that the matrix is square, computes the inverse of the original matrix using the inverse of L and U, and displays the determinant.

Uploaded by

Soban Bakhtiyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

Matlan LU Code

This document contains MATLAB code to perform LU factorization on a user-input square matrix. It calculates the L and U matrices, checks that the matrix is square, computes the inverse of the original matrix using the inverse of L and U, and displays the determinant.

Uploaded by

Soban Bakhtiyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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