Matlab 中的数值问题:相同的输入,相同的代码 ->不同的输出?

发布于 2024-09-17 05:14:20 字数 542 浏览 4 评论 0原文

当我比较具有相同输入的 Matlab 软件不同运行的结果时,我遇到了问题。为了缩小问题范围,我执行了以下操作:

  • 使用 Matlab 的 save() 方法保存所有相关变量
  • 调用计算结果的方法
  • 使用 save() 再次保存所有相关输出变量

在不更改调用的方法的情况下,我再次运行

  • 加载变量上面保存并使用 isequal() 与当前输入变量进行比较,
  • 使用当前输入变量再次调用我的方法,
  • 加载上面保存的输出变量并进行比较。

我不敢相信最后“行”中的比较发现了细微的差异。计算包括单精度和双精度数,误差在1e-10的量级(输出是双精度数)。

我能想到的唯一可能的解释是,Matlab 在保存变量时会丢失一些精度(我认为这不太可能,我使用默认的二进制 Matlab 格式),或者包含像 a=b+c+d 这样的计算,这可以计算为 a=(b+c)+d 或 a=b+(c+d),这可能会导致数值差异。

您知道上述观察结果的原因可能是什么吗?

多谢!

I am experiencing problems when I compare results from different runs of my Matlab software with the same input. To narrow the problem, I did the following:

  • save all relevant variables using Matlab's save() method
  • call a method which calculates something
  • save all relevant output variables again using save()

Without changing the called method, I did another run with

  • load the variables saved above and compare with the current input variables using isequal()
  • call my method again with the current input variables
  • load the out variables saved above and compare.

I can't believe the comparison in the last "line" detects slight differences. The calculations include single and double precision numbers, the error is in the magnitude of 1e-10 (the output is a double number).

The only possible explanation I could imagine is that either Matlab looses some precision when saving the variables (which I consider very unlikely, I use the default binary Matlab format) or that there are calculations included like a=b+c+d, which can be calculated as a=(b+c)+d or a=b+(c+d) which might lead to numerical differences.

Do you know what might be the reason for the observations described above?

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

以为你会在 2024-09-24 05:14:20

这似乎确实是由计算中的单/双混合引起的。由于我只切换到双精度,所以问题不再出现。感谢大家的想法。

it really seems to be caused by the single/double mix in the calculations. Since I have switched to double precision only, the problem did not occur anymore. Thanks to everybody for your thoughts.

┼── 2024-09-24 05:14:20

这些可能是舍入错误。您可以像这样找到系统的浮点精度:

>> eps('single')

ans =

  1.1921e-07

在我的系统上,此报告为 10^-7,这可以解释您的订单的差异

these could be rounding errors. you can find the floating point accuracy of you system like so:

>> eps('single')

ans =

  1.1921e-07

On my system this reports 10^-7 which would explain discrepancies of your order

冷默言语 2024-09-24 05:14:20

为了确保结果可重现,特别是如果您使用任何随机生成函数(直接或间接),您应该在每次运行开始时恢复相同的状态:

%# save state (do this once)
defaultStream = RandStream.getDefaultStream;
savedState = defaultStream.State;
save rndStream.mat savedState

%# load state (do this at before each run)
load rndStream.mat savedState
defaultStream = RandStream.getDefaultStream();
defaultStream.State = savedState;

To ensure reproducible results, especially if you are using any random generating functions (either directly or indirectly), you should restore the same state at the beginning of each run:

%# save state (do this once)
defaultStream = RandStream.getDefaultStream;
savedState = defaultStream.State;
save rndStream.mat savedState

%# load state (do this at before each run)
load rndStream.mat savedState
defaultStream = RandStream.getDefaultStream();
defaultStream.State = savedState;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文