计算逆矩阵需要注意的问题

计算逆矩阵需要注意的问题:

方阵:

A = rand(5)

A =

    0.5492    0.6463    0.8491    0.6685    0.6669
    0.7284    0.5212    0.3725    0.2068    0.9337
    0.5768    0.3723    0.5932    0.6539    0.8110
    0.0259    0.9371    0.8726    0.0721    0.4845
    0.4465    0.8295    0.9335    0.4067    0.7567

B = rand(1,5)

B = B'

B =

    0.5583
    0.5989
    0.1489
    0.8997
    0.4504


方阵计算方法1:

C = A\B

C =

   -0.9242
    5.7732
   -5.1228
    2.9991
   -0.4805


方阵计算方法2:

C1 = inv(A)*B

C1 =

   -0.9242
    5.7732
   -5.1228
    2.9991
   -0.4805


方阵计算方法3:

C2 = (inv(A'*A)*A')*B

C2 =

   -0.9242
    5.7732
   -5.1228
    2.9991
   -0.4805


方阵计算方法4:

C3 = (A'*inv(A*A'))*B

C3 =

   -0.9242
    5.7732
   -5.1228
    2.9991
   -0.4805


方阵计算方法5:

[P,Q]=mypq(A);

C4 = P'*inv(P*P')*inv(Q'*Q)*Q'*B

C4 =

   -0.9242
    5.7732
   -5.1228
    2.9991
   -0.4805


简单非方阵:

A = rand(5,4)

A =

    0.6575    0.1343    0.3017    0.6311
    0.9509    0.0605    0.0117    0.8593
    0.7223    0.0842    0.5399    0.9742
    0.4001    0.1639    0.0954    0.5708
    0.8319    0.3242    0.1465    0.9969


简单非方阵计算方法1:

C = A\B

C =

    0.3940
    1.0239
   -0.5243
    0.1851


简单非方阵计算方法2:

C1 = inv(A)*B
Error using inv
Matrix must be square.


简单非方阵计算方法3:

C2 = (inv(A'*A)*A')*B

C2 =

    0.3940
    1.0239
   -0.5243
    0.1851


简单非方阵计算方法4:

C3 = (A'*inv(A*A'))*B
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND =  2.039534e-18.

C3 =

   -4.0888
  -14.5982
   -6.9755
  -36.5726


简单非方阵计算方法5:

C4 = P'*inv(P*P')*inv(Q'*Q)*Q'*B
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND =  2.039534e-18.
Error using  *
Inner matrix dimensions must agree.


复杂非方阵:

x = linspace(-3,3,n)';

A(:,1) = ones(n,1);
for j=1:15
    A(:,2*j) = sin(j/2*x);p(:,2*j+1) = cos(j/2*x);
end

B = rand(50,1);


复杂非方阵计算方法1:

C = A\B

C =

   1.0e+10 *

   -3.9512
    0.0613
    7.5360
   -0.1063
   -6.5327
    0.1255
    5.1400


复杂非方阵计算方法2:

C1 = inv(A)*B
Error using inv
Matrix must be square.


复杂非方阵计算方法3:

C2 = (inv(A'*A)*A')*B
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND =  3.971633e-18.

C2 =

   1.0e+13 *

   -0.0000
   -6.5291
   -5.2530
    1.8172
    1.6742
   -3.1518
    2.0103


复杂非方阵计算方法4:

C3 = (A'*inv(A*A'))*B
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND =  6.046658e-19.

C3 =

   1.0e+14 *

    1.2922
   -0.4676
    0.7775
   -0.1933
   -0.1726
    0.6507
   -0.4371


复杂非方阵计算方法5:

C4 = P'*inv(P*P')*inv(Q'*Q)*Q'*B
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND =  6.046658e-19.
Error using  *
Inner matrix dimensions must agree.


问题:

对于简单非方阵只有方法1和3可用,对于复杂非方阵只有方法1可用!

将以上方法中的inv替换成pinv就正确了,matlab中的pinv函数的实现是用svd实现的:

[U,S,V] = svd(A,'econ');
s = diag(S);
if nargin < 2
    tol = max(size(A)) * eps(norm(s,inf));
end
r1 = sum(s > tol)+1;
V(:,r1:end) = [];
U(:,r1:end) = [];
s(r1:end) = [];
s = 1./s(:);
X = bsxfun(@times,V,s.')*U';

现在还不明白为什么svd可以,而以上方法不可以?




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值