为什么 MATLAB 中不满足逆等式?

发布于 2024-12-12 21:23:07 字数 296 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海的爱人是光 2024-12-19 21:23:07

很可能是浮点错误。请注意,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 to rat 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.

月朦胧 2024-12-19 21:23:07

考虑这个 (MATLAB R2011a):

a = 1e10;
>> b = inv(a)*inv(a)
b =
  1.0000e-020
>> c = inv(a*a)
c =
  1.0000e-020
>> b==c
ans =
     0
>> format hex
>> b
b =
   3bc79ca10c924224
>> c
c =
   3bc79ca10c924223

当 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):

a = 1e10;
>> b = inv(a)*inv(a)
b =
  1.0000e-020
>> c = inv(a*a)
c =
  1.0000e-020
>> b==c
ans =
     0
>> format hex
>> b
b =
   3bc79ca10c924224
>> c
c =
   3bc79ca10c924223

When MATLAB calculates the intermediate quantities inv(a), or a*a (whether a 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 as abs(inv(A*B*C) - inv(C)*inv(B)*inv(A)) < thresh. Here thresh can be an arbitrary small number, or some expression involving eps, 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 than format long for diagnosing floating point precision issues such as the one you've come across.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文