2

此代码运行正确:

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,非零 引发TypeError(“无法确定\ n%s的真值”%自我)

TypeError:无法确定 ton <= 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
4

1 回答 1

2

分段类

您需要使用 sympy Piecewise 类

正如评论中所建议的:

Piecewise(((t - ton)/5, ton <= t), (0, True))
于 2016-03-13T15:01:13.487 回答