目录
扫码关注下方微信公众号,回复“矩阵实验”或者“dlut”即可获得所有代码实验结果
2020年所有上机实验代码结果
- 注:实验图片可放大,即可看清楚
2.1
分别利用Gauss消元法和列主消去法编程求解方程组 ,其中
并求出矩阵的分解及列主元的分解(求出),并用分解的方法求的逆矩阵及的行列式
代码
function [x , L,U,Ani] = DelGauss(A,b)
% Gauss 消去法
% 输出解x,L,U,A的逆矩阵
Acopy=A;
n = length(b);
L = zeros(n,n);
for k = 1:n-1
mul = A(k+1:n,k)/A(k,k);
L(k+1:n,k) = mul;
A(k+1:n,k+1:n) = A(k+1:n,k+1:n) - mul * A(k,k+1:n);
b(k+1:n) = b(k+1:n) - mul * b(k);
A(k+1:n,k) = zeros(n-k,1);
end
x = zeros(n,1);
x(n) = b(n)/ A(n,n);
U = A;
for k = n-1:-1:1
x(k) = (b(k) - A(k,k+1:n) * x(k+1:n))/A(k,k);
end
L(logical(eye(size(L))))=1;
Uni=inv(U);
Lni=inv(L);
Ani=Uni*Lni;
function [x,L,U,P] = ZGauss(A,b)
% 列主元Guass 消去法
% 输出解x,L,U,P
n = length(b);
L = zeros(n,n);
P = eye(n,n);
for k=1:n-1
[ap,pos] = max(abs(A(k:n,k)));
pos = pos +k -1;
if pos > k
A([k,pos],:) = A([pos,k],:);
b([k pos],:) = b([pos,k],:);
P([k,pos],:) = P([pos,k],:);
end
mul = A(k+1:n,k) / A(k,k);
L(k+1:n,k) = mul;
A(k+1:n,k+1:n) = A(k+1:n,k+1:n) - mul * A(k ,k+1:n);
b(k+1:n) = b(k+1:n) - mul * b(k);
A(k+1:n,k) = zeros(n-k,1);
end
x = zeros(n,1);
x(n) = b(n) / A(n,n);
U = A;
for k = n-1:-1:1
x(k) = (b(k) - A(k,k+1:n) * x(k+1:n)) / A(k,k);
end
L(logical(eye(size(L))))=1;
结果
高斯消元法结果
列主高斯消元法结果