I faced an issue when trying to use quad to integrate a function. Essentially, I have two versions of code where I define t(a) in different places. Both codes looks the same to me but the result I am getting is slightly different.
I am guessing that it is due to the error associated with using the quad method, but am not too sure. Would appreciate any help!
import numpy as np
from scipy.integrate import quad
s = 0.05
# Version 1
def POi1(w):
def t(a):
return (1/(0.27*a**(-1)+(1-0.27)*a**(-3*w-1))**(1/2))
return (np.exp(-((1+w)**2)/(2*s**2))*(1/(quad(t, 0, 1)[0]+((2/3)*(1/(np.abs(1+w)*1*(1-0.27)**2))))))
PO1 = quad(POi1, -np.inf, -1)[0]
print(PO1)
#Version 2
def t(a):
return (1/(0.27*a**(-1)+(1-0.27)*a**(-3*w-1))**(1/2))
def POi1(w):
return (np.exp(-((1+w)**2)/(2*s**2))*(1/(quad(t, 0, 1)[0]+((2/3)*(1/(np.abs(1+w)*1*(1-0.27)**2))))))
PO1 = quad(POi1, -np.inf, -1)[0]
print(PO1)
your current code doesn't work (version 2) because the function
twill take as parametera, only one parameter, but you can see that requires 2 params,aandw, it may work if you have a variablewalready defined (maybe you work in jupyter notebook) and this it may be the cause of your different resultsto make your code work and have the same result for your version 2 you can use:
in this example a closure it is used to pass the
wvariable, you will get the same results