1、Matlab线性方程组求解1. Gauss消元法:function x=DelGauss(a,b)% Gauss消去法n,m=size(a);nb=length(b);det=1; %存储行列式值x=zeros(n,1);for k=1:n-1 for i=k+1:n if a(k,k)=0 return end m=a(i,k)/a(k,k); for j=k+1:n a(i,j)=a(i,j)-m*a(k,j); end b(i)=b(i)-m*b(k); end det=det*a(k,k); %计算行列式enddet=det*a(n,n);for k=n:-1:1 %回代求解 for
2、j=k+1:n b(k)=b(k)-a(k,j)*x(j); end x(k)=b(k)/a(k,k);endExample: A=1.0170 -0.0092 0.0095;-0.0092 0.9903 0.0136;0.0095 0.0136 0.9898; b=1 0 1; x=DelGauss(A,b)x = 0.9739 -0.0047 1.00102. 列主元Gauss消去法:function x=detGauss(a,b)% Gauss列主元消去法n,m=size(a);nb=length(b);det=1; %存储行列式值x=zeros(n,1);for k=1:n-1 ama
3、x=0; %选主元 for i=k:n if abs(a(i,k)amax amax=abs(a(i,k);r=i; end end if amaxk %交换两行 for j=k:n z=a(k,j);a(k,j)=a(r,j);a(r,j)=z; end z=b(k);b(k)=b(r);b(r)=z;det=-det; end for i=k+1:n %进行消元 m=a(i,k)/a(k,k); for j=k+1:n a(i,j)=a(i,j)-m*a(k,j); end b(i)=b(i)-m*b(k); end det=det*a(k,k);enddet=det*a(n,n);for k=n:-1:1 %回代求解 for j=k+1:n b(k)=b(k)-a(k,j)*x(j); end x(k)=b(k)/a(k,k);endExample: x=detGauss(A,b)x = 0.9739 -0.00471.0010