Issue
I am doing a multi integral with 4 variables, among them 2 have limits as functions. However the error appears on one of my constant-limit variable. Really cannot figure our why. Many thanks for your advice!
from numpy import sqrt, sin, cos, pi, arcsin, maximum
from sympy.functions.special.delta_functions import Heaviside
from scipy.integrate import nquad
def bmax(x):
return 1.14*10**9/sin(x/2)**(9/7)
def thetal(x,y,z):
return arcsin(3.7*10**15*sqrt(cos(x/2)**2/10**6-1.23*10**10/z+0.003*sin(x/2)**2*(2.51*10**63/sin(x/2)**9/y**7-1))/(z*sin(x/2)**2*cos(x/2)*(2.51*10**63/sin(x/2)**9/y**7-1)))
def rt(x,y):
return 3.69*10**12/(2.5*10**63/sin(x/2)**7*y**7-sin(x/2)**2)
def rd(x,y):
return maximum(1.23*10**10,rt(x,y))
def rl(x,y):
return rd(x,y)*(sqrt(1+5.04*10**16/(rd(x,y)*cos(x/2)**2))-1)/2
def wbound():
return [1.23*10**10,3.1*10**16]
def zbound():
return [10**(-10),pi-10**(-10)]
def ybound(z):
return [0,bmax(z)-10**(-10)]
def xbound(z,y,w):
return [thetal(z,y,w),pi-thetal(z,y,w)]
def f(x,y,z,w):
return [5.77/10**30*sin(z)*sin(z/2)*y*sin(x)*Heaviside(w-rl(z,y))*Heaviside(w-rd(z,y))/w**2]
result = nquad(f, [xbound, ybound,zbound,wbound])
Solution
The reason for that error is that although you don't want these bounds to depend on the variables, nquad
still passes the variables to the functions you provide to it. So the bound functions have to take the right number of variables:
def wbound():
return [1.23*10**10,3.1*10**16]
def zbound(w_foo):
return [10**(-10),pi-10**(-10)]
def ybound(z, w_foo):
return [0,bmax(z)-10**(-10)]
def xbound(z,y,w):
return [thetal(z,y,w),pi-thetal(z,y,w)]
Now the functions zbound
and ybound
accept the extra variables but simply ignore them.
I'm not sure about the last bound, xbound(...)
: Do you want the variables y
and z
to be flipped? The supposedly correct ordering according to the definition of scipy.integrate.nquad
would be
def xbound(y,z,w):
...
Edit: As kazemakase pointed out, the function f
should return a float
instead of a list so the brackets [...]
in the return statement should be removed.
Answered By - obachtos
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.