function [x,XA]= Gauss (A,b)
N = size(A);
n = N(1);
index = 0;
pos = zeros(n,1);
B = A;
for i=1:n
me = max(abs(B(1:n,i))); %选取列主元
for k=1:n
if(abs(A(k,i))==me)
index = k;
pos(i,1) = k; %保存列主元所在的行号
break;
end
end
m = A(index,i);
for j=1:n
if(j ~= index)
l = A(j,i);
A(j,1:n)=A(j,1:n)-l*A(index,1:n)/m;
b(j)=b(j)-l*b(index)/m; %消元
end
end
B = A;
for k=1:n
if(pos(k,1)~=0)
B(pos(k,1),1:n)=0; %避免列主元在同一行
end
end
end
XA = A;
for i=1:n
x(i,1)=b(pos(i,1))/A(pos(i,1),i); %求解
end
测试用例:
>> A=magic(3);x=ones(3,1);b=A*x
b =
15
15
15
>> xx=Gauss(A,b)
xx =
1
1
1
>> xx-x
ans =
0
0
0
>>
);
for j=(k+1);
if m>
nb=size(b;
end
end
end
if A(n:(n-1)
for i=(k+1),n);double',k)*A(k;该矩阵所对应的方程组无解')
elseif m<:nb
for j1=(i+1);); for input arguments of type ',j)/,k):nb
b(m;n
printf('Guass',j)=b(i;
mb=size(b,b)
m=size(A;A(i;
end
这个程序使用时出现Undefined function ':n
for j=1:n-1
if A(k;
for j=1;该矩阵所对应的方程组有无穷多解',无法进行回代过程'
end
end
end
x=b,j)*A(i;主元素为零',
break,j):(n-1)
i=n-i;
end
end
end
end
end
end
for k=1,k);
else
for i=(k+1);
n=size(A;A(k,i),k)=A(i,j)-b(k,j)/,j)=b(i,k)/:
function x=Gauss(A,2),j)=b(i,j)-A(i:n
A(i;
end
b(i,n)==0
printf(',j).怎么解决;该矩阵为奇异矩阵;A(n;
else for j=1,j)-A(i,j)=b(m,1):n
b(i;
end
for i=1:nb
b(i,1);n
printf(',k)==0
printf(',j)=A(i:n
A(i;)
else
if m==mb
for k=1,2)