求矩阵行列式和逆,手写的MATLAB程序 | 您所在的位置:网站首页 › matlab怎么编写矩阵程序 › 求矩阵行列式和逆,手写的MATLAB程序 |
行列式:
%Aij = (-1)^(i+j)*Mij%D = a11*A11 + a12*A12 + a13*A13 + ... + a1n*A1n%H = [1 2 3;7 9 7;5 6 1;];det(H) = 14function H_Det = Matrix_Det(H,N) %输入矩阵 阶数 H_Det = 0;if N==1 H_Det = H(1,1); return;endtemp = zeros(N-1,N-1);for i=1:N for j=2:N %第二行开始 for k=1:N-1 if k>=i cln = k+1; else cln = k; end temp(j-1,k) = H(j,cln); end end t = Matrix_Det(temp,N-1); %递归 if rem(1+i,2)==0 %(-1)^(1+i) H_Det = H_Det+H(1,i)*t; else H_Det = H_Det-H(1,i)*t; endend end 伴随阵:%伴随阵A* = |Aij|(nxn)%Aij = (-1)^(i+j)*Mij%H = [1 2 3;7 9 7;5 6 1;];det(H)*inv(H) = [-33 16 -13;28 -14 14;-3 4 -5];function H_Adjoint = Matrix_Adjoint(H,N)H_Adjoint = zeros(N,N);if N==1% H_Adjoint = [H(2,2) H(2,1);H(1,2) H(1,1)]; H_Adjoint = 1; return;endtemp = zeros(N-1,N-1);for i=1:N %逐行扫描 for j=1:N %逐列扫描 for k=1:N-1 %行组合 if k>=j row = k+1; else row = k; end for t=1:N-1 %列组合 if t>=i cln = t+1; else cln = t; end temp(k,t) = H(row,cln); end end if rem(i+j,2)==0 H_Adjoint(i,j) = Matrix_Det(temp,N-1); %求代数余子式 else H_Adjoint(i,j) = -Matrix_Det(temp,N-1); end endendend 逆:%A^(-1) = 1/|A|*(A*)%H = [1 2 3;7 9 7;5 6 1;];inv(H) = [-2.3571 1.1429 -0.9286;2.0000 -1.0000 1.0000;-0.2143 0.2857 -0.3571];function H_Inv = Matrix_Inverse(H,N)H_Det = Matrix_Det(H,N);H_Adjoint = Matrix_Adjoint(H,N);H_Inv = H_Adjoint/H_Det;end |
CopyRight 2018-2019 实验室设备网 版权所有 |