I have Matlab code. I need to add possibility to get input from console. Now I have only rand function. I don't need abs
Posted: Fri May 20, 2022 6:25 pm
I have Matlab code. I need to add possibility to get
input from console. Now I have only rand function. I don't
need absolutely new code, I am asking for adding some code to have
input from console to this initial code. Thank you.
n=input('Enter the size of the matrix\n');
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
input from console. Now I have only rand function. I don't
need absolutely new code, I am asking for adding some code to have
input from console to this initial code. Thank you.
n=input('Enter the size of the matrix\n');
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