求解形式为Ax = B ==>的矩阵错误:矩阵接近奇异或缩放不正确 [英] Solving matrices of the form Ax = B ==> error: Matrix is close to singular or badly scaled

查看:309
本文介绍了求解形式为Ax = B ==>的矩阵错误:矩阵接近奇异或缩放不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解决Ax = B形式的系统

I'm having trouble solving a system of the form Ax=B

系统解决方案应该是

x = inv(A)*B

但是,这不起作用.

尝试上面的代码行时,出现以下错误消息:

I get the following error message when I try the above line of code:

Warning: Matrix is close to singular or badly scaled.
     Results may be inaccurate. RCOND = 1.156482e-018. 

似乎matlab在反转我指定的矩阵时遇到麻烦.我试图通过输入inv(A)* A

It seems that matlab is having trouble inverting the matrix that I've specified. I tried to verify that the inverse function was working properly by typing in inv(A)*A

这应该给出单位矩阵,但是我得到了相同的错误和一些垃圾编号.

This should give the identity matrix, however I got the same error and some garbage numbers.

这是我正在使用的A矩阵:

This is the A matrix that I'm using:

A = [5/2   1/2  -1     0     0    -1/2  -1/2   0     0
     1/2   1/2   0     0     0    -1/2  -1/2   0     0 
    -1     0     5/2  -1/2  -1     0     0    -1/2   1/2
     0     0    -1/2   1/2   0     0     0     1/2  -1/2
     0     0    -1     0     3/2  -1/2   1/2   0     0
    -1/2  -1/2   0     0    -1/2   2     0    -1     0  
    -1/2  -1/2   0     0     1/2   0     1     0     0 
     0     0    -1/2   1/2   0    -1     0     2     0 
     0     0     1/2  -1/2   0     0     0     0     1]

关于为什么这种方法行不通的任何想法?我还尝试将A转换为稀疏矩阵(sparse(A)),然后运行逆命令.没有骰子.

Any ideas as to why this isn't working? I also tried to convert A to a sparse matrix (sparse(A)), and then run the inverse command. No dice.

推荐答案

问题确实出在您的数学上.您提供的矩阵不是全等级的,因此它是不可逆的. 您可以手动验证(没有花时间这样做),但是MATLAB已经通过显示警告来指出这一点.

The problem is indeed in your mathematics. The matrix you provided isn't of full rank, so it isn't invertible. You could verify that manually (haven't taken the time to do so), but MATLAB already points this out by showing that warning.

由于您正在使用浮点数,因此有时会引起其他细微问题,您可以在det(A)的结果中看到其中的一个问题,该结果的顺序为1e-16,即机器精度或实际为0

Since you are working with floating point numbers, this sometimes causes other subtle problems, one of which you can see in the result of det(A), which is in the order of 1e-16, i.e. machine precision or 0 in practice.

通过执行rank函数:rank(A) = 8,可以看到此Matrix的等级不完整.对于9x9矩阵,这确实意味着该矩阵不可加倍(因为rank函数说明了机器精度).

You can see that this Matrix is not of full rank by executing the rank function: rank(A) = 8. For a 9x9 matrix, this indeed means that the matrix is not invertible for doubles (as the rank function accounts for machine precision).

如果要使用MATLAB获得与手动计算相对应的结果,可以使用Symbolic Toolbox及其vpa(可变精度算术)来解决可能的数值问题,但代价是计算速度较慢.

If you want to use MATLAB to get a result that corresponds to a manual calculation, you can use the Symbolic Toolbox and its vpa (variable precision arithmetic) to work around possible numerical problems at the cost of a slower calculation.

B = [5  1 -2  0  0 -1 -1  0  0;
     1  1  0  0  0 -1 -1  0  0;
    -2  0  5 -1 -2  0  0 -1  1;
     0  0 -1  1  0  0  0  1 -1;
     0  0 -2  0  3 -1  1  0  0;
    -1 -1  0  0 -1  4  0 -2  0;
    -1 -1  0  0  1  0  2  0  0;
     0  0 -1  1  0 -2  0  4  0;
     0  0  1 -1  0  0  0  0  2];
A = B/2;
size(A)    % = [9 9]
det(A)     % = -1.38777878078145e-17
rank(A)    % = 8
C = vpa(A);
det(C)     % = 0.0
rank(C)    % = 8

同时使用VPA和浮点,您将得到等级为8,大小为[9 9],行列式实际上为0,即单数或不可逆.更改一些条目可能会使您的矩阵规则(非奇数),但是不能保证它能正常工作,并且可以解决另一个问题.

Both with VPA and floating points you will get that the rank is 8, the size is [9 9] and the determinant is practically 0, i.e. singular or not invertible. Changing a few entries might make your matrix regular (non-singular), but it is not guaranteed to work and it will solve a different problem.

要解决x的实际问题A*x=b,可以尝试使用mldivide(又称反斜杠运算符)或Moore-Penrose伪逆:

To solve your actual problem A*x=b for x, you can try to use mldivide (a.k.a. the backslash operator) or a Moore-Penrose pseudo-inverse:

x1 = A\b;
x2 = pinv(A)*b;

但是请记住,这样的系统没有唯一的解决方案,因此伪逆运算符和反斜杠运算符都可能(在这种情况下)将返回非常不同的解决方案,它们中的任何一个是否可以接受取决于您的实际情况.应用程序.

But do remember that such a system does not have a unique solution, so both the pseudo-inverse and the backslash operator may (and in this case will) return very different solutions, whether any of them is acceptable really depends on your application.

这篇关于求解形式为Ax = B ==>的矩阵错误:矩阵接近奇异或缩放不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆