📄 gausselim.m
字号:
%Gaussian Elimination
%Description: This procedure solves a system of linear equations using
%Gaussian Elimination Algorithm. Elementary row operations are used
%throughout this algorithm, which consists of two parts. The first reduces
%the matrix to row echelon form or upper triangular form while the second
%uses backward sustitution to solve the equation.
%Input: the augmented matrix of the system [A b]
%Output: solution of the system and also the debug information if
%necessary.
%Note: This procedure is numerically unstable. Besides, zero pivot
%element of the matrix could cause this procedure to terminate without
%returning the correct solution.
%This program is written by Xiaoke Yang @ School of Automation Science and
%Electrical Engineering, Beihang University.You can copy, modify or
%redistribute it freely.Welcome reports of bugs, suggestions, etc.
%yxkmlstar@gmail.com
%Last modified by Xiaoke Yang, Oct.10,2007
% A=[2 -1 1;-3 2 -1;-2 2 1];
% b=[8 -11 -3]';
% A=[1e-12 1;1 2]
% b=[1 3]'
A=[0.012 0.01 0.167;1 0.8334 5.91;3200 1200 4.2]
b=[0.6781 12.1 981]'
N=length(b)
%forward elimination
for i=1:N-1
if(abs(A(i,i))<1e-15)
disp(sprintf('A(%d,%d)=0, program terminated!\n',i,i))
return;
end
m=-1/A(i,i);
for j=i+1:N %j indicating the row
for k=i+1:N %k indicating the column
A(j,k)=A(j,k)+A(j,i)*m*A(i,k);
end
b(j)=b(j)+A(j,i)*m*b(i);
A(j,i)=0;
end
disp(sprintf('after %d elimination, \n[A b]=',i))
disp([A b])
end
%backword substitution
x(N)=b(N)/A(N,N);
for i=N-1:-1:1
s=0;
for j=i+1:N;
s=s+x(j)*A(i,j);
end
x(i)=(b(i)-s)/A(i,i);
end
disp(sprintf('The solution to this system is \nx='));
disp(x);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -