Exponential function produces error with integrate command

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개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2020년 12월 7일

편집:

2020년 12월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by