This is LU deposition code of matlab. What's wrong with this code.... Thank you
n and B was given from above, what is
for i = 1:n
U(1,i) = A(1,i);
L(i,1) = A(i,1)/U(1,1); %u(1,1)=1
end
for j = 2:n
for k = j:n
U(j,k) = A(j,k)-L(j,1:j-1)*U(1:j-1,k);
L(k,j) = (A(k,j)-L(k,1:j-1)*U(1:j-1,j))/U(j,j);
end
end
for i = 1:n
x(i) = B(i) / L(i,i:n)*U(i:n,i);
end
fprintf("Ans x = \n");
fprintf("%f \n", x);
y=zeros(m,1); % initiation for y
y(1)=B(1)/L(1,1);
for i=2:m
%y(i)=B(i)-L(i,1)*y(1)-L(i,2)*y(2)-L(i,3)*y(3);
y(i)=-L(i,1)*y(1);
for k=2:i-1
y(i)=y(i)-L(i,k)*y(k);
end;
y(i)=(B(i)+y(i))/L(i,i);
end;
y
% Now we use this y to solve Ux = y
x=zeros(m,1);
x(m)=y(m)/U(m,m);
i=m-1;
q=0;
while (i~= 0)
x(i)=-U(i,m)*x(m);
q=i+1;
while (q~=m)
x(i)=x(i)-U(i,q)*x(q);
q=q+1;
end;
x(i)=(y(i)+x(i))/U(i,i);
i=i-1;
end;
x
Another program that you can use :
function[L,U,X]=LU_Parker(A,B)
[m n]=size(A);
if (m ~= n )
disp ( 'LR2 error: Matrix must be square' );
return;
end;
% Part 2 : Decomposition of matrix into L and U
L=zeros(m,m);
U=zeros(m,m);
for i=1:m
% Finding L
for k=1:i-1
L(i,k)=A(i,k);
for j=1:k-1
L(i,k)= L(i,k)-L(i,j)*U(j,k);
end
L(i,k) = L(i,k)/U(k,k);
end
% Finding U
for k=i:m
U(i,k) = A(i,k);
for j=1:i-1
U(i,k)= U(i,k)-L(i,j)*U(j,k);
end
end
end
for i=1:m
L(i,i)=1;
end
% Program shows U and L
U
L
% Now use a vector y to solve 'Ly=b'
y=zeros(m,1);
y(1)=B(1)/L(1,1);
for i=2:m
y(i)=-L(i,1)*y(1);
for k=2:i-1
y(i)=y(i)-L(i,k)*y(k);
y(i)=(B(i)+y(i))/L(i,i);
end;
end;
% Now we use this y to solve Ux = y
x=zeros(m,1);
x(1)=y(1)/U(1,1);
for i=2:m
x(i)=-U(i,1)*x(1);
for k=i:m
x(i)=x(i)-U(i,k)*x(k);
x(i)=(y(i)+x(i))/U(i,i);
end;
Get Answers For Free
Most questions answered within 1 hours.