Exponential function produces error with integrate command

1 次查看(过去 30 天)
Hello. I'm relatively new to Matlab, and I have the following problem which boils down from a larger, more complicated integral, but for now I am working with a simplified case. I believe that I have found the source of my problem.
I wrote
fun = @(x) (exp(x).*exp(-(x.^2)/2);
q = integral(fun,-1,Inf)
The value of q is then NaN.
Why is this the case? I evaluated this integral in Wolfram Alpha and received a number.

采纳的回答

David Goodmanson
David Goodmanson 2020-12-7
编辑:David Goodmanson 2020-12-7
Hi Ben
for large x, exp(x) overflows to infinity before the exp(-(x.^2)/2) factor has a chance to bring in down. Try completing the square in the exponent:
fun = @(x) exp(-((x-1).^2)/2)*exp(1/2)
  1 个评论
Ben Miller
Ben Miller 2020-12-7
编辑:Ben Miller 2020-12-7
That is what I suspected, and if I combine everything into one exp expression it works.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by