📄 gauss.m
字号:
function sol=gauss(a,b)
% File name:gauss.m
% Author:Yan Anxin
% ID number:081810
% Date:finished on 2008.10.29
% Description:
% Here defines a function,applying Gaussian main-element elimination to solve
% linear equations by the standard form Ax=b.
% In the syntax sol=gauss(a,b),
% a----input,matrix of coefficient
% b----input,right vector
% sol----output,returns the solution of linear equation
siz=size(a); %gets the dimension of the coefficient matrix
n=siz(1);
a=[a b]; %forms augmented matrix
for i=1:n
maxyuan=0; %the largest element of the column vector
maxyuanabs=0; %absolute value of maxyuan
for k=i:n
if(abs(a(k,i))>maxyuanabs) %to find out the main-element and its column
maxyuanabs=abs(a(k,i));
maxyuan=a(k,i);
maxn=k;
end
end
%exits the loop if there is no nonzero column element,or exchanges rows
if(maxyuan==0) ,continue,end
change=a; %exchanges rows
for k=i:(n+1)
change(i,k)=a(maxn,k);
change(maxn,k)=a(i,k);
a=change;
end
for p=(i+1):n %eliminate elements,forming triangular matrix
L=a(p,i)/a(i,i);
for t=i:(n+1)
a(p,t)=a(p,t)-a(i,t)*L;
end
end
end
x(n)=a(n,n+1)/a(n,n); %back substitutes to solve the equations set
for i=1:(n-1)
s=0;
for j=((n-i)+1):n
s=s+a(n-i,j)*x(j);
end
x(n-i)=(a(n-i,n+1)-s)/a(n-i,n-i);
end
sol=x';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -