guass method

function   [A,b,x]=gauss(A,b)

%Input:  A is the coffecient matrix
%     :  b is the number at the right of equation

%Output: A is the matrix after gauss 
%      : b is the vector after gauss
%      : x is the solution of the linear equation
n=size(A,2);%the demesion of equation

for k=1:n-1
    if A(k,k)~=0
        for i=k+1:n 
            m(i,k)=A(i,k)/A(k,k);%the number a_{ik}/a_{kk}

           for j=1:n 

                A(i,j)=A(i,j)-A(k,j)*m(i,k);

            end
                b(i)=b(i)-b(k)*m(i,k);

        end

    end 
end
x=zeros(n,1);
x(n)=b(n)/A(n,n);
for k=n-1:-1:1
    x(k)=(b(k)-(A(k,k+1:n)*x(k+1:n)))/A(k,k);
end

end

你可能感兴趣的:(数值分析)