试用LU分解求解线性方程组
4x-y+z=7
4x-8y+z=-21
-2x+y+5z=15
1.代码实现
function X=exe311(A,B)
[N,N]=size(A);
X=zeros(N,1);
Y=zeros(N,1);
C=zeros(1,N);
R=1:N;
for p=1:N-1
[max1,j]=max(abs(A(p:N,p)));
C=A(p,:);
A(p,:)=A(j+p-1,:);
A(j+p-1,:)=C;
d=R(p);
R(p)=R(j+p-1);
R(j+p-1)=d;
if A(p,p)==0
disp('fail');
break
end
for k=p+1:N
mult=A(k,p)/A(p,p);
A(k,p)=mult;
A(k,p+1:N)=A(k,p+1:N)-mult*A(p,p+1:N);
end
end
Y(1)=B(R(1));
for k=2:N
Y(k)=B(R(k))-A(k,1:k-1)*Y(1:k-1);
end
X(N)=Y(N)/A(N,N);
for k=N-1:-1:1
X(k)=(Y(k)-A(k,k+1:N)*X(k+1:N))/A(k,k);
end
2.运行结果