此问题在此处已有答案:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()(10个答案)
昨天关门了。
我目前正在尝试绘制一个分段函数,看起来像下面这样:
def kappa_function(x):
if (x < 1873):
return 120.5 - 0.1488*(x - 273.15)+ (1.554e-4)*(x-273.15)**2 - 9.153e-8*(x - 273.15)**3 + 2.093e-11*(x - 273.15)**4
elif x >=1873:
return 42-(x-1873)/70 + ((x-1873)**2)/500000
tempspace = np.linspace(200,10000,10000)
kappa_f = kappa_function(tempspace)
但是,当我运行这个时,我得到以下结果:
Traceback (most recent call last):
File "C:\Users\smith\Documents\Fenics\carbon-constants.py", line 20, in <module>
kappa_f = kappa_function(tempspace)
File "C:\Users\smith\Documents\Fenics\carbon-constants.py", line 14, in kappa_function
if (x < 1873):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
如何解决此问题?
先谢了
编辑:进一步提出的答案,如果我们将这个系统扩展到condlist
中的3个条件,则错误返回:
def kappa_function(x):
condlist = [x < 1873, 1873 < x < 2000, x > 2000]
funclist = [lambda x: 120.5 - 0.1488*(x - 273.15)+ (1.554e-4)*(x-273.15)**2 - 9.153e-8*(x - 273.15)**3 + 2.093e-11*(x - 273.15)**4,
lambda x: 42-(x-1873)/70 + ((x-1873)**2)/500000, 1]
return np.piecewise(x, condlist, funclist)
如何解决这一问题?
1条答案
按热度按时间mum43rcc1#
你可以使用np.piecewise()来创建这样的分段函数。