Matlab. There is a code. ONLY backward substitution doesn't work correctly (at the end of code). Correct it please. ###C

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Matlab. There is a code. ONLY backward substitution doesn't work correctly (at the end of code). Correct it please. ###C

Post by answerhappygod »

Matlab. There is a code. ONLY backward substitution doesn't work
correctly (at the end of code). Correct it please.
###CODE
%A=rand(n) , B= rand(n,1)
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
%forward substitution
Y = zeros(n,1); %Intializing Y
Y(1) = B(1)/L(1,1);
for k=2:n
Y(k) = (B(k) -
L(k,1:k-1)*Y(1:k-1))/L(k,k);
end
Y
%backward substitution
% X = zeros(n,1);
% X(n) = Y(n)/U(n,n);
% for k=n-1:-1:1
% x(k) = (Y(k) - U(k,k+1:n)*X(k+1:n))/U(k,k);
%
% end
X
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply