Inf and NaN problem
조회 수: 8 (최근 30일)
이전 댓글 표시
I have to do some computations with very big numbers values, so my matlab code returns "Inf" and "NaN.. I want to know if there is a way to avoid this problem? here is the part of my code that returns Inf and NaN
UtiliteProba(b,l)=exp((UtiliteB(b,l)+UtiliteC(b,l))/T);
proba(b,l)=UtiliteProba(b,l)/constante(1,b);
Actually the "UtiliteProba"= Inf and "proba"=NaN , the "constante" is equal to Inf too..
thank you so much for your help
댓글 수: 0
답변 (2개)
Iain
2015년 1월 14일
Logarithms are your friends. Here's what I mean maths:
log(UtiliteProba) = (UtiliteB(b,l)+UtiliteC(b,l))/T (values in the range of a few hundred correspond to the maximum values matlab can handle with doubles)
proba = e^(log(UtiliteProba) - log(constante(1,b))
log(proba) = (log(UtiliteProba) - log(constante(1,b))
Star Strider
2015년 1월 14일
The only way I can imagine to avoid the Inf and NaN values (assuming none of the arguments ‘UtiliteB’ and ‘UtiliteC’ are either Inf or NaN) is to not take the exponential and keep them as logarithms until you need to actually evaluate them as exponentials:
UtiliteProba(b,l) = ((UtiliteB(b,l)+UtiliteC(b,l))/T);
proba(b,l) = UtiliteProba(b,l) - log(constante(1,b));
댓글 수: 2
Star Strider
2015년 1월 14일
‘log(constante) would be log(inf)’
If ‘constante = Inf’, then none of your calculations using it will produce any useful results.
You need to review your calculation of ‘constante’ and see what the problem is with it.
참고 항목
카테고리
Help Center 및 File Exchange에서 NaNs에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!