Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

HW5 Programming solutions

Q4
1. LU factorization without pivoting

Code:
% This script provides the solutoin to get LU factorization of A, without
% pivoting
%L is the lower triangular matrix
%U is the upper triangular matrix
% A is stored in the matrix U

A = [1 1 1 1 1; 1 2 3 4 5; 1 3 6 10 15; 1 4 10 20 35;1 5 15 35 70]'


n =5;

U =A;
L=eye(n);
for k=1:n
if (U(k,k) == 0) Error('Pivoting is needed!'); end
L(k+1:n,k)=U(k+1:n,k)/U(k,k);
for j=k+1:n
U(j,:)=U(j,:)-L(j,k)*U(k,:);
end
end
disp(' L value is :');
disp(L);
disp(' U Value is :');
disp(U);

Output:
>> clear
>> HW5_LU_nopivot

A=

1 1 1 1 1
1 2 3 4 5
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70

L value is :
1 0 0 0 0
1 1 0 0 0
1 2 1 0 0
1 3 3 1 0
1 4 6 4 1
U Value is :
1 1 1 1 1
0 1 2 3 4
0 0 1 3 6
0 0 0 1 4
0 0 0 0 1

2. LU factorization with pivoting


% This script provides LU factorization with partial pivoting PA = LU
% L is the lower triangular matrix
% U is the upper triangular matrix
% P is the pivot

A = [ 1 1 1 1 1; 1 2 3 4 5; 1 3 6 10 15; 1 4 10 20 35; 1 5 15 35 70]'

[n,n]=size(A);
L=eye(n);
P=L;
U=A;
for k=1:n
[pivot m]=max(abs(U(k:n,k)));
m=m+k-1;
if m~=k
% interchange rows m and k in U
temp=U(k,:);
U(k,:)=U(m,:);
U(m,:)=temp;
% interchange rows m and k in P
temp=P(k,:);
P(k,:)=P(m,:);
P(m,:)=temp;
if k >= 2
temp=L(k,1:k-1);
L(k,1:k-1)=L(m,1:k-1);
L(m,1:k-1)=temp;
end
end
for j=k+1:n
L(j,k)=U(j,k)/U(k,k);
U(j,:)=U(j,:)-L(j,k)*U(k,:);
end
end
disp(' The L value is :');
disp(L);
disp( ' The U value is :');
disp(U);
disp(' The P value is :');
disp(P);
Output:

A=

1 1 1 1 1
1 2 3 4 5
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70

The L value is :
1.0000 0 0 0 0
1.0000 1.0000 0 0 0
1.0000 0.5000 1.0000 0 0
1.0000 0.7500 0.7500 1.0000 0
1.0000 0.2500 0.7500 -1.0000 1.0000

The U value is :
1.0000 1.0000 1.0000 1.0000 1.0000
0 4.0000 14.0000 34.0000 69.0000
0 0 -2.0000 -8.0000 -20.5000
0 0 0 -0.5000 -2.3750
0 0 0 0 -0.2500

The P value is :
1 0 0 0 0
0 0 0 0 1
0 0 1 0 0
0 0 0 1 0
0 1 0 0 0

3. Cholesky factorization
% This script provides cholesky factorization of the matrix A
% L is the lower triangular matrix
% L is replaced by A here

A = [1 1 1 1 1; 1 2 3 4 5; 1 3 6 10 15; 1 4 10 20 35; 1 5 15 35 70]'

[n nn]=size(A);
for k=1:n
A(k,k)=sqrt(A(k,k));
A(k+1:n,k)=A(k+1:n,k)/A(k,k);
for j=k+1:n
A(j:n,j)=A(j:n,j)-A(j,k)*A(j:n,k);
end
end
disp(' A after cholesky factorization')
disp(A)
L = A;
P = L*L';
disp(' According cholesky factorization A = L* Lt')
disp(P)

A_transpose = A'
U = conj(A_transpose)
disp(' U is the upper traingular matrix of A');
disp(U);

R = U'*U
disp('R is equal to P, that satisfies cholesky factorization of R tranpose*R ');

OutPut

>> HW5_Cholesky_factorization

A=

1 1 1 1 1
1 2 3 4 5
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70

A after cholesky factorization


1 1 1 1 1
1 1 3 4 5
1 2 1 10 15
1 3 3 1 35
1 4 6 4 1

According cholesky factorization A = L* Lt


5 14 29 43 16
14 52 121 192 44
29 121 331 545 70
43 192 545 1245 70
16 44 70 70 70

A_transpose =

1 1 1 1 1
1 1 2 3 4
1 3 1 3 6
1 4 10 1 4
1 5 15 35 1
U=

1 1 1 1 1
1 1 2 3 4
1 3 1 3 6
1 4 10 1 4
1 5 15 35 1

U is the upper traingular matrix of A


1 1 1 1 1
1 1 2 3 4
1 3 1 3 6
1 4 10 1 4
1 5 15 35 1

R=

5 14 29 43 16
14 52 121 192 44
29 121 331 545 70
43 192 545 1245 70
16 44 70 70 70

R is equal to P, that satisfies cholesky factorization of R tranpose*R

You might also like