一、顺序Gauss消去法
function x=order_gauss(a,b,flag)
if nargin<3,flag=0;end
n=length(b);
a=[a,b];
for k=1:n-1
if (a(k,k)==0)
disp('对角元素为0!')
return;
end
a((k+1):n,(k+1):(n+1))=a((k+1):n,(k+1):(n+1))...
-a((k+1):n,k)*a(k,(k+1):(n+1))/a(k,k)
a((k+1):n,k)=zeros(n-k,1);
if flag==0,a,end
end
x=zeros(n,1)
x(n)=a(n,(n+1))/a(n,n)
for k=n-1:-1:1
x(k,:)=(a(k,(n+1))-a(k,(k+1):n)*x((k+1):n))/a(k,k);
end
二、列主元Gauss消去法
function x=line_main_gauss(a,b,flag)
if nargin<3,flag=0;end
n=length(b);
a=[a,b];
for k=1:(n-1)
[am, m]=max(abs(a(k:n,k)));m=m+k-1
if m>k
t=a(k,:);
a(k,:)=a(m,:)
a(m,:)=t
end
a((k+1):n,(k+1):(n+1))=a((k+1):n,(k+1):(n+1))...
-a((k+1):n,k)*a(k,(k+1):(n+1))/a(k,k)
a((k+1):n,k)=zeros(n-k,1);
if flag==0,a,end
end
x=zeros(n,1)
x(n)=a(n,(n+1))/a(n,n)
for k=n-1:-1:1
x(k,:)=(a(k,(n+1))-a(k,(k+1):n)*x((k+1):n))/a(k,k);
end
三、LU分解
function[l,u]=lu(a)
n=length(a)
l=eye(n,n);u=zer