我试图对(0,1)上两个分段函数的乘积进行积分。我的代码如下:
def gfunc1(x):
if np.logical_and(x >= 0.5, x<=1):
temp =(-1.0)*np.pi*np.cos(np.pi*x)+(x+0.5)*(np.pi**2)*np.sin(np.pi*x)
else:
temp = np.pi*np.cos(np.pi*x)+(1.5-x)*(np.pi**2)*np.sin(np.pi*x)
return temp
def lfunc(x,*args):
return args[0](x)*args[1](x,args[2])
def bfunc(x,i):
if (i == 0):
if np.logical_and(x <= dxl[1], x>= dxl[0]):
temp = (dxl[1] - x)/(dxl[1]-dxl[0])
else:
temp = 0.0
elif (i == (len(dxl)-1)):
if np.logical_and(x >= dxl[len(dxl)-2], x <= dxl[len(dxl)-1]):
temp = (x- dxl[len(dxl)-2])/(dxl[len(dxl)-1] - dxl[len(dxl)-2])
else:
temp = 0.0
else:
if np.logical_and(dxl[i-1]<=x,x<=dxl[i]):
temp = (x - dxl[i-1])/(dxl[i]-dxl[i-1])
elif np.logical_and(dxl[i]<=x,x<=dxl[i+1]):
temp =(dxl[i+1]-x)/(dxl[i+1]-dxl[i])
else:
temp = 0.0
return temp
dxl = np.linspace(0,1,100)
res = quadrature(lfunc,0,1,args=(gfunc1,bfunc,1))
print res[0]
我收到错误消息:ValueError:具有多个元素的数组的真值不明确。使用a.any()或a.all()。 有人能帮我修一下这个错误吗?非常感谢你。在
问题可能出在分段函数的定义上。在
试着这样做:
注意对逻辑向量数组使用},
以及使用
&
而不是{np.where
作为向量化if-else替代。在我没有尝试修复您的整个代码,其他需要if-else的函数可能也需要这样重新定义。在
相关问题 更多 >
编程相关推荐