Ask Your Question

Problem with quad from SciPy

asked 2013-09-24 18:45:08 +0200

owari gravatar image

I have a simple code that seems to work fine (although I cannot be sure when there are several constants like a13 that seem to be integration constants while the integrals are all definite?), at least runs with no error, when the symbolic "integral" function is used instead of "quad", but now using "quad" the code runs into error. This is an example code:


from scipy.integrate import quad



phi11(y)=y*(y-1);    phi12(y)=y*(y-1)*(y+1);    phi13(y)=y*(y-1)*(y^2+1)


U1(y)=sum(alpha1[j]*phi1[j](y) for j in range(3))

     for j in range(3)]

print eq1

The error is:

quadpack.error: Supplied function does not return a valid float.

Any idea how to resolve it? I have tested many different usages of the functions inside the argument of quad, using lambda, explicitly specifying (y) as the argument of the functions, defining a function Y(y)=y so to use Y instead of y in writing the expression "yU1phi1[j]", and etc but all with no success. Searching the web also didn't help much. Please bare with me I am not an expert.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-09-25 05:24:18 +0200

Mark gravatar image

There are two comments in order here:

A) as you state yourself, the integrands of your expression, e.g.

y |--> (y - 1)*((y - 1)*(y + 1)*a12*y + (y - 1)*(y^2 + 1)*a13*y + (y - 1)*a11*y)*y

contains lots of symbolic varibles: a12,a13,a11 which are no valid floats, even after substituting a valid float for the integration variable y. That's what the error tells you ;-) . SciPy's quad has to know the numerical values of such parameters - how else could it do a numerical integral? How to supply float parameters to quad is described at E.g.:

(0.1261904761904762, 1.4009957215507927e-15)

B) having said that, and not knowing what your actual problem is, I however presume that the preceeding is not what you actually want, and that rather you want to set up some kind of linear equation (eq1 ?) with symbolic variables and numerically evaluated 'constants'. If that is so, I would disect the numerical integration of each of those constant off from the symbolic part of the code.

edit flag offensive delete link more


very true, thank you ...

owari gravatar imageowari ( 2013-09-25 15:23:27 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-09-24 18:45:08 +0200

Seen: 1,188 times

Last updated: Sep 25 '13