LU Decomposition

3220 단어 수치 분석
function       [L,U,y,x]=LU(A,b)

%Input :     A is the coefficient matrix
%      :     b is the row vector

%Output:     L is the below triangle matrix
%      :     U is the above triangle matrix
%      :     y is the Ly=b
%      :     x is the solution of Ux=y
%Funciton:   LU to solve the equation

n=size(A,2);
U(1,:)=A(1,:);
L(:,1)=A(:,1)/U(1,1);

for r=2:n

for i=r:n

    U(r,i)=A(r,i)-L(r,1:r-1)*U(1:r-1,i);
    L(i,r)=(A(i,r)-L(i,1:r-1)*U(1:r-1,r))/U(r,r);

end

end
y=zeros(n,1);

y(1)=b(1);
for i=2:n
    y(i)=b(i)-L(i,1:i-1)*y(1:i-1);
end
x=zeros(n,1);
x(n)=y(n)/U(n,n);
for i=n-1:-1:1
    x(i)=(y(i)-U(i,i+1:n)*x(i+1:n))/U(i,i);
end



end


좋은 웹페이지 즐겨찾기