数值方法 线性方程组的高斯消元法

来源:互联网 发布:购买备案过的域名 编辑:程序博客网 时间:2024/06/05 20:59

function X=uptrbk(A,B)
%Input     -A is an N x N nonsingular matrix
%          -B is an N x 1 matrix
%Output    -X is an N x 1 matrix containing the solution to AX=B
%Initialize X and the temporary storage matrix C
[N N]=size(A);
X = zeros(N,1);
C = zeros(1,N+1);
%Form the augmented matrix : Aug=[A|B]
Aug=[A B];
for p=1:N-1
    % Partial povoting for column p
    [Y,j]=max(abs(Aug(p:N,p)));
    %Interchange row p and j
    C=Aug(p,:);
    Aug(p,:)=Aug(j+p-1,:);
    Aug(j+p-1,:)=Aug(j+p-1,:);
    Aug(j+p-1,:)=C;
 if Aug(p,p)==0
     'A was singular.   No unique solution';
 end
 %Elimination process for column p
 for k=p+1:N
     m=Aug(k,p)/Aug(p,p);
     Aug(k,p:N+1)=Aug(k,p:N+1)-m*Aug(p,p:N+1);
 end
end

U=Aug(1:N,1:N);
Y=Aug(1:N,N+1);
X=backsub(U,Y);    

利用了偏序选主元的策略
原创粉丝点击