Reputation: 1070
I'm trying to reproduce the error function using scipy, not using the erf method.
This is my code:
#!/usr/bin/env python
import numpy as np
import math
from scipy.integrate import quad
def error_func(y):
return 2/math.sqrt(np.pi)*quad(np.exp(-y**2), 1, np.inf, args=(y))[0]
g = [error_func(x) for x in np.arange(-1,1,0.2)]
print g
This code returns the following error message:
File "./test.py", line 9, in <module>
g = [error_func(x) for x in np.arange(-1,1,0.2)]
File "./test.py", line 7, in error_func
return 2/math.sqrt(np.pi)*quad(np.exp(-y**2), 1, np.inf, args=(y))[0]
File "/usr/local/lib/python2.7/site-packages/scipy/integrate/quadpack.py", line 316, in quad
points)
File "/usr/local/lib/python2.7/site-packages/scipy/integrate/quadpack.py", line 383, in _quad
return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)
quadpack.error: quad: first argument is not callable
If I understand correctly, the first argument of quad must be a function. I thing that I pass it right. What is going wrong in my code?
Upvotes: 0
Views: 2106
Reputation: 280572
Your first problem is that
np.exp(-y**2)
isn't a function. It's a number; specifically, it's the value of e^(-y^2). If you want to define a function that maps y
to np.exp(-y**2)
, the easiest way is with the lambda
syntax:
lambda y: np.exp(-y**2)
You have other problems, though:
(y)
isn't a tuple in args=(y)
; it's just y
in grouping parentheses. A 1-element tuple would be (y,)
.
Also, you shouldn't be passing the args
argument to quad
anyway, since your function doesn't need any additional arguments beyond the one we're integrating over.
Finally, your integration bounds are wrong; you should be integrating from 0
to the argument of the error function:
def error_func(x):
return 2 / math.sqrt(np.pi) * quad(lambda y: np.exp(-y**2), 0, x)[0]
Upvotes: 2