0

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?

3
  • 3
    The code you posted is visibly different from the code in the stack trace. The code in the stack trace has no lambda y. Make sure you're running the code you think you are. Commented Dec 28, 2016 at 17:52
  • 2
    I see you edited your post... but since your code still visibly doesn't match the stack trace, it's clear that you tried to just spot-fix the part I pointed out. Don't do that. Run code, get the error message, and then copy-paste both the exact code you ran and the exact error message into your question. Anything else hides bugs, introduces other bugs, and generally makes it much more difficult for anyone to help you. Commented Dec 28, 2016 at 17:59
  • scipy.integrate.quad expects a function as its first argument. You give it np.exp(-y**2), which is a number. Try replacing it with lambda x: np.exp(-x**2). Also, read the scipy.integrate.quad docs: docs.scipy.org/doc/scipy-0.18.1/reference/generated/… Commented Dec 28, 2016 at 18:15

1 Answer 1

2

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! It helps a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.