Matlab returns different values with two equivalent functions
1 回表示 (過去 30 日間)
古いコメントを表示
Marcos Baños
2020 年 3 月 8 日
コメント済み: Marcos Baños
2020 年 3 月 10 日
Let's say I have two functions:
f = @(x) (1.01.*exp(4.*x) - 4.62.*exp(3.*x) - 3.11.*exp(2.*x) + 12.2.*exp(x) - 1.99)
F = @(x) (((1.01.*exp(x) - 4.62).*exp(x)-3.11).*exp(x) + 12.2).*exp(x) - 1.99
These two functions are equivalent but when I want to execute with MATLAB it gives me different results with the same input. (for example with 0.925)
I would like to ask why cause this situation and which one has should I trust.
Thanks in advance!
2 件のコメント
採用された回答
Thiago Henrique Gomes Lobato
2020 年 3 月 8 日
編集済み: Thiago Henrique Gomes Lobato
2020 年 3 月 8 日
The difference between then in my computer with your example is -3.197442310920451e-14 (depending of your processor may be slightely different but still extremely low). This difference is negletible and can be regarded as numerical/approximation error between taking the exp of a number and multiplying two (to four, in your case) exp's. You can reproduce it easily with an easier example:
exp(3.245*2)-exp(3.245)*exp(3.245)
ans =
-1.136868377216160e-13
The results are then actually the same and you can trust both of them with the given accuracy, which should be enough for almost any application you may think.
6 件のコメント
Stephen23
2020 年 3 月 8 日
編集済み: Stephen23
2020 年 3 月 8 日
"So it means that you have not enough precision in MATLAB to produce the correct answer?"
This is not specific to MATLAB, it is due to how all modern computers store fractional values as binary floating point numbers, and would occur with any numeric application that uses the very-efficient inbuilt floating point operations of your CPU. Read these to know more:
This is worth reading as well:
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!