求解Ax=d的解x
1. LU分解
% file: myLU.m
function [L,U]=myLU(A) %实现对矩阵A的LU分解,L为下三角矩阵 A [n,n]=size(A); L=zeros(n,n); U=zeros(n,n); for i=1:n L(i,i)=1; end for k=1:n for j=k:n U(k,j)=A(k,j)-sum(L(k,1:k-1).*U(1:k-1,j)\'); end for i=k+1:n L(i,k)=(A(i,k)-sum(L(i,1:k-1).*U(1:k-1,k)\'))/U(k,k); end end
2. 用分解获得的L,U求解x
[L,U] = myLU(A) [n,m]=size(A) y(1)=d(1); for i=2:n for j=1:i-1 d(i)=d(i)-L(i,j)*y(j); end y(i)=d(i); end x(n)=y(n)/U(n,n); for i=(n-1):-1:1 for j=n:-1:i+1 y(i)=y(i)-U(i,j)*x(j); end x(i)=y(i)/U(i,i); end