提供的函数不返回有效的

2024-04-20 03:51:14 发布

您现在位置:Python中文网/ 问答频道 /正文

import matplotlib.pyplot as plt
import numpy as np
import math
from scipy import *
from scipy.integrate import quad, dblquad, tplquad
q=range(1,6)
L=range(1,6)
sigmak=range(1,6)
x_lower = -3000
x_upper = 3000
y_lower = -3000
y_upper = 3000  #Integrate range
def final(a,b):  #final(a,b)=0 to be plotted on a-b plane
    m=a
    n=b
    def f3(x,y):
        mass=0
        for i in range(len(q)):
            mass+=(L[i]*exp(-(x*x+y*y/(q[i]*q[i]))/(2*sigmak[i]*sigmak[i])))/(2*3.1415926*q[i]*sigmak[i]*sigmak[i])
        return mass*(m-x)/((x-m)**2+(y-n)**2)
    val=dblquad(f3,x_lower, x_upper, lambda x : y_lower, lambda x: y_upper)
    return val[0]
y,x=np.ogrid[-1000:1000:200j,-1000:1000:200j]# plot range
f=final(x,y)

plt.figure(figsize=(9,4))
plt.subplot(121)
extent=[np.min(x),np.max(x),np.min(y),np.max(y)]
cs=plt.contour(f,extent=extent,levels=[0,0.1],colors=["b","r"],linestyles=["solid","dashed"],linewidths=[2,2])
plt.show()

以上是我的密码。我想在平面上画出final(x,y)=0。final(x,y)是一个函数复杂。什么时候我运行我的代码,它会

Traceback (most recent call last):
  File "test.py", line 25, in <module>
    f=final(x,y)
  File "test.py", line 22, in final
    val=dblquad(f3,x_lower, x_upper, lambda x : y_lower, lambda x: y_upper)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/integrate/quadpack.py", line 433, in dblquad
    return quad(_infunc,a,b,(func,gfun,hfun,args),epsabs=epsabs,epsrel=epsrel)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/integrate/quadpack.py", line 252, in quad
    retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/integrate/quadpack.py", line 317, in _quad
    return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/integrate/quadpack.py", line 381, in _infunc
    return quad(func,a,b,args=myargs)[0]
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/integrate/quadpack.py", line 252, in quad
    retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/integrate/quadpack.py", line 317, in _quad
    return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
quadpack.error: Supplied function does not return a valid float.

那我有什么问题?如果有人能帮我,谢谢你!你知道吗


Tags: inpyreturnnplinerangescipyupper
1条回答
网友
1楼 · 发布于 2024-04-20 03:51:14

不知道你想在这里做什么

dblquad返回2个浮点值、结果积分和误差估计。 通过像下面这样的for循环,我们可以导出一个数组,但我认为这不是您需要的

def final(a,b):  #final(a,b)=0 to be plotted on a-b plane
    outcome = []
    for i in range(len(a)):
        #print(i)
        print(a[i])
        m=a[i]
        n=b[i]
        def f3(x,y):
            mass=0
            for i in range(len(q)):
                mass+=(L[i]*exp(-(x*x+y*y/(q[i]*q[i]))/(2*sigmak[i]*sigmak[i])))/(2*3.1415926*q[i]*sigmak[i]*sigmak[i])
            return mass*(m-x)/((x-m)**2+(y-n)**2)          

        val=dblquad(f3,a[0], a[-1], lambda x :  m, lambda x: a[-1])

        outcome.append(val)
    return np.array(outcome)

y=np.ogrid[-10:10:10j]
x=np.ogrid[-10:10:10j]
f=final(x,y)

对你想要达到的目标要更加精确。你知道吗

相关问题 更多 >