Borrar filtros
Borrar filtros

Getting NaN while using the ratio of power and factorial

2 visualizaciones (últimos 30 días)
Yash Khandelwal
Yash Khandelwal el 13 de Jul. de 2022
Comentada: James Tursa el 13 de Jul. de 2022
With the following piece of code, I get NaN
a2= ((-sqrt(d))^lp);
a3 =((-sqrt(d))^l);
for m=0:500
a1= ((2^m)*(d^m))/(factorial(m))
Is there any way to modify the code to get rid of the issue?
  1 comentario
James Tursa
James Tursa el 13 de Jul. de 2022
Cross posted on stackoverflow and answered there over 4 hours ago. If you had simply checked the answer there you could have saved yourself the trouble of posting here and getting essentially the same answer.

Iniciar sesión para comentar.

Respuestas (4)

Torsten el 13 de Jul. de 2022
Editada: Torsten el 13 de Jul. de 2022
exp(400)*200 is quite large ...
format long
a2= ((-sqrt(d))^lp);
a3 =((-sqrt(d))^l);
a1 = 1.0;
sum1 = a1;
for m=0:500
a1 = a1* 2*d/(m+1);
sum1 = sum1*a2*a3
sum1 =
ans =

Steven Lord
Steven Lord el 13 de Jul. de 2022
m = 500;
d = 200;
The numerator and denominator of your expression (for sufficiently large m) both overflow to inf. Dividing infinity by infinity results in a NaN.
numerator = 2^m*d^m
numerator = Inf
denominator = factorial(m)
denominator = Inf
x = numerator / denominator
x = NaN
One potential approach to avoid this is to avoid explicitly computing 2^m, d^m, or factorial(m).
numeratorVector = repmat(2*d, 1, m); % prod(numeratorVector) would effectively give numerator
denominatorVector = 1:m; % prod(denominatorVector) would effectively give denominator
xVector = numeratorVector./denominatorVector;
format longg
x2 = prod(xVector)
x2 =
Let's check symbolically.
numeratorSymbolic = sym(2*d)^m;
vpa(numeratorSymbolic) % Pretty big
ans = 
denominatorSymbolic = factorial(sym(m));
vpa(denominatorSymbolic) % Also pretty big
ans = 
x3 = vpa(numeratorSymbolic/denominatorSymbolic) % Big but not quite as big as above
x3 = 
You could be a little more sophisticated / clever if you wanted (preemptively cancelling out factors of 2 in numeratorVector by dividing even values in denominatorVector by 2.) Or you could keep track of x for each value of m then figure out what you need to multiply it by to get x for the next value of m.

Benjamin Thompson
Benjamin Thompson el 13 de Jul. de 2022
The factorial function output increases very fast as input increases. See "doc factorial" for details. The output is "inf" for input of 171 or larger.
  1 comentario
Steven Lord
Steven Lord el 13 de Jul. de 2022
But 400^m overflows for m > 118 so this whole expression becomes infinite or undefined beyond that point.
d = 200;
ans = 1.1043e+307
ans = Inf

Iniciar sesión para comentar.

Vijeta Singh Yadav
Vijeta Singh Yadav el 13 de Jul. de 2022
Data overflow and underflow problem exists in the code
visit this link for avoid data overflow and underflow problems.


Más información sobre Debugging and Analysis en Help Center y File Exchange.


Community Treasure Hunt

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

Start Hunting!

Translated by