使用指数(FORTRAN)时溢出
我在计算某些指数方面遇到问题。问题是,我有一笔指数的总和,每个指数都给我溢出,所以我没有找到一种使用对数使自己摆脱如此大数字的方法。 对于下面的代码,程序可以运行正常,而无需溢出,但我需要使用更大的特征值值,可以说,至少要大300倍。我找不到使用对数围绕Exponentias的总和的方法。
如您所见,问题不是最终结果,而是中间计算中的问题。
请让我知道此信息是否不完整
program testing
implicit none
integer, parameter :: q = SELECTED_REAL_KIND(10)
integer, parameter :: qc = SELECTED_REAL_KIND(10)
integer :: Ndim
real (q) :: Temperature
real (q),allocatable :: Eigenvalues (:),rho (:)
real (q) :: Z
integer :: i
Ndim=4
allocate (rho (Ndim),Eigenvalues(Ndim))
!Parameter Definition
Temperature=0.00000158
Eigenvalues(1)=-0.000893
Eigenvalues(2)=-0.000893
Eigenvalues(3)=-0.000788
Eigenvalues(4)=-0.000446
Z = 0.;
rho = 0.
do i =1, Ndim
Z = Z + exp ( - Eigenvalues (i)/(Temperature))
write(*,*) Z
enddo
do i = 1, Ndim
rho (i) = rho (i) + exp ( - Eigenvalues (i)/(Temperature) )/Z
write(*,*) rho(i)
enddo
end
I'm having problems regarding the calculation of some exponentials. The problem is, i have a sum of exponentials that each one gives me overflow, so i didn't found a way to use logarithm to free myself of such big numbers.
For the code below, the program runs OK without overflow but I need to use bigger values of Eigenvalues, lets say, at least 300 times bigger. I couldn't find a way to work around the sum of exponentias using logarithm.
As you can see, the problem is not in the final result, but in the intermediate calculations.
Please let me know if this information is incomplete
program testing
implicit none
integer, parameter :: q = SELECTED_REAL_KIND(10)
integer, parameter :: qc = SELECTED_REAL_KIND(10)
integer :: Ndim
real (q) :: Temperature
real (q),allocatable :: Eigenvalues (:),rho (:)
real (q) :: Z
integer :: i
Ndim=4
allocate (rho (Ndim),Eigenvalues(Ndim))
!Parameter Definition
Temperature=0.00000158
Eigenvalues(1)=-0.000893
Eigenvalues(2)=-0.000893
Eigenvalues(3)=-0.000788
Eigenvalues(4)=-0.000446
Z = 0.;
rho = 0.
do i =1, Ndim
Z = Z + exp ( - Eigenvalues (i)/(Temperature))
write(*,*) Z
enddo
do i = 1, Ndim
rho (i) = rho (i) + exp ( - Eigenvalues (i)/(Temperature) )/Z
write(*,*) rho(i)
enddo
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于Boltzmann因素,而不是明确地计算
EXP(-e/T) exp(-e/t)的高温或低温泰勒膨胀来计算输出。
另请注意,由于能量是相对的,因此您可以从所有能量中减去任意
e0
,并获得相同的结果,因此修改代码会更稳定,更准确。
For Boltzmann factors, rather than calculating
exp(-E/T)
explicitly, you will likely need to implement afunction boltzmann_factor(energy, temperature)
which checks the relative sizes of energy and temperature and calculates the output using the high-temperature or low-temperature Taylor expansions ofexp(-E/T)
where necessary.Also note that since energy is relative you can subtract an arbitrary
E0
from all of your energies and obtain the same result, so modifying your code would givethis will be more stable, and more accurate.