quadpack.error:提供的函数未在python中返回有效的float [英] quadpack.error: Supplied function does not return a valid float in python

查看:191
本文介绍了quadpack.error:提供的函数未在python中返回有效的float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想针对(z)集成一个函数,并以(x)和(y)作为参数.我的目标是获得在不同位置(x,y)的积分结果,在这种情况下,我应该获得16个积分值,分别对应于(x1,y1),(x1,y2)...,(x2, y1)...依此类推. 这是代码:

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.

推荐答案

错误提示,您的fun函数应该返回float,而是返回2D数组:

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.

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

If you are trying to do an N dimensional integral, you may want to look at scipy.integrate.nquad.

这篇关于quadpack.error:提供的函数未在python中返回有效的float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆