フィルターのクリア

Difference in MATLAB function evaluation

2 ビュー (過去 30 日間)
GKH
GKH 2017 年 1 月 31 日
コメント済み: Star Strider 2017 年 1 月 31 日
According to the properties of gamma function, for any x > 0
But when I try to use it in MATLAB, it shows a very large error
gamma(150) - 149*gamma(149)
ans =
-1.5240e+247
Can anybody explain the reason for this? I need it for an approximation algorithm I am trying to develop.
Thanks in advance.
  1 件のコメント
Star Strider
Star Strider 2017 年 1 月 31 日
FWIW, I get similar results for these (in R2016b):
n = 149;
q1 = gamma(n+1) - n*gamma(n);
q2 = gamma(n+1) - factorial(n);
q3 = gamma(n+1) - prod(1:n);
q1 =
-15.2402e+246
q2 =
-819.3656e+243
q3 =
-819.3656e+243
I hope this is a problem with my understanding of the gamma function, and not a problem with its implementation in MATLAB.

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

採用された回答

Honglei Chen
Honglei Chen 2017 年 1 月 31 日
For your use case, 150! is a very large number so the numerical precision issue starts to kick in. I'd suggest you to use gammaln instead, for example, you can do
gammaln(150)-(log(149)+gammaln(149))
and the result is indeed 0.
HTH.

その他の回答 (1 件)

Jan
Jan 2017 年 1 月 31 日
編集済み: Jan 2017 年 1 月 31 日
While the absolute error is huge, the relative error is tiny:
(gamma(150) - 149*gamma(149)) / gamma(150)
>> -3.9868e-014
This is the expected error caused by using the IEEE754 double format. This stores about 16 valid digits.
If an approxmation algorithm is sensitive for this difference, it is instable and of limited use.

カテゴリ

Help Center および File ExchangeGamma Functions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by