下面是运用MATLAB写的一个代码,可用来求解线性方程组。
function x=ch2_gauss(A,b)
n1=size(A,1);n2=size(A,2);n3=length(b);
if(n1~=n2)
disp("A is not a squarenmatrix");
return;
end
if(n2~=n3)
disp("dimension of A and b is not equal");
return
end
n=n1;
L=zeros(n);
for k=1:n-1
if A(k,k)~=0
for i=k+1:n
L(i,k)=A(i,k)/A(k,k);
for j=k:n
A(i,j)=A(i,j)-L(i,k)*A(k,j);
end
b(i)=b(i)-L(i,k)*b(k);
end
end
k
[A';b']'
end
L
x(n)=b(n)/A(n,n);
for k=n-1:-1:1
sum=0
for i=k+1:n
sum=sum+A(k,i)*x(i);
end
x(k)=(b(k)-sum)/A(k,k);
end
x
标签:disp,end,求解,sum,线性方程组,n1,n2,size From: https://blog.51cto.com/u_16271069/7694457