Matlab实现——求矩阵的逆(LU分解)

欢迎前往个人博客 驽马点滴 和视频空间 哔哩哔哩-《挨踢日志》

Program  ( Solve By Factorization with Pivoting )

思路及原理:

就得到:

程序:

function  X=Ni(A)
%Input  - A is an N x N matrix
%Output - I is an N x N inverse matrix of A 
%and I(j,:)containing the solution to AX(:,j) =E(:,j).
%Initialize X, Y,the temporary storage matrix C, and the row 
% permutation information matrix R
[N,N]=size(A);
B=eye(N);   %B is an N x N identity matrix
X=zeros(N,N);
Y=zeros(N,N);
C=zeros(1,N);
R=1:N;
%the next steps is to find the factorization(factorize for only once)
for p=1:N-1
%Find the pivot row for column p
   [max1, j]=max(abs(A(p:N,p)));
%Interchange row p and j
      C=A(p,:);
      A(p,:)=A(j+p-1,:);
      A(j+p-1,:)=C;
      d=R(p);
      R(p)=R(j+p-1);
      R(j+p-1)=d;
      if A(p,p)==0
      'A is singular.  No unique solution'
      break
      end
   %Calculate multiplier and place in subdiagonal portion of A
      for k=p+1:N
         mult=A(k,p)/A(p,p);
     A(k,p) = mult;
         A(k,p+1:N)=A(k,p+1:N)-mult*A(p,p+1:N);
      end
end
for j=1:N    
    %when j is fixed then the method is similar to the Program 3.3
    %Solve for Y(:,j)
    Y(1,j) = B(R(1),j);
    for k=2:N
        Y(k,j)= B(R(k),j)-A(k,1:k-1)*Y(1:k-1,j);
    end
    %Solve for X(:,j)
    X(N,j)=Y(N,j)/A(N,N);
    for k=N-1:-1:1
        X(k,j)=(Y(k,j)-A(k,k+1:N)*X(k+1:N,j))/A(k,k);
    end
end


 

如果运行程序可以看到:Ni(A)和inv(A)运算得到的逆矩阵是相同的

而且 A*Ni(A)=E 所以结果是令人满意的

此方法中LU非直接三角分解只用了一次,通过增加一个j的循环,实现方程组的逐个求解,将得到的N个解向量C(:,j)合到X中得到最终结果。

个人感觉此方法的稳定性不错,暂时不需要改进了吧。

设计这个程序的时候并没遇到什么困难,思路已经想好了:

求解N个方程AXj=Ej;只要通过两个矩阵的对应列向量来存储Xj和Ej以及增加一个j循环,然后利用原来的Program就能达到预期的目的。

 

欢迎前往个人博客 驽马点滴 和视频空间 哔哩哔哩-《挨踢日志》

  • 12
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值