100% found this document useful (1 vote)
290 views6 pages

Acm Assignment-2: Doolittle LU Decomposition Cholesky Method Matlab-Code

This document contains code for implementing the Doolittle LU decomposition and Cholesky decomposition methods in Matlab. For Doolittle LU decomposition, it accepts a matrix A and vector B as input, calculates the L and U matrices, and solves for the vectors X and Z. For Cholesky decomposition, it checks if the input matrix A is symmetric and positive definite, then decomposes A into the upper triangular matrix U and solves for vectors X and Z. Sample outputs are provided for test matrices.

Uploaded by

Pritesh Kumar
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
100% found this document useful (1 vote)
290 views6 pages

Acm Assignment-2: Doolittle LU Decomposition Cholesky Method Matlab-Code

This document contains code for implementing the Doolittle LU decomposition and Cholesky decomposition methods in Matlab. For Doolittle LU decomposition, it accepts a matrix A and vector B as input, calculates the L and U matrices, and solves for the vectors X and Z. For Cholesky decomposition, it checks if the input matrix A is symmetric and positive definite, then decomposes A into the upper triangular matrix U and solves for vectors X and Z. Sample outputs are provided for test matrices.

Uploaded by

Pritesh Kumar
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/ 6

ACM ASSIGNMENT-2

Doolittle LU decomposition

Cholesky Method
Matlab-Code

PRITESH KUMAR SINGH


14ME254
Doolittle LU decomposition
clc;
a=input('enter matrix a :');
b=input('enter matrix b :');

[m,n]=size(a);
l=a;

if m~=n
disp('Matrix entered is not a sqaure matrix')
else

%to find L and Z


for k=1:(n-1)
for i=k+1:n
ratio=l(i,k)/l(k,k);
l(i,k)=ratio;
for j=k+1:n
l(i,j)=l(i,j)-ratio*l(k,j);
end
end
end

for i=2:n
sum=0;
for j=1:i-1
sum=sum+l(i,j)*z(j);
end
z(1)=b(1)
z(i)=(b(i)-sum)
end

%to find U and X


for i=1:n-1
for k=(i+1):n
ratio=a(k,i)/a(i,i);
for j=i:n
a(k,j)=a(k,j)-ratio*a(i,j);
end
end
end

for i=n:-1:1
sum1=0;
for j=i+1:n
sum1=sum1+a(i,j)*x(j);
end
x(i)=(z(i)-sum1)/a(i,i)
end
end
OUTPUT
enter matrix a :[1 4 1;1 6 -1;2 -1 2]

enter matrix b :[7 13 5]

z=
7.0000
-93.8000
-431.1000

z=
7.0000
6.0000
-431.1000

z=
7.0000
6.0000
-431.1000

z=
7
6
18

x=
-44.9000 1.0000 -2.0000

x=
-44.9000 1.0000 -2.0000

x=
5 1 -2
Cholesky Method

clc;
clear all;

A= input('enter matrix a :');


B= input('enter matrix b :');

%A = [-2 -1 0 ; -1 -2 -1 ; 0 -1 1] ;
%B = [1 ; 0 ; 0] ;
[m,n]=size(A);
C=zeros(n);

for i=1:m
for j=1:n
C(i,j)=A(j,i);
end
end

if (C==A)
disp('Matrix A is a symmetric matrix ');

E=eig(A);
for i=1:m
if E(i,1)>0
kishan=1;
else
kishan=0;
end
end

if kishan==1
disp('Matrix A is positve Definite ');
for i=1:n
if i==1
for j=i+1:n
u(i,i)=sqrt(A(i,i))
u(i,j)=A(i,j)/u(i,i)
end
end

if (i>1) && (i<=n)


for k=1:i-1
u(i,i)=sqrt(A(i,i)-(u(k,i)).^2)
for j=i+1:n
u(i,j)=(A(i,j)-u(k,i)*u(k,j))/u(i,i)
end
end

end
i=i+1;
end
l=u';
z=zeros(n,1);

%to find Z in L*Z=B


for i=1:n
sum=0;
for j=1:i-1
sum=sum+l(i,j)*z(j);
end
z(i)=(B(i)-sum)/l(i,i)
end

%to find X in U*X=Z


for i=n:-1:1
sum1=0;
for j=i+1:n
sum1=sum1+u(i,j)*x(j);
end
x(i)=(z(i)-sum1)/u(i,i)
end

else
disp('Matrix A is symmetric but not positive definite matrix');
disp('cholesky decomposition method cannot be applied');
end

else
disp('Matrix A is not a symmetric matrix');
disp('cholesky decomposition method cannot be applied');
end

OUTPUT:
enter matrix a :[10 -1 2;-1 12 -1;2 -1 20]
enter matrix b :[11 10 21]

Matrix A is a symmetric matrix


Matrix A is a positive Definite matrix

u=
3.1623

u=
3.1623 -0.3162

u=
3.1623 -0.3162
u=
3.1623 -0.3162 0.6325

u=
3.1623 -0.3162 0.6325
0 3.4496 0

u=
3.1623 -0.3162 0.6325
0 3.4496 -0.2319

u=
3.1623 -0.3162 0.6325
0 3.4496 -0.2319
0 0 4.4211

z=
3.4785
0
0

z=
3.4785
3.2177
0

z=
3.4785
3.2177
4.4211

x=
001
x=
0 1.0000 1.0000

x=
1.0000 1.0000 1.0000

You might also like