此代码运行正确:
import sympy as sp
def xon (ton, t):
return (t-ton)/5
xonInt = sp.integrate (xon(ton, t),t)
print xonInt
但是当函数变得分段时,例如:
import sympy as sp
def xon (ton, t):
if ton <= t:
return (t-ton)/5
else:
return 0
xonInt = sp.integrate (xon(ton, t),t)
print xonInt
我收到以下错误:
文件“//anaconda/lib/python2.7/site-packages/sympy/core/relational.py”,第 103 行,位于 nonzero raise TypeError("无法确定真值 %s" % 自己)
TypeError:无法确定真值 吨 <= t
据我了解,错误是由于
ton
和t
都可以是正数和负数。这是对的吗?如果我为 t
设置正积分限制,错误不会消失。如何计算给定分段函数的积分?
更新:功能的更新版本,有效:
import sympy as sp
t = sp.symbols('t')
ton = sp.symbols('ton')
xon = sp.Piecewise((((t-ton)/5), t <= ton), (0, True))
xonInt = sp.integrate (xon,t)
print xonInt