<FONT style="FONT-SIZE: 14px" color=#555576><FONT style="FONT-SIZE: 14px; LINE-HEIGHT: 17pt" color=#555576>function x=nagauss2(a,b,flag)<BR>% 用途:选列主元Gauss消去法解线性方程组ax=b<BR>% 格式:x=nagauss2(a,b,flag) a为系数矩阵,b为右端列向量,flag若为0,则显示中间过程<BR>% 否则不显示,默认值为0,x为解向量<BR>if nargin<3,flag=0;end<BR>n=length(b);a=[a,b];<BR>for k=1:(n-1)<BR>% 选主元<BR>[ap,p]=max(abs(a(k:n,k)));p=p+k-1;<BR>if p>k,<BR> t=a(k,:);a(k,:)=a(p,:);a(p,:)=t;<BR>end<BR>% 消元<BR>a((k+1:n,(k+1):(k+1):(n+1))=a((k+1):n,(k+1):n,(k+1):(n+1))-a((k+1):n,k)/a(k,k)*a(k,(k+1):(n+1));<BR>a((k+1):n,k)=zeros(n-k,1);<BR>if flag==0,a,end<BR>end <BR>% 回代<BR>x=zeros(n,1);<BR>x(n)=a(n,n+1)/a(n,n);<BR>for k=n-1:-1:1<BR> x(k,:)=(a(k,n+1)-a(k,(k+1):n)*x((k+1:n))/a(k,k);<BR>end <BR></FONT></FONT> |