Reputation: 23
I would like to integrate a function with respect to (z) and make (x) and (y) as arguments. My goal is to get the result of integration at different location (x,y), In this case, I should get 16 values of integration which correspond to (x1,y1), (x1,y2) ..., (x2,y1) ... and so on. This is the code:
import numpy as np
import math
import scipy.integrate
a = 5
b = 6
xn=np.linspace(0,3,4)
yn=np.linspace(3,6,4)
x,y=np.ix_(xn,yn)
def fun(z,x,y):
model=(x**2/a**2+y**2/b**2+z**2/a**2)**0.5
#print(model)
return model
def int(x,y):
int=scipy.integrate.quad(fun,0,10,args=(x,y,))[0]
print (int)
return int
integral = int(x,y)
print (integral)
But I got this error message:
....
int=scipy.integrate.quad(model,0,10,args=(x,y,))[0]
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-
packages/scipy/integrate/quadpack.py", line 254, in quad
retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-
packages/scipy/integrate/quadpack.py", line 319, in _quad
return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
quadpack.error: Supplied function does not return a valid float.
Please could anyone show me how to fix this error and thank you in advance.
Upvotes: 2
Views: 3976
Reputation: 7842
As the error suggests, your fun
function should be returning a float
but is instead returning a 2D array:
[[ 1.11803399 1.20185043 1.30170828 1.41421356]
[ 1.13578167 1.21837779 1.31698308 1.42828569]
[ 1.18743421 1.26666667 1.36177988 1.46969385]
[ 1.26885775 1.34329611 1.43333333 1.53622915]]
The function that you are integrating should evaluate to a single number at some point in parameter space.
If you are trying to do an N dimensional integral, you may want to look at scipy.integrate.nquad
.
Upvotes: 3