LU Decomposition

来源:互联网 发布:java 代码生成 方式 编辑:程序博客网 时间:2024/04/27 20:07
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 equationn=size(A,2);U(1,:)=A(1,:);L(:,1)=A(:,1)/U(1,1);for r=2:nfor 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);endendy=zeros(n,1);y(1)=b(1);for i=2:n    y(i)=b(i)-L(i,1:i-1)*y(1:i-1);endx=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);endend
0 0
原创粉丝点击