1、程序
function [ L,U,zhibiao ] = LU_Decom( A ) %求矩阵A的LU分解,其中, %A为要分解的矩阵 %L为单位下三角阵 %U为单位下三角阵
%flag为指标向量,zhishi='failure'表示计算失败,zhibiao='OK'表示计算成功 [n,m]=size(A);
%要求所分解的矩阵是方阵;否则停止结算,并输出出错信息 if n~=m
error('The rows and columns of matrix A must be equal!'); return; end
L=eye(n);U=zeros(n);zhibiao='OK'; for k=1:n for j=k:n z=0;
for q=1:k-1
z=z+L(k,q)*U(q,j); end
U(k,j)=A(k,j)-z; end
if abs(U(k,k)) for i=k+1:n z=0; z=z+L(i,q)*U(q,k); end L(i,k)=(A(i,k)-z)/U(k,k); end end 运算结果: A=[1 2 3;4 5 6;7 8 0] [ L,U,zhibiao] = LU_Decom( A ) 00??1?L??010????7?1.21???123?? U??056????00?13??zhibiao?OK2、库函数 A=[1 2 3;4 5 6;7 8 0] [L,U,P]=lu(A) 运算结果: 00??1?L??0.142910????0.57140.51??80??7? U??00.85713???04.5??0??001??P??100????010??