📄 gausselimpartpivot.m
字号:
%Gaussian Elimination-Partial Pivoting
%Description: This procedure solves a system of linear equations using
%Gaussian Elimination Algorithm with partial pivoting strategy. Partial
%pivoting is the interchanging of rows in order to place a particularly good
%element(usually the largest) in the diagonal position.
%Input: the augmented matrix of the system [A b]
%Output: solution of the system and also the debug information if
%necessary.
%Note: Compared with Gaussian Elimination Algorithm, this procedure is not
%sensitive to zero pivot element of the matrix and could always return the
%solution, but it is also numerically unstable, sometimes the solution
%might be wrong.
%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=[1 1e12;1 2]
% b=[1e12 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
%pivoting
[Amax,t]=max(abs(A(i:N,i)));
T=A(i+t-1,:);
A(i+t-1,:)=A(i,:);
A(i,:)=T;
T=b(i+t-1);
b(i+t-1)=b(i);
b(i)=T;
%end pivoting
disp(sprintf('after %d pivoting,\n [A b]=',i));
disp([A b]);
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 + -