3

I am trying to integrate u -> exp(-u²/2) from -infinity to x. When I plot the function, there is a sudden drop around 21 and it goes down to 0 around 36 whereas it should be roughly constant to 2.5. How do you explain it?

import numpy as np
from scipy.integrate import quad
import matplotlib.pyplot as plt

def intExp(x):
    return quad(lambda u: np.math.exp(-u*u/2),-np.Inf, x, full_output=0)

def plot(a,b, u, v,s):
    plt.close()
    t = np.arange(a,b,s)
    plt.plot(t , map(intExp,t))
    plt.axis([a, b, u, v])
    plt.show()

plot(-10, 50, -1, 3, 1)

Thanks for the help!

Moritz
  • 5,130
  • 10
  • 40
  • 81
hokkaidi
  • 858
  • 6
  • 19

1 Answers1

3

Has to do with the step width. Changing epsabs from its default value to 1e-9 works:

import numpy as np
from scipy.integrate import quad
import matplotlib.pyplot as plt

def intExp(x):
    return quad(lambda u: np.math.exp(-u*u/2),-np.Inf, x, full_output=0,epsabs=1e-9)

def plot(a,b, u, v,s):
    plt.close()
    t = np.arange(a,b,s)
    plt.plot(t , map(intExp,t))
    plt.axis([a, b, u, v])
    plt.show()


plot(-10, 50, -1, 3, 1)
Moritz
  • 5,130
  • 10
  • 40
  • 81