solve system ode --> TypeError: cannot determine truth value of Relational

3,597 views
Skip to first unread message

Dabitto

unread,
Dec 28, 2017, 1:01:56 PM12/28/17
to sympy
Hello,

I have the following script .

import sympy as sym

i, r1, c1, r2, c2, t = sym.symbols('i, r1, c1, r2, c2, t' )
x1 = sym.Function('x1')
x2 = sym.Function('x2')

eq1 = r1*c1*sym.Derivative(x1(t),t) + x1(t) - x2(t) - r1*i
eq2 = r2*c1*sym.Derivative(x1(t),t) + r2*c2*sym.Derivative(x2(t),t) + x2(t) - r2*i
sol=sym.dsolve((eq1, eq2))
print(sol)


If I run this I have an error : TypeError: cannot determine truth value of Relational

Someone can help me please ?

Thank you

Dabitto

Leonid Kovalev

unread,
Dec 28, 2017, 1:48:05 PM12/28/17
to sympy
The problem is that the solver needs to know the sign of a certain expression in terms of the coefficients (the discriminant of a polynomial), and it cannot determine the sign based on the information given, as the discriminant ends up being `D = (1/(c2*r2) - 1/(c1*r1))**2`. Symbols are not automatically assumed real; so the square of some expression like that could even be negative. It's a good idea to declare symbols as real, by
 
sym.symbols('i, r1, c1, r2, c2, t', real=True)

 but this doesn't solve the problem because it might happen that c1*r1 == c2*r2, and then D is 0. 

I just edited the source to tell it which branch to choose, and got the result:

[Eq(x1(t), C1*exp(t*(-(c1*r1 - c2*r2)/(2*c1*c2*r1*r2) - (c1*r1 + c2*r2)/(2*c1*c2*r1*r2)))/(c1*r1) + C2*exp(t*((c1*r1 - c2*r2)/(2*c1*c2*r1*r2) - (c1*r1 + c2*r2)/(2*c1*c2*r1*r2)))/(c1*r1) + i*(r1 + r2)), Eq(x2(t), C1*(1/(c1*r1) - (c1*r1 - c2*r2)/(2*c1*c2*r1*r2) - (c1*r1 + c2*r2)/(2*c1*c2*r1*r2))*exp(t*(-(c1*r1 - c2*r2)/(2*c1*c2*r1*r2) - (c1*r1 + c2*r2)/(2*c1*c2*r1*r2))) + C2*(1/(c1*r1) + (c1*r1 - c2*r2)/(2*c1*c2*r1*r2) - (c1*r1 + c2*r2)/(2*c1*c2*r1*r2))*exp(t*((c1*r1 - c2*r2)/(2*c1*c2*r1*r2) - (c1*r1 + c2*r2)/(2*c1*c2*r1*r2))) + i*r2)]

So at least you have your solution... Unfortunately I cannot think of any workaround at user level. I think the handling of D here should be changed in two ways:

a) test it with D.is_positive etc instead of D>0 which throws the error you got
b) if the sign can't be determined, return a Piecewise object listing the options with appropriate cases:

Piecewise((sol1, D>0), (sol2, D<0), (sol3, Eq(D, 0)))

Dabitto

unread,
Dec 29, 2017, 4:38:25 AM12/29/17
to sympy
Hi Leonid,
Thank you for spending time for my problem.
Your answer is very clear.
Regards,
Dabitto

Leonid Kovalev

unread,
Dec 30, 2017, 7:15:34 PM12/30/17
to sympy
No problem. I spent a bit more time on this problem, and submitted a pull request which adds support for symbolic-coefficient systems like yours. So there's hope some future version of SymPy will handle these.
Reply all
Reply to author
Forward
0 new messages