Numerical integration involving log(exp(f(x))) shows NaN

4 ビュー (過去 30 日間)
YI-WEN PAN
YI-WEN PAN 2023 年 6 月 11 日
編集済み: Torsten 2024 年 9 月 9 日
Hi everyone,
As title mentioned, when I integrated the function log(exp(f(x))) within the range [-100,100], the result showed "-inf"
,but if I changed log(exp(f(x))) into the equivalent form which is f(x), the integral result was a numerical value.
f(x)=-(x^2)/2, btw.
The following code may explain clearly
f1 = @(x) log(exp(-(x.^2)/2));
r1 = integral(f1,-100,100)
Warning: Inf or NaN value encountered.
r1 = -Inf
f2 = @(x) -(x.^2)/2;
r2 = integral(f2,-100,100)
r2 = -3.3333e+05
My question is why does r1 get "-Inf"?
Is there any way to let r1 equal to r2, if f1 has to be the form log(exp(f(x)))?
Thank you very much in advance.

採用された回答

Animesh
Animesh 2023 年 6 月 11 日
The possible reason for getting r1=-Inf in the expression log(exp(f(x))), is due to the limited precision of floating point numbers. This can cause numeric underflow.
In this case the value of the term exp(-(x.^2)/2) will become very small as the magnitude of x grows large. Eventually the value will become so small that it will get rounded down to zero. This will cause the entire expression to evaluate to -Inf as log of 0 is negative infinity.
A workaround that you could try is using symbolic integration instead of numerical integration. This would allow you to simplify the expression log(exp(f(x))) and evaluate the integral analytically.
syms x;
f = log(exp(-(x.^2)/2));
r = simplify(int(f, x, -100, 100))
  3 件のコメント
wenji
wenji 2024 年 9 月 9 日
Instead using int, is there any way to avoid this limited precision floating point number? i am working with a triple integration so I am afriad i could not simply use int....my function would be like f=@(a,b,c) exp(a)^-1*log(1+exp(a)*b^-2)
Torsten
Torsten 2024 年 9 月 9 日
編集済み: Torsten 2024 年 9 月 9 日
If you state your problem properly (what is c ? what are the limits of integration ?) - preferably in a new question - maybe someone will be able to help.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumbers and Precision についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by