Tanja - 1 year ago 164

Python Question

This code runs correctly:

`import sympy as sp`

def xon (ton, t):

return (t-ton)/5

xonInt = sp.integrate (xon(ton, t),t)

print xonInt

But when the function becomes piecewise, e.g.:

`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

I get the following error:

File "//anaconda/lib/python2.7/site-packages/sympy/core/relational.py", line > 103, innonzero

raise TypeError("cannot determine truth value of\n%s" % self)

TypeError: cannot determine truth value of

ton <= t

As far as I understand, the error is due to the fact that both

`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

Answer Source

You need to use the sympy Piecewise class.

As suggested in the comments:

```
Piecewise(((t - ton)/5, ton <= t), (0, True))
```