Ask Your Question

Problem with quad from SciPy

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:

reset()
forget()

from scipy.integrate import quad

var('y')
var('kx')

alpha1=var('a11,a12,a13')

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

phi1=[phi11,phi12,phi13]

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

eq1=[
quad(U1.diff(y,y)*phi1[j],0.0,1.0)[0]
-kx*quad(y*U1*phi1[j],0.0,1.0)[0]
-kx^2*quad(U1*phi1[j],0.0,1.0)[0]
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 close merge delete

1 Answer

Sort by ยป oldest newest most voted

There are two comments in order here:

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

U1*phi1[0]
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 http://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html#scipy.integrate.quad E.g.:

f(y,a12,a13,a11)=U1*phi1[0]
quad(f,0.0,1.0,args=(1,1,1,))
(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.

more

Comments

very true, thank you ...

( 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

Stats

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

Seen: 1,188 times

Last updated: Sep 25 '13