为什么 MATLAB 中不满足逆等式?
MATLAB 不满足逆矩阵运算,即;
(ABC)-1 = C-1 * B-1 * A-1
在 MATLAB 中,
if inv(A*B*C) == inv(C)*inv(B)*inv(A)
disp('satisfied')
end
它不符合条件。当我制作成format long
时,我意识到有点差异,但当我制作成formatrat
时,它甚至不满足。
为什么会这样呢?
MATLAB does not satisfy matrix arithmetic for inverse, that is;
(ABC)-1 = C-1 * B-1 * A-1
in MATLAB,
if inv(A*B*C) == inv(C)*inv(B)*inv(A)
disp('satisfied')
end
It does not qualify. When I made it format long
, I realized that there is difference in points, but it even does not satisfy when I make it format rat
.
Why is that so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很可能是浮点错误。请注意,
format
函数仅影响数字的显示方式,不是 MATLAB 计算或保存它们的方式。 因此将其设置为rat
不会减少不准确的情况。我还没有测试过,但您可以尝试 分数工具箱精确的有理数算术,应该与上面相等。
Very likely a floating point error. Note that the
format
function affects only how numbers display, not how MATLAB computes or saves them. So setting it torat
won't help the inaccuracy.I haven't tested, but you may try the Fractions Toolbox for exact rational number arithmetics, which should give an equality to above.
考虑这个 (MATLAB R2011a):
当 MATLAB 计算中间量
inv(a)
或a*a
(无论a
是标量还是一个矩阵),它默认将它们存储为最接近的双精度浮点数 - 这是不精确的。所以当这些稍微不准确的中间结果用于后续计算时,就会出现舍入误差。与其比较浮点数是否直接相等,例如 inv(A*B*C) == inv(C)*inv(B)*inv(A),通常最好比较与阈值的绝对差,例如abs(inv(A*B*C) - inv(C)*inv(B)*inv(A)) <脱粒。这里的 thresh 可以是任意小数,也可以是涉及 eps 的某种表达式,它可以为您提供在您工作的精度下两个数字之间的最小差异。
format
命令仅控制结果在命令行的显示,而不控制结果内部存储的方式。特别是,formatrat
不会使 MATLAB 以符号方式进行计算。为此,您可以查看符号数学工具箱。对于诊断浮点精度问题(例如您遇到的问题),format hex
通常比format long
更有用。Consider this (MATLAB R2011a):
When MATLAB calculates the intermediate quantities
inv(a)
, ora*a
(whethera
is a scalar or a matrix), it by default stores them as the closest double precision floating point number - which is not exact. So when these slightly inaccurate intermediate results are used in subsequent calculations, there will be round off error.Instead of comparing floating point numbers for direct equality, such as
inv(A*B*C) == inv(C)*inv(B)*inv(A)
, it's often better to compare the absolute difference to a threshold, such asabs(inv(A*B*C) - inv(C)*inv(B)*inv(A)) < thresh
. Herethresh
can be an arbitrary small number, or some expression involvingeps
, which gives you the smallest difference between two numbers at the precision at which you're working.The
format
command only controls the display of results at the command line, not the way in which results are internally stored. In particular,format rat
does not make MATLAB do calculations symbolically. For this, you might take a look at the Symbolic Math Toolbox.format hex
is often even more useful thanformat long
for diagnosing floating point precision issues such as the one you've come across.