19. Write a computer program to implement the LU-decomposition of a matrix A. Use Crout's method. Find det(A). Use this
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
19. Write a computer program to implement the LU-decomposition of a matrix A. Use Crout's method. Find det(A). Use this
A=rand(n) , B= rand(n,1)
%We should remeber that the n in both A and B has to be the same
number so the method works
% Here we initialize L and U in order to prevent dynamic
allocation during the process
% initializing L and U,to prevent dynamic allocation during the
process
L = zeros(n,n);
U = zeros(n,n);
%Substituting 1's im the diagonal of U
for i=1:n
U(i,i)=1;
end
%Calculating the 1st column of L
%the first column of L in Crout's factorization is always equal
to the
%first column of A
for i=1:n
L(i,1) = A(i,1);
end
%Calculating the elements in the first row of U(Except U11 which
already
%was already calculated
for i=2:n
U(1,i) = A(1,i) / L(1,1);
end
%calculating the remaining elements row after row.The elements
of L are calculated first
%because they are used for calculating the elements of U
for i = 2:n
for j = 2:i
L(i, j) = A(i, j) - L(i, 1:j - 1) * U(1:j - 1, j);%%%formula
end
for j = i + 1:n
U(i, j) = (A(i, j) - L(i, 1:i - 1) * U(1:i - 1, j)) / L(i,
i);%%%formula
end
end
L
U
%det(A)=det(L)*det(U).
% As we know, det(U)=1.
%Calculate det(L), which is
%the product of diagonal elements
detA=1;
for i=1:n
detA=detA*L(i,i);
end
detA
% AX=B
% let A=LU => LUX=B
% let UX=Y => LY=B
Y=L\B
X=U\Y
19. Write a computer program to implement the LU-decomposition of a matrix A. Use Crout's method. Find det(A). Use this decomposition to solve the system of linear equations Mz = f, where M A (44)