Exponential function produces error with integrate command

5 vues (au cours des 30 derniers jours)
Ben Miller
Ben Miller le 7 Déc 2020
Modifié(e) : Ben Miller le 7 Déc 2020
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.

Réponse acceptée

David Goodmanson
David Goodmanson le 7 Déc 2020
Modifié(e) : David Goodmanson le 7 Déc 2020
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 commentaire
Ben Miller
Ben Miller le 7 Déc 2020
Modifié(e) : Ben Miller le 7 Déc 2020
That is what I suspected, and if I combine everything into one exp expression it works.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by