gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:18:37 28 4
gpt4 key购买 nike

我想整合一个关于 (z) 的函数,并将 (x) 和 (y) 作为参数。我的目标是获得不同位置 (x,y) 的积分结果,在这种情况下,我应该获得对应于 (x1,y1), (x1,y2) ..., (x2, y1) ...等等。这是代码:

 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)

但是我收到了这个错误信息:

 ....
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.

谁能告诉我如何解决这个错误,提前谢谢你。

最佳答案

正如错误提示的那样,您的 fun 函数应该返回一个 float 而不是返回一个二维数组:

[[ 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]]

您正在集成的函数应该在参数空间中的某个点评估为单个数字。

如果您正在尝试进行 N 维积分,您可能需要查看 scipy.integrate.nquad

关于python - quadpack.error : Supplied function does not return a valid float in python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22729857/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com