本程序实现了四种QR分解的方法,分别是 Gram-Schmidt、modified Gram-Schmidt、Householder reduction 和 Givens reduction。要求输入矩阵列向量无关, 分解所得矩阵 A = Q R A=QR A=QR, 其中Q为正交方阵,R为上三角矩阵。
QR分解-古典施密特正交法
QR分解-改进施密特正交法
QR分解-Householder分解
QR分解-Givens分解
URV分解
A = U R V T A=URV^T A=URVT,其中, A A A为 m ∗ n m*n m∗n矩阵, U U U和 V V V分别为 m m m阶和 n n n阶正交矩阵。
U U U的前 r r r列是 R ( A ) R(A) R(A)的标准正交基,后 m − r m-r m−r列是 N ( A T ) N(A^T) N(AT)的标准正交基。
V V V的前 r r r列是 R ( A T ) R(A^T) R(AT)的标准正交基,后 m − r m-r m−r列是 N ( A ) N(A) N(A)的标准正交基.
求行列式
求解向量
求解向量的有三种情况,即无解、有唯一解、有无穷多解。
本程序在QR分解的基础上:
输入:
调用:
输入:
调用:
输入:
调用:
% % LU
% % QR(Gram-Schmidt)
%Orthogonal Reduction
% % Householder Reduction
% % Givens reduction)
% % URV
% %程序实现,要求如下:
%
% 1、一个综合程序,根据选择参数的不同,实现不同的矩阵分解;在此基础上,实现Ax=b方程组的求解,以及计算A的行列式;
%
% 2、可以用matlab、Python等编写程序,需附上简单的程序说明,比如参数代表什么意思,输入什么,输出什么等等,附上相应的例子;
%
% 3、一定是可执行文件,例如 .m文件等,不能是word或者txt文档。附上源代码,不能为直接调用matlab等函数库;
function varargout = matrixAnalysis(A,method)
A
[m,n] = size(A);
if isnumeric(method)
b = method
[~,Q1,R1] = Hous(A);
[~,~,R2] = Hous([A b]);
R1 = roundn(R1,-10);
R2 = roundn(R2,-10);
R1(all(R1==0,2),:) = [];
R2(all(R2==0,2),:) = [];
if(size(R1,1)~=size(R2,1))
fprintf("方程无解");
varargout{1} = [];
return;
elseif(size(R1,1)==size(R2,1)&&size(R2,1)==n)
R1contrary = contraryTra(R1);
fprintf("·······方程有唯一解为:\n");
x = R1contrary*Q1'*b;
varargout{1} = x;
else
%
R3= [A b];
P1 = eye(m);
for i=1:m-1
[~,currentMaxIndex] = max(abs(R3(i:m,i)));%寻找主元
P1([i currentMaxIndex+i-1],:)=P1([currentMaxIndex+i-1 i],:);%主元行变换
R3([i currentMaxIndex+i-1],:)=R3([currentMaxIndex+i-1 i],:);%主元行变换
for j=i+1:m
k = (R3(j,i)/R3(i,i));
R3(j,:) = R3(j,:)-k*R3(i,:);
P1(j,:) = P1(j,:)-k*P1(i,:);
end
end
R3;
% U
R3(all(abs(R3)<=1e-10,2),:) = [];
[m1,~] = size(R3);
[~,Ind]=max(R3~=0 , [] , 2);%z主元位置
for i=1:m1
R3(i,:) = R3(i,:)/R3(i,Ind(i));
for j=1:i-1
k = (R3(j,Ind(i))/R3(i,Ind(i)));
R3(j,:) = R3(j,:) - R3(i,:)*k;
P1(j,:) = P1(j,:) - P1(i,:)*k;
end
end
R3;
%U = [A(:,Ind) P1(m1+1:m,:)'];
% R
funSol = zeros(n,n-m1);
a=1:n;
[c i]=setdiff(a,Ind);
t=a(sort(i));
funSol(1:m1,:) = -R3(:,t);
fprintf("·······方程有无穷多解,基础解系为:\n");
funSol(m1+1:n,:) = eye(n-length(Ind))
fprintf("·······方程有无穷多解,其特解为:\n");
praSol = [R3(:,m+1);zeros(1,n-size(R2,1))]
x = [funSol praSol];
varargout{1} = x;
end
else
switch method
case "LU"
[~,P,Q,R] = LU(A);
varargout{1} = P;
varargout{2} = Q;
varargout{3} = R;
case "QR-古典型施密特"
[P,Q,R] = smiClass(A);
varargout{1} = Q;
varargout{2} = R;
case "QR-改进型施密特"
[P,Q,R] = smiImpr(A);
varargout{1} = Q;
varargout{2} = R;
case "GivensReduction"
[P,Q,R] = Givens(A);
varargout{1} = Q;
varargout{2} = R;
case "HouseholderReduction"
[P,Q,R] = Hous(A);
varargout{1} = Q;
varargout{2} = R;
case "URV1"
[U,V,R] = URV1(A);
varargout{1} = U;
varargout{2} = V;
varargout{3} = R;
case 'URV2'
[U,V,R] = URV2(A);
varargout{1} = U;
varargout{2} = V;
varargout{3} = R;
case 'det'
[P,Q,R,Det] = ourDet(A);
varargout{1} = Det;
end
end
end
function [count,P,Q,R] = LU(A)
[m,n] = size(A);
if m~=n
error("······您输入的不是方阵,无法进行LU分解或行列式计算·······");
end
count=0;
R = A;
P = eye(n);
Q = eye(n);
for i=1:n-1
a =eye(n);%初始化P0矩阵
b = a;%初始化M矩阵
[~,currentMaxIndex] = max(abs(R(i:n,i)));%寻找主元
if((currentMaxIndex+i-1)~=i)
count=count+1;
end
a([i currentMaxIndex+i-1],i:n)=a([currentMaxIndex+i-1 i],i:n);%主元行变换
%逐步计算 P M
P0{i} = a;
R = P0{i}*R;
M{i} = b;
for j=i+1:n
M{i}(j,i) = -(R(j,i)/R(i,i));
end
R = M{i}*R;%迭代变换
P = P0{i}*P;
Q = P0{i}*Q/(P0{i})*inv(M{i});
end
% P = roundn(P,-3);
% Q = roundn(Q,-3);
% R = roundn(R,-3);
end
function [P,Q,R] = smiClass(A)
[m,n] = size(A);
if m