线性方程组求根的数值方法有直接法和迭代法,直接法常用于处理阶数较小的方程组。其中, 列主元高斯法比较简单,但是实现成代码需要考虑很多细节,matlab的调试过程需要很大的耐心,下面贴出matlab程序。
%Gaussian elimination method to find root of linear equations
function x=Gaussian_elimination(A,b) %b is a column vector
[n,~]= size(A);
x(1:n,1) = 0;
if abs(det(A))<=1e-8
error('The matrix is a singular matrix');
return ;
end
%elimination
for k=1:n
ak = max( abs( A(k:n,k) ) );
index = find( abs(A(:,k))==ak ); %index is a column vector
%main element
temp = A(k,:); %temp is a row vector
A(k,:) = A(index(1),:);
A(index(1),:) = temp;
temp_b = b(k);
b(k) = b(index(1));
b(index(1)) = temp_b;
%clear up column elements
if A(k,k)~=0
for i=k+1:n
if A(i,k)~=0
m&#