# Question about accuracy of two different methods

1 ビュー (過去 30 日間)
Devin 2018 年 11 月 14 日
コメント済み: Devin 2018 年 11 月 15 日
Greetings,
I have a question about accuracy. I have mathmatically equivalent method 1 and method 2. Practically, the final diffference beteween vol and vol1 is 8.8818e-15. In my program, this tiny difference can be amplified. But I still need to use method 1 form to get results as method 2.
%Method 1:
vol = 6
for i = 1:5000
vol = vol + 18.3 * 1e-3 * 0.1;
end
%Method 2:
vol1 = 6 + 18.3 * 1e-3 * 0.1 * 5000;
Does anyone can help me to explain why these two methods are different? And how can I get accurate results to avoid this tiny difference to be amplified?

サインインしてコメントする。

### 採用された回答

Stephen 2018 年 11 月 14 日
Method 1 has 5000 operations, each of which is bound to generate some error. Your machine epsilon may vary from mine, but mine is 2.2204 e-16 on a 64 bit installation of Matlab. (Type "eps" on your command line) Every Matlab operation is rounded to the nearest eps, so if you have 5000 operations, that is 5000 opportunities to accumulate error.
If you are extraordinarily unlucky, and each of those rounding errors went in the same direction, you might see as much as 5.5511e-13 in error accumulation. (max error = 5000*eps/2). So in reality, your accumulation of 8.88e-15 in error over 5000 operations is really not all that bad, as the errors seem to be distributed near the desired mean of zero. If your system is sensitive to that level of error, I would suggest you reconsider the reasons why you "must" use method one.
##### 3 件のコメント表示非表示 2 件の古いコメント
Devin 2018 年 11 月 15 日
Thank you very much, I will discuss with my prof.

サインインしてコメントする。

R2018a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by