フィルターのクリア

gamma function for N>100

9 ビュー (過去 30 日間)
Ladan Avazpour
Ladan Avazpour 2018 年 10 月 8 日
コメント済み: Ladan Avazpour 2018 年 10 月 9 日
Hi, everyone.
I have written a simple code by using a gamma function. when I choose N>98, the answer is Nan. What should I do to fix this problem?
function sii = diagonals(n, al, alp, bet)
ssi = zeros(1,n+1);
for i = 1:n+1
sii(i) = (2^(al+1)*(gamma(i-1+alp+1))*(gamma(i-1+bet+1)))/((2*(i-1)+al+1)*(factorial(i-1))^2);
end
end
  2 件のコメント
John D'Errico
John D'Errico 2018 年 10 月 8 日
編集済み: John D'Errico 2018 年 10 月 8 日
Is there a good reason why you have not just used gamma itself? Why would you write your own poor code to compute something that is computed well already for you? I'm not going to try to decrypt what you are doing based on undocumented, poorly written code. Perhaps you are trying to write your own incomplete gamma. Oops, also exists already, part of MATLAB.
Ladan Avazpour
Ladan Avazpour 2018 年 10 月 8 日
I have done this after I found that the gamma function is not working for big number and I think it may work if I can add something to it. In addition, this is my first time using Matlab and I just learning.

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

採用された回答

Walter Roberson
Walter Roberson 2018 年 10 月 9 日
If gammaln cannot be made to suit your purpose then you should use the symbolic toolbox or equivalent
  1 件のコメント
Ladan Avazpour
Ladan Avazpour 2018 年 10 月 9 日
Thank you. gamma(sym(X)) is working properly.(after double(x))

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

その他の回答 (1 件)

John D'Errico
John D'Errico 2018 年 10 月 8 日
No. You will not make it work for large values, when they exceed the limits of gamma itself. Your code is not better than what you will find inside gamma itself. Sorry, but NOT.
Instead, consider using the function gammaln, the natural log of the gamma function. In essentially all cases when gamma produces an overflow, gammaln will be the solution, by re-writing what you are doing to accommodate logs. Only consider exponentiating at the end.
gammaln(10000)
ans =
82099.7174964424
Those big numbers are meaningless in a double precision context, because they would vastly overflow the dynamic range of a double precision number.
  1 件のコメント
Ladan Avazpour
Ladan Avazpour 2018 年 10 月 9 日
Thank you

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

カテゴリ

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