% Column principal element elimination solution Equation Group ax=b, realize pa=lu
function [X,deta] =gauss (A, B)
N=length (b); [P,q]=size (A);
If p~=q| | P~=n fprintf (' the dimension of the square is different, please lose again! '); % Error detection
End
% to increase the operating speed, give the initial value to L,U,X,C,D1
L=zeros (N,n);
U=zeros (N,n);
X=zeros (n,1);
C=zeros (1,n);
d1=0;
% Select the main element by column, and line Exchange, record line information
For i=1:n-1
Max=abs (A (i,i));
M=i;
For J=i+1:n
If Max<abs (A (j,i))
Max=abs (A (j,i));
M=j;
End
End
if (m~=i)
For K=i:n
C (k) =a (i,k);
A (i,k) =a (m,k);
A (M,k) =c (k);
End
D1=b (i);
B (i) =b (m);
B (m) =d1;
End
% to calculate the elimination of the element
For k=i+1:m
For J=i+1:n
A (k,j) =a (i,j)-A (i,j) *a (i,i);
End
B (k) =b (k)-B (i) *a (k,i)/A (i,i);
A (K,i) = 0;
End
% back-generation solution
X (n) =b (n)/A (n,n);
For I=n-1:-1:1
sum=0.0;
For J=i+1:n
Sum=sum+a (I,j) *x (j);
End
X (i) = (b (i)-sum)/A (i,i);
End
% calculates the value of the determinant
Deta=1;
For K=1:n
Deta=deta*a (K,K);
End
% output pa=lu information in the l,u
For I=1:n
For J=1:n
If I<j
U (i,j) =a (I,J);
ElseIf i==j
L (I,J) = 1;
U (i,j) =a (K,J);
Else
L (i,j) =a (I,J);
End
End
End
End
Solving the problem Equation Group (script file):
A=[-0.002 2 2
1 0.78125 0
3.996 5.5625 4];
B=[0.4;1.3816;7.4178];
%[l,u,x,deta]=mylu (A, B)
[X,deta]=gauss (A, B)
DISP ( the solution obtained by the Gaussian elimination method of the column principal element is:')
Six Operation Result:
x =
1.8167
0.0527
-0.0337
Deta =
798.0666
Gauss column principal element elimination function