Tom
Tom

Reputation: 233

Integrating over an interpolate function (interp1d) in python

I am trying to do double integration over an interpolated function, in which r = r(x,y).

from scipy import interpolate
import scipy as sp

r = [0, 1, 2]
z = [0, 1, 2]

def cartesian(x, y, f):
    r = sp.sqrt(x**2 + y**2)
    return f(r)

interp = interpolate.interp1d(r, z)

print(cart(1,1,interp))

a = sp.integrate.dblquad(cart, 0, 1, lambda x: 0, lambda x: 1, args=(interp))
print(a)

Executing the Cartesian function once produces the correct answer. However the integral gives the the following error:

TypeError: integrate() argument after * must be an iterable, not interp1d

I don't understand why my function isn't iterable and do not know how to convert it into an iterable form. Many thanks for any help.

Upvotes: 1

Views: 866

Answers (1)

John Zwinck
John Zwinck

Reputation: 249394

args is supposed to be a sequence of arguments, so:

sp.integrate.dblquad(cart, 0, 1, lambda x: 0, lambda x: 1, args=(interp,))

The comma after interp is critical: in Python, (x) is just x, but (x,) is a tuple (i.e. a sequence).

Upvotes: 1

Related Questions