Issue with gammainc(x,a) for small x and larger a
Mostra commenti meno recenti
Hi,
I've been having some trouble with the gammainc(x,a) function.
When x = 0.01 and a = 100, MATLAB says gammainc(x,a) = 0.
However, it should equal approx. 1e-358 (according to wolfram alpha).
How can I keep the precision in the value that close to zero?
Thank you!
1 Commento
David Hill
il 2 Giu 2020
You could try programming the gammainc function yourself using symbolic variables; otherwise, the function cannot produce a result better than double precision.
Risposta accettata
Più risposte (2)
David Goodmanson
il 2 Giu 2020
Modificato: David Goodmanson
il 3 Giu 2020
Hi John,
Matlab does not appear to have a symbolics version of the incomplete gamma function, but it's still possible to make progress. The function is
1/gamma(a) * integral {0, x0} x^(a-1) exp(-x)
In double precision, the smallest expressible number is
realmin ans = 2.225073858507201e-308
so a calculation down around 1e-358 is going to underflow to zero. However, you can do the integral separately from the gamma function and do some borrowing of the exponent to get a result.
a = 100
fun = @(x) x.^(a-1).*exp(-x)
I = integral(fun,0,.01)
I = 9.901478680963859e-203
This appears to be fairly accurate, although a version using variable precision arithmetic might do better.
G = gamma(a)
G = 9.332621544394404e+155
I think we can assume this one to be accurate.
You can't divide I by G because that drops below realmin, but you can do
I200 = 1e200*I;
Gm100 = 1e-100*G;
gam_inc = I200/Gm100
gam_inc = 1.060953627430776e-58
and putting back the 1e-300 that was borrowed results in a value near 1e-358
1 Commento
John Fullerton
il 3 Giu 2020
Steven Lord
il 2 Giu 2020
Depending on what you plan to do with this tiny number, using the 'scaledlower' option for gammainc may help. I computed the overall scale factor symbolically as otherwise it would overflow.
x = 0.01;
a = 100;
L = gammainc(x, a, 'scaledlower')
scalefactor = gamma(sym(a)+1)*exp(sym(x))/sym(x)^a;
vpaScalefactor = vpa(scalefactor, 20)
result = vpa(L/scalefactor, 20)
1 Commento
John Fullerton
il 3 Giu 2020
Categorie
Scopri di più su Linear Matrix Inequalities in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!